Google App Engine Go - Reddit API返回错误429

时间:2013-05-23 10:06:45

标签: api google-app-engine go reddit

我正在尝试在Google App Engine中的Golang中创建Reddit API。我的代码:

package RedditAPI

import(
    "appengine"
    "encoding/json"
    "io/ioutil"
    "net/http"
    "appengine/urlfetch"
    "time"
    "net/url"
)

func GetTopSubmissions(c appengine.Context){
    one, two:=Call(c, "http://www.reddit.com/r/Bitcoin/top.json", "POST", nil);
    c.Infof("%v, %v", one, two);
}


func Call(c appengine.Context, address string, requestType string, values url.Values)(map[string]interface{}, error){
    req, err:=http.NewRequest("GET", address, nil)
    if err!=nil{
        c.Infof("Request: %v", err)
        return nil, err
    }

    req.Header.Add("User-Agent", "This is a very creative name for a Reddit bot v1.0 by /u/username")
    c.Infof("%v", req.Header.Get("User-Agent"))
    c.Infof("%v", req)
    c.Infof("%v", req.UserAgent())

    duration, err:= time.ParseDuration("60s")
    tr := &urlfetch.Transport{Context: c, Deadline: duration}

    resp, err:=tr.RoundTrip(req)
    if err != nil {
        c.Infof("Post: %v", err)
        return nil, err
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        c.Infof("ReadAll: %v", err)
        return nil, err
    }
    result := make(map[string]interface{})
    err = json.Unmarshal(body, &result)
    if err != nil {
        c.Infof("Unmarshal: %v", err)
        c.Infof("%s", body)
        return nil, err
    }
    return result, nil
}

返回

2013/05/23 03:00:34 Unsolicited response received on idle HTTP channel starting with "H"; err=<nil>
2013/05/23 03:01:42 INFO: &{GET http://www.reddit.com/r/Bitcoin/top.json HTTP/1.1 1 1 map[User-Agent:[This is a very creative name for a Reddit bot v1.0 by /u/username]] <nil> 0 [] false www.reddit.com map[] <nil> map[]   <nil>}
2013/05/23 03:01:42 INFO: This is a very creative name for a Reddit bot v1.0 by /u/username
2013/05/23 03:01:42 INFO: map[error:429], <nil>
INFO     2013-05-23 03:01:42,720 server.py:584] default: "GET / HTTP/1.1" 200 81

导致此错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

HTTP错误429是“请求太多”:http://tools.ietf.org/html/rfc6585#section-4

https://github.com/reddit/reddit/wiki/API,Reddit说“每分钟不超过30个请求。这可以让你的请求有些突然,但要保持理智。平均而言,我们每隔两秒就会看到不超过一个请求您。”他们还说,“每30秒不要多次击中同一页。”

如果您在每个用户点击应用程序时请求相同的资源,您可以使用GAE的memcache支持。如果您正在请求大量资源,Reddit wiki页面会提到“[r]一次请求多个资源”,因此可能存在一些用于批处理请求的工具(不确定,不熟悉API)。如果你正在做一个cronjob,你可以不那么频繁地运行它。

无论如何,剩下的谜团更多的是Reddit API问题,而不是Go问题。回复matthewbauer评论(https://github.com/jzelinskie/reddit)或Reddit API文档可能有更多。