Created
August 8, 2014 14:39
-
-
Save elephantum/e606715054078a008400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ideas <- function(x) { | |
Sys.sleep(1) | |
re_coordinates = 'shadow%3Afalse%7C(\\d+\\.\\d+)%2C(\\d+\\.\\d+)' | |
url <- paste('http://moscowidea.ru/ideas/', x, sep="") | |
read_data <- function(){ | |
thepage = readLines(url) | |
coord_page <- thepage[grep(re_coordinates, thepage)] | |
mypattern_in = 'part_idea__summary__suggestion' | |
mypattern_out = 'part_idea__summary__purpose' | |
sugg_start = grep(mypattern_in, thepage) + 2 | |
sugg_finish = grep(mypattern_out, thepage) - 1 | |
suggestion = thepage[c(sugg_start:sugg_finish)] | |
idea_sugg <- do.call('paste', as.list(suggestion)) | |
return(list(idea=idea_sugg, code=200, data=coord_page, url=url)) | |
} | |
data <- tryCatch(read_data(), error=function(e){return(list(idea=NA, url=url, code=404, data=c()))}) | |
if(data[['code']] == 200){ | |
if(length(data[['data']]) > 0) { | |
m <- regmatches(data[['data']], regexec(re_coordinates, data[['data']]))[[1]] | |
return(data.frame(idea=data[['idea']], url=data[['url']], code=200, has_coordinates=TRUE, x=m[2], y=m[3])) | |
} | |
else { | |
return(data.frame(idea=data[['idea']], url=data[['url']], code=200, has_coordinates=FALSE, x=NA, y=NA)) | |
} | |
} | |
return(data.frame(idea=NA, url=data[['url']], code=data[['code']], has_coordinates=FALSE, x=NA, y=NA)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment