Skip to content

Commit

Permalink
Merge pull request #15 from joshblum/new-api
Browse files Browse the repository at this point in the history
Uses the hn aloglia api instead of the thriftdb api
  • Loading branch information
igrigorik committed Aug 28, 2014
2 parents 44fdf64 + a80cf3d commit 40d4c97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
8 changes: 4 additions & 4 deletions hnbutton/button.html
Expand Up @@ -16,8 +16,8 @@

<body>
<div>
{{if .Id}}
<a onclick="parent.postMessage('vote','*')" href="http://news.ycombinator.com/item?id={{.Id}}" target="_blank">
{{if .Story_id}}
<a onclick="parent.postMessage('vote','*')" href="http://news.ycombinator.com/item?id={{.Story_id}}" target="_blank">
{{else}}
<a onclick="parent.postMessage('submit','*')" href="http://news.ycombinator.com/submitlink?u={{.Url}}&amp;t={{.Title}}" target="_blank">
{{end}}
Expand All @@ -32,7 +32,7 @@
<tr>
<td><div class="hn_cn"><s></s><i></i></div></td>
<td><div class="hn_cc"><span style="color:#333;">
{{if .Id}}
{{if .Story_id}}
{{.Points}}
{{else}}
submit
Expand All @@ -50,4 +50,4 @@
</div>
</body>
</html>
{{end}}
{{end}}
31 changes: 13 additions & 18 deletions hnbutton/hnbutton.go
Expand Up @@ -18,20 +18,16 @@ import (
var buttonTemplate, _ = template.New("page").ParseFiles("hnbutton/button.html")

type hnapireply struct {
Hits int
Results []Result
}

type Result struct {
Item Hit
NbHits int
Hits []Hit
}

type Hit struct {
Id int
Story_id int
Points int
Hits int
Num_comments int
Username string
Author string
}

func Button(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -74,7 +70,7 @@ func Button(w http.ResponseWriter, r *http.Request) {

var item Hit
if cachedItem, err := memcache.Get(c, hkey); err == memcache.ErrCacheMiss {
pageData := "http://api.thriftdb.com/api.hnsearch.com/items/_search?filter[fields][url][]=" + url.QueryEscape(req_url[0])
pageData := "http://hn.algolia.com/api/v1/search_by_date?query=" + url.QueryEscape(req_url[0])

client := &http.Client{
Transport: &urlfetch.Transport{
Expand All @@ -90,20 +86,19 @@ func Button(w http.ResponseWriter, r *http.Request) {

defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

var hnreply hnapireply
if err := json.Unmarshal(body, &hnreply); err != nil {
panic("Cannot unmarshall JSON data")
}

if hnreply.Hits == 0 {
if hnreply.NbHits == 0 {
item.Hits = 0
} else {
item.Hits = hnreply.Hits;
item.Id = hnreply.Results[0].Item.Id;
item.Points = hnreply.Results[0].Item.Points;
item.Num_comments = hnreply.Results[0].Item.Num_comments;
item.Username = hnreply.Results[0].Item.Username;
item.Hits = hnreply.NbHits;
item.Story_id = hnreply.Hits[0].Story_id;
item.Points = hnreply.Hits[0].Points;
item.Num_comments = hnreply.Hits[0].Num_comments;
item.Author = hnreply.Hits[0].Author;
}

var sdata []byte
Expand All @@ -130,7 +125,7 @@ func Button(w http.ResponseWriter, r *http.Request) {
if err := json.Unmarshal(cachedItem.Value, &item); err != nil {
panic("Cannot unmarshall hit from cache")
}
c.Infof("Fetched from memcache: %i", item.Id)
c.Infof("Fetched from memcache: %i", item.Story_id)
}

// Cache the response in the HTTP edge cache, if possible
Expand All @@ -146,7 +141,7 @@ func Button(w http.ResponseWriter, r *http.Request) {
}

} else {
c.Infof("Points: %f, ID: %i \n", item.Points, item.Id)
c.Infof("Points: %f, ID: %i \n", item.Points, item.Story_id)

if err := buttonTemplate.ExecuteTemplate(w, "button", item); err != nil {
panic("Cannot execute template")
Expand Down

0 comments on commit 40d4c97

Please sign in to comment.