代码之家  ›  专栏  ›  技术社区  ›  Arturo

Reddit的JSON API中允许哪些不同的排序类型?

  •  0
  • Arturo  · 技术社区  · 13 年前

    我遇到了一个函数,它使用API从reddit获取顶部图像。

    sr='pics'
    sorting=''
    url = r'http://www.reddit.com/r/{sr}/{top}.json'.format(sr=sr, top=sorting)
    

    然而,我发现早期的原始编码器发布了这样的内容:

    #'sorting' is whether or not the sorting of the reddit should be customized or not,
    # if it is: Allowed passing params/queries such as t=hour, week, month, year or all"""
    

    我不确定如何使用这些其他的“排序”命令,也不确定在哪里可以找到更多关于如何使用它们的信息。我甚至不确定是否应该将“排序”作为json/reddit/python表示法的一部分进行更多的研究。

    我想做的不仅是通过sorting=''定义的顶部图像,还通过另一个查询获得底部图像(如果可能的话,以其他方式排序)。

    2 回复  |  直到 13 年前
        1
  •  2
  •   Ric    13 年前

    如果你正常地去一个子版块,点击顶部,然后从“本周”链接,URL是

    http://www.reddit.com/r/pics/top/?sort=top&t=week

    如果我将top/更改为top.json

    http://www.reddit.com/r/pics/top.json?sort=top&t=week

    我得到了等效的JSON。 然而,本周URL中有争议的链接

    http://www.reddit.com/r/pics/controversial/?sort=controversial&t=week

    它变成

    http://www.reddit.com/r/pics/controversial.json?sort=controversial&t=week

    因此,从这次测试中似乎不可能找到底部。

    就像@Martijn评论的那样。这是一个reddit API问题,而不是Python或JSON问题。

        2
  •  0
  •   Glorfindel Doug L.    6 年前

    hot, new, rising, controversial, top, wiki

    要坚持使用您的url变量: url = 'http://www.reddit.com/r/{sr}/{top}.json' 您的方法需要分配上述数组中的一个参数,例如“rising”

    您应该设置一个默认值,这样方法调用在没有传递参数的情况下不会失败,例如:

    def get_data_from_reddit_api(sr="default subreddit value", top="hot")
      #url = ...
      #rest of reddit api request & subsequent parsing of the json feed
    end