1

我正在尝试在 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

此错误的原因可能是什么?

4

1 回答 1

1

HTTP 错误 429 是“请求过多”:https ://www.rfc-editor.org/rfc/rfc6585#section-4

https://github.com/reddit/reddit/wiki/API上,Reddit 说“每分钟发出不超过 30 个请求。这允许您的请求有一些突发性,但要保持理智。平均而言,我们应该不会看到更多比你每两秒发出一个请求。” 他们还说,“不要每 30 秒多次点击同一页面。”

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

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

于 2013-11-03T22:37:27.867 回答