在reddit URL中输入5个字符(数字)ID

时间:2012-07-18 00:16:11

标签: r reddit

在reddit URL中,有由base36生成的“5 characternumerics”thing_id部分(例如,来自“http://redd.it/wplf7”的“wplf7”)。

wplf7是从54941875生成的 - 这是我到目前为止发现的...我想知道54941875是如何生成的。

我正在尝试使用R来抓取reddit特定部分的评论(让我们说http://www.reddit.com/r/leagueoflegends/)并且我坚持使用这5个字符的数字。

任何能够以简单的方式解释这一点的人?不幸的是,Python不是我的域名,Reddit网站上列出的2000行python代码对我没什么帮助。

谢谢,

2 个答案:

答案 0 :(得分:7)

首先设置一个唯一的用户代理,因为reddit喜欢这个

options(HTTPUserAgent="My name is BOB")

我很想你想要获得http://www.reddit.com/r/leagueoflegends/的内容。您需要将.json附加到网址:

library(RJSONIO)
library(RCurl)
# library(XML)

jdata<-getURL('http://www.reddit.com/r/leagueoflegends/.json')
jdata<-fromJSON(jdata)
# xdata<-getURL('http://www.reddit.com/r/leagueoflegends/.xml')
# xdata<-xmlParse(xdata)

显然内容非常丰富,例如域名,永久链接,作者,帖子标题:

Domains<-sapply(jdata[[2]]$children,function(x){x$data$domain})
permalinks<-sapply(jdata[[2]]$children,function(x){x$data$permalink})
authors<-sapply(jdata[[2]]$children,function(x){x$data$author})
titles<-sapply(jdata[[2]]$children,function(x){x$data$title})
ids<-sapply(jdata[[2]]$children,function(x){x$data$id})
created<-as.POSIXct(sapply(jdata[[2]]$children,function(x){x$data$created}),origin="1970/01/01")


> head(titles)
[1] "Pendragon 3-day-banning someone for randoming in ranked, or saying hes going to. Mixed feelings..."
[2] "Dig Kicks L0cust."                                                                                 
[3] "Summoners, y u no communicate??"                                                                   
[4] "Without Even Trying"                                                                               
[5] "Cross Country Tryndamere (Chaox Stream)"                                                           
[6] "Top 5 Flops - Episode 4 ft Dyrus, Phantoml0rd, and HatPerson vs Baron Nashor"                      
> 

为了研究如何生成这些ID,我们可以将@Ben Bolker的base36ToInteger函数应用于我们收集的ID,并将它们与创建它们的日期进行比较:

createData<-data.frame(created=created,ids=sapply(ids,base36ToInteger))
> dput(createData)
structure(list(created = structure(c(1342658844, 1342657298, 
1342622962, 1342643655, 1342641187, 1342654768, 1342665353, 1342640599, 
1342648272, 1342662822, 1342654185, 1342659591, 1342624350, 1342647907, 
1342637587, 1342591960, 1342625515, 1342642330, 1342651384, 1342668363, 
1342608976, 1342608165, 1342632545, 1342638611, 1342643489), class = c("POSIXct", 
"POSIXt")), ids = c(55047001, 55044612, 55010018, 55025557, 55022809, 
55040754, 55056689, 55022221, 55031424, 55053023, 55039810, 55048123, 
55010880, 55030934, 55019343, 54976515, 55011555, 55024060, 55035670, 
55061120, 54998192, 54997264, 55015528, 55020295, 55025363)), .Names = c("created", 
"ids"), row.names = c("wrujd", "wrsp0", "wr202", "wrdzp", "wrbvd", 
"wrppu", "ws20h", "wrbf1", "wriio", "wrz6n", "wrozm", "wrvej", 
"wr2o0", "wri52", "wr973", "wqc5f", "wr36r", "wrcu4", "wrlsm", 
"ws5fk", "wqsvk", "wqs5s", "wr694", "wr9xj", "wrdub"), class = "data.frame")

enter image description here

这意味着reddit会在创建新帖子时在网站上按顺序生成这些数字。

如果没有具体的指示,我会将其留在此处,但希望你能得到这个想法。

答案 1 :(得分:5)

我从通用基本转换posted by Erich Neuwirth on r-help in 2008的代码开始:这是递归的,所以可能很慢 - 但我花了相应的时间来开发它!

numberInBase <- function(number,base){
    numberInBaseRecur<-function(number,base){
        lastDigit<-function(number,base) number %% base
        if (number == 0) result <- c(0)
        else result <- c(numberInBaseRecur(number %/% base,base),
                         lastDigit(number,base))
        result
    }
    result <- numberInBaseRecur(number,base)
    while (result[1]== 0 && length(result)>1)
        result <- result[-1]
    result
} 

快速测试:

numberInBase(36^3,36)
## [1] 1 0 0 0

现在我们只需要将十进制转换为基数36,然后索引相应的字母数字字符串。这是你的榜样:

b36string <- c(0:9,letters)
paste(b36string[numberInBase(54941875,36)+1],collapse="")
## [1] "wplf7"

如果你需要走另一条路,那就有一个 post by Jim Holtman from Jan 2012 that gives a solution

base36ToInteger <- function (Str) {
    common <- chartr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                     , ":;<=>?@ABCDEFGHIJKLMNOPQRS:;<=>?@ABCDEFGHIJKLMNOPQRS"
                     , Str)
    x <- as.numeric(charToRaw(common)) - 48
    sum(x * 36 ^ rev(seq(length(x)) - 1))
} 

base36ToInteger("wplf7")

(我没有停下来弄清楚这实际上是如何运作的,但你可以阅读帖子......)