How to reduce query time for stackexchange api?

a 夏天 提交于 2019-12-11 04:16:29

问题


I try to collect for a list of users the questions.

So I prepared this command lines:

library(stackr)
dft <- data.frame()
for (j in 1:nrow(df)) {
     questions <- stack_users(df$userid[j], "questions", num_pages=1000000, pagesize=100, filter="withbody")                
    for (s in 1:nrow(questions)){
      dft <- rbind(dft, data.frame(
        tags               = ifelse(is.null(questions$tags[s])               , NA, questions$tags[s]),
        is_answered        = ifelse(is.null(questions$is_answered[s])        , NA, questions$is_answered[s]),
        view_count         = ifelse(is.null(questions$view_count[s])         , NA, questions$view_count[s]),
        accepted_answer_id = ifelse(is.null(questions$accepted_answer_id[s]) , NA, questions$accepted_answer_id[s]),
        answer_count       = ifelse(is.null(questions$answer_count[s])       , NA, questions$answer_count[s]),
        score              = ifelse(is.null(questions$score[s])              , NA, questions$score[s]),
        last_activity_date = ifelse(is.null(questions$last_activity_date[s]) , NA, questions$last_activity_date[s]),
        creation_date      = ifelse(is.null(questions$creation_date[s])      , NA, questions$creation_date[s]),
        last_edit_date     = ifelse(is.null(questions$last_edit_date[s])     , NA, questions$last_edit_date[s]),
        question_id        = ifelse(is.null(questions$question_id[s])        , NA, questions$question_id[s]),
        link               = ifelse(is.null(questions$link[s])               , NA, questions$link[s]),
        title              = ifelse(is.null(questions$title[s])              , NA, questions$title[s]),
        body               = ifelse(is.null(questions$body[s])               , NA, questions$body[s]),
        owner_reputation   = ifelse(is.null(questions$owner_reputation[s])   , NA, questions$owner_reputation[s]),
        owner_user_id      = ifelse(is.null(questions$owner_user_id[s])      , NA, questions$owner_user_id[s]),
        owner_user_type    = ifelse(is.null(questions$owner_user_type[s])    , NA, questions$owner_user_type[s]),
        owner_accept_rate  = ifelse(is.null(questions$owner_accept_rate[s])  , NA, questions$owner_accept_rate[s]),
        owner_link         = ifelse(is.null(questions$owner_link[s])         , NA, questions$owner_link[s])
      ))

    }       

}

However it takes to much time to collect a list of different user ids. Is there any way to reduce the execution time or update to code I could make?


回答1:


Partial answer since I'm not fluent in r:

Are you trying to get the list of questions for a given set of users?

If so,

for (j in 1:nrow(df)) {
    questions <- stack_users(df$userid[j]... 

is a poor way to do it.

Refer to the API's /users/{ids}/questions doc:

(The) {ids} (parameter) can contain up to 100 semicolon delimited ids. To find ids programmatically look for user_id on user or shallow_user objects.

(Emphasis added)

So instead of something that evaluates to stack_users(1,... (one id)

Group the ids in batches of 100 for that function. Something like:

stack_users(c(1,2,3,4,5,...),...

(But remember I'm not an r coder.)



来源:https://stackoverflow.com/questions/52395556/how-to-reduce-query-time-for-stackexchange-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!