Created
November 21, 2016 21:09
-
-
Save sheriferson/f9137682f6d1b49a94c7a975a6c10f34 to your computer and use it in GitHub Desktop.
This file contains 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
library(httr) | |
library(magrittr) | |
library(jsonlite) | |
library(purrr) | |
library(stringr) | |
# My API token is saved in an environment file | |
pinsecret <- Sys.getenv('pin_token') | |
# GET all my links in JSON | |
pins_all <- GET('https://api.pinboard.in/v1/posts/all', | |
query = list(auth_token = pinsecret, | |
format = 'json')) | |
pins <- pins_all %>% content() %>% fromJSON() | |
pins %>% | |
select(href, time) %>% | |
head() %>% | |
knitr::kable() | |
get_hostname <- function(href) { | |
tryCatch({ | |
parsed_url <- parse_url(href) | |
if (!parsed_url$hostname %>% is.null()) { | |
hostname <- parsed_url$hostname %>% gsub('^www.', '', ., perl = T) | |
return(hostname) | |
} else { | |
return('unresolved') | |
} | |
}, error = function(e) { | |
return('unresolved') | |
}) | |
} | |
pins$hostname <- map_chr(pins$href, .f = get_hostname) | |
## let's select medium posts | |
# this is the first attempted method that is not so safe | |
# medium <- pins %>% | |
# filter(str_detect(href, 'medium.com')) | |
# this is a better filtering for Medium posts that's more safe | |
medium <- pins %>% | |
filter(hostname == 'medium.com') | |
# medium %>% | |
# select(href, time) %>% | |
# head(n = 7) %>% | |
# knitr::kable() | |
medium$cleanhref <- sub("#\\..{9}$", "", medium$href) | |
medium %>% | |
select(href, cleanhref) %>% | |
head() %>% | |
knitr::kable() | |
# sanitize links and update on pinboard | |
medium$addition_response <- vector(length = nrow(medium)) | |
medium$deletion_response <- vector(length = nrow(medium)) | |
for (ii in 1:nrow(medium)) { | |
deletion <- GET('https://api.pinboard.in/v1/posts/delete', | |
query = list(auth_token = pinsecret, | |
url = medium$href[ii])) | |
medium$deletion_response[ii] <- deletion$status_code | |
addition <- GET('https://api.pinboard.in/v1/posts/add', | |
query = list(auth_token = pinsecret, | |
url = medium$cleanhref[ii], | |
description = medium$description[ii], | |
extended = medium$extended[ii], | |
tags = medium$tags[ii], | |
dt = medium$time[ii], | |
shared = medium$shared[ii], | |
toread = medium$toread[ii])) | |
medium$addition_response[ii] <- addition$status_code | |
Sys.sleep(5) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment