-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_getData_dev.Rmd
172 lines (135 loc) · 4.65 KB
/
01_getData_dev.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: "flickr map Switzerland"
author: "Duc-Quang Nguyen"
date: "14 May 2016"
output: html_document
---
## FlickR
* [timelyportfolio R and flickr](http://timelyportfolio.github.io/rCharts_Rflickr/iso_httr.html)
* [flickr color analysis](http://beautifuldata.net/2013/05/color-analysis-of-flickr-images/)
* [](https://github.com/furukama/flickr/blob/master/flickr_get.R)
* [my API page](https://www.flickr.com/services/apps/by/112725067@N03)
* [official doc API](https://www.flickr.com/services/api/)
* [analyze-instagram-r](http://thinktostart.com/analyze-instagram-r/)
# https://www.instagram.com/developer/endpoints/locations/
```{r setup, include=FALSE}
require(httr)
library(RCurl)
library(magrittr)
library(dplyr)
library(jsonlite)
# api_key <- 'ab48e232418f51f62fd633de5c6e78ef'
# secret <- '67eb98ae14392afa'
#
# flickr.app <- oauth_app("r to flickr",api_key,secret)
# flickr.endpoint <- oauth_endpoint(
# request = "https://www.flickr.com/services/oauth/request_token"
# , authorize = "https://www.flickr.com/services/oauth/authorize"
# , access = "https://www.flickr.com/services/oauth/access_token"
# )
#
# tok <- oauth1.0_token(
# flickr.endpoint
# , flickr.app
# , cache = F
# )
#
# save(api_key, secret, flickr.app, flickr.endpoint, tok, file = "~/swissinfo/_helpers/secrets.Rdata")
load("~/swissinfo/_helpers/secrets.Rdata")
## my request
# https://www.flickr.com/services/api/explore/flickr.photos.search
content_type <- 1
# To define bbox, use http://bboxfinder.com/#45.859412,5.844727,47.524620,10.755615
bbox <- paste(c(5.899658,45.836454,10.541382,47.735629), collapse = ",")
flickrSearch <- function(
bbox,
content_type = 1,
min_taken_date = format( Sys.Date() - 7, "%Y-%m-%d"),
max_taken_date = format( Sys.Date(), "%Y-%m-%d"),
api_key = api_key,
tok = tok
) {
search <- GET(url=sprintf(
"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%s&bbox=%s&content_type=%s&min_taken_date=%s&max_taken_date=%s&format=json&nojsoncallback=1"
, api_key
, bbox
, content_type
, min_taken_date
, max_taken_date
, tok$credentials$oauth_token
)
) %>%
content( as = "text" ) %>>%
jsonlite::fromJSON ()
stopifnot(search[[2]] == "ok")
# subset the list to get only the relevant data
search %$% photos %$% photo
}
time.frames <- data.frame(
min_taken_date = seq(as.Date("2014-01-01"), Sys.Date(), "week")
)
time.frames$max_taken_date = c(time.frames[-1, ] -1, Sys.Date() + 1)
pics <- do.call(rbind, lapply(1:nrow(time.frames), function(i) {
cat("\n", i)
flickrSearch(
bbox = bbox,
min_taken_date = time.frames[i,'min_taken_date'],
max_taken_date = time.frames[i,'max_taken_date'],
api_key = api_key,
tok = tok)
}))
stopifnot(!any(duplicated(pics)))
# get the lon lat of each pics
flickr.photos.geo.getLocation
getGeo <- function(id, api_key = api_key, tok = tok) {
call <- GET(url=sprintf(
"https://api.flickr.com/services/rest/?method=flickr.photos.geo.getLocation&api_key=%s&photo_id=%s&format=json&nojsoncallback=1"
, api_key
, id
, tok$credentials$oauth_token
)) %>%
content( as = "text" ) %>>%
jsonlite::fromJSON ()
stopifnot(call[[2]] == "ok")
result <- call %$% photo
data.frame(
id = as.numeric(result$id),
lat = as.numeric(result$location$latitude),
lon = as.numeric(result$location$longitude),
locality = if(is.null(result$location$locality$`_content`)) "" else result$location$locality$`_content`,
county = if(is.null(result$location$county$`_content`)) "" else result$location$county$`_content`,
region = if(is.null(result$location$region$`_content`)) "" else result$location$region$`_content`,
country = if(is.null(result$location$country$`_content`)) "" else result$location$country$`_content`
)
}
geos <- do.call(rbind, lapply(pics$id, function(id) {
cat("\n", id)
getGeo(id = id, api_key = api_key, tok = tok)
}))
search <- GET(url=sprintf(
"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%s&bbox=%s&content_type=%s&format=json&nojsoncallback=1"
, api_key
, bbox
, content_type
, tok$credentials$oauth_token
)
) %>%
content( as = "text" ) %>>%
jsonlite::fromJSON ()
stopifnot(test[[2]] == "ok")
# subset the list to get only the relevant data
chpics <- search %$% photos %$% photo
%>>%
( .$photos$photo ) %>>%
( data.frame(
date = format( Sys.Date() - i, "%Y-%m-%d")
,.
,stringsAsFactors=F
)) %>>%
return
}
) %>>%
# combine all the days into a data frame
( do.call(rbind, .) )
flickr.photos.geo.getLocation
```