diff options
| author | Adam Mathes <adam@trenchant.org> | 2018-04-27 15:52:00 -0700 | 
|---|---|---|
| committer | Adam Mathes <adam@trenchant.org> | 2018-04-27 15:52:00 -0700 | 
| commit | 5068ca5c0d88694ec1c25dce81c747d5cfa6b54e (patch) | |
| tree | 644bfb34b8525dbb7077ea5b2a3f5a1e24512b82 | |
| parent | b773d3b1fa1ca4e01b100eb5e89b88b8347740ce (diff) | |
| download | neko-5068ca5c0d88694ec1c25dce81c747d5cfa6b54e.tar.gz neko-5068ca5c0d88694ec1c25dce81c747d5cfa6b54e.tar.bz2 neko-5068ca5c0d88694ec1c25dce81c747d5cfa6b54e.zip  | |
separator for scrape
| -rw-r--r-- | models/item/item.go | 24 | ||||
| -rw-r--r-- | static/ui.html | 5 | ||||
| -rw-r--r-- | static/ui.js | 14 | ||||
| -rw-r--r-- | web/web.go | 49 | 
4 files changed, 75 insertions, 17 deletions
diff --git a/models/item/item.go b/models/item/item.go index 8d1d473..f70bba1 100644 --- a/models/item/item.go +++ b/models/item/item.go @@ -81,7 +81,13 @@ func filterPolicy() *bluemonday.Policy {  	return p  } +func ItemById(id int64) *Item { +	items, _ := Filter(0, 0, "", false, false, id) +	return items[0] +} +  func (i *Item) GetFullContent() { +	fmt.Printf("fetching from %s\n", i.Url)  	g := goose.New()  	article, err := g.ExtractFromURL(i.Url)  	if err != nil { @@ -105,9 +111,8 @@ func (i *Item) GetFullContent() {  	}  	p := filterPolicy() -	md = p.Sanitize(ht) - -	img = article.TopImage +	i.FullContent = p.Sanitize(ht) +	i.HeaderImage = article.TopImage  	_, err = models.DB.Exec(`UPDATE item                                SET full_content=?, header_image=? @@ -117,7 +122,7 @@ func (i *Item) GetFullContent() {  	}  } -func Filter(max_id int64, feed_id int64, category string, unread_only bool, starred_only bool) ([]*Item, error) { +func Filter(max_id int64, feed_id int64, category string, unread_only bool, starred_only bool, item_id int64) ([]*Item, error) {  	var args []interface{} @@ -148,13 +153,20 @@ func Filter(max_id int64, feed_id int64, category string, unread_only bool, star  		query = query + " AND item.read_state=0 "  	} +	if item_id != 0 { +		query = query + " AND item.id=? " +		args = append(args, item_id) +	} + +	// this is kind of dumb, but to keep the logic the same +	// we kludge it this way for a "by id" select  	if starred_only {  		query = query + " AND item.starred=1 "  	}  	query = query + "ORDER BY item.id DESC LIMIT 15" -	// log.Println(query) -	// log.Println(args...) +	//	log.Println(query) +	//	log.Println(args...)  	rows, err := models.DB.Query(query, args...)  	if err != nil { diff --git a/static/ui.html b/static/ui.html index 89283a4..cae2c56 100644 --- a/static/ui.html +++ b/static/ui.html @@ -38,11 +38,12 @@      <script id="item_template" type="text/jqtmp">        <h2><a target="_blank" class="i" id="i_${item_id}" href="${item.url}">${item.title }</a>       <span class={{if item.starred}}"unstar"{{else}}"star"{{/if}}>★</span> -    <span class="full">{{if item.full}}▼{{else}}►{{/if}}</span>      </h2>        <p class="dateline" style="clear: both;">          <a href="${item.feed_url}">${item.feed_title}</a> | <a href="${item.url}">${item.p_url}</a> -        | ${item.feed_category} +    | ${item.feed_category} | +    <span class="full">{{if item.full}}hide{{else}}scrape{{/if}} full text</span> +          </p>        {{if item.header_image}}        <div class="img"><img src="${item.header_image}" /></div> diff --git a/static/ui.js b/static/ui.js index 0c60630..4e6d7e0 100644 --- a/static/ui.js +++ b/static/ui.js @@ -208,7 +208,7 @@ var ControlsView = Backbone.View.extend({  var Item = Backbone.Model.extend({      idAttribute: "_id", -    url: '/item/', +    url: '/item',      initialize: function() {          var p_url = this.get('url'); @@ -252,6 +252,16 @@ var Item = Backbone.Model.extend({      full: function() {          this.set({'full': !(this.get('full'))} ); +        // this should just use this.fetch() but +        // it kept GETing from /item instead of /item/id +        // so just hacking this in for now + +        if(this.get('full_content') == "") {         +            $.getJSON('/item/' + this.get('_id'), function(data) { +                var i = App.items.get(data['_id']) +                i.set('full_content', data['full_content']); +            }); +        }      }  }); @@ -259,6 +269,8 @@ var Item = Backbone.Model.extend({  var ItemCollection = Backbone.Collection.extend({      model: Item, +    url: '/item', +  	initialize: function() {  	    _.bindAll(this, 'boot', 'reboot');      }, @@ -52,7 +52,7 @@ func streamHandler(w http.ResponseWriter, r *http.Request) {  	}  	var items []*item.Item -	items, err := item.Filter(int64(max_id), feed_id, category, unread_only, starred_only) +	items, err := item.Filter(int64(max_id), feed_id, category, unread_only, starred_only, 0)  	if err != nil {  		log.Println(err)  	} @@ -63,12 +63,17 @@ func streamHandler(w http.ResponseWriter, r *http.Request) {  }  func itemHandler(w http.ResponseWriter, r *http.Request) { -	var i item.Item -	err := json.NewDecoder(r.Body).Decode(&i) -	if err != nil { -		log.Println(err) -	} else { -		i.Save() +	switch r.Method { +	case "PUT": +		var i item.Item +		err := json.NewDecoder(r.Body).Decode(&i) +		if err != nil { +			log.Println(err) +		} else { +			i.Save() +		} +	case "GET": +		fullTextHandler(w, r)  	}  	defer r.Body.Close()  } @@ -181,6 +186,31 @@ func imageProxyHandler(w http.ResponseWriter, r *http.Request) {  	return  } +func fullTextHandler(w http.ResponseWriter, r *http.Request) { +	fmt.Printf("request: %v\n\n", r) + +	fmt.Printf("url string: %s\n\n", r.URL.String()) + +	itemID, _ := strconv.Atoi(r.URL.String()) +	//	fmt.Printf("item id: %v\n\n", itemID) + +	if itemID == 0 { +		fmt.Printf("wah wah wah\n") +		return +	} + +	i := item.ItemById(int64(itemID)) +	// fmt.Println("item fetched: %v\n\n", i) + +	if i.FullContent == "" { +		i.GetFullContent() +	} + +	w.Header().Set("Content-Type", "application/json") +	js, _ := json.Marshal(i) +	w.Write(js) +} +  var AuthCookie = "auth"  var SecondsInAYear = 60 * 60 * 24 * 365 @@ -236,7 +266,10 @@ func Serve() {  	http.Handle("/static/", http.StripPrefix("/static/", fs))  	http.HandleFunc("/stream/", AuthWrap(streamHandler)) -	http.HandleFunc("/item/", AuthWrap(itemHandler)) + +	//	http.HandleFunc("/item/", AuthWrap(itemHandler)) +	http.Handle("/item/", http.StripPrefix("/item/", AuthWrap(itemHandler))) +  	http.HandleFunc("/feed/", AuthWrap(feedHandler))  	http.HandleFunc("/tag/", AuthWrap(categoryHandler))  	http.Handle("/image/", http.StripPrefix("/image/", AuthWrap(imageProxyHandler)))  | 
