Skip to content

Instantly share code, notes, and snippets.

@Macuyiko
Created October 31, 2016 09:08
Show Gist options
  • Save Macuyiko/a690aaeb16c7acc5caa526a6632db86e to your computer and use it in GitHub Desktop.
Save Macuyiko/a690aaeb16c7acc5caa526a6632db86e to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
from requests_oauthlib import OAuth1
from urllib.parse import parse_qs
from datetime import datetime
consumer_key = '----- obtain this from tumblr -----'
secret_key = '----- obtain this from tumblr -----'
# Replace with your Blog / Delicious profile:
blog_identifier = 'tumblr.macuyiko.com'
delicious_profile = 'https://del.icio.us/macuyiko'
request_token = 'https://www.tumblr.com/oauth/request_token'
authorize = 'https://www.tumblr.com/oauth/authorize'
access_token = 'https://www.tumblr.com/oauth/access_token'
def read_oauth():
try:
rl = open('oauth.txt', 'r').readlines()
return {'resource_owner_key':rl[0].strip(), 'resource_owner_secret':rl[1].strip()}
except FileNotFoundError:
return None
def obtain_oauth():
oauth = OAuth1(consumer_key, client_secret=secret_key)
r = requests.post(url=request_token, auth=oauth)
credentials = parse_qs(r.text)
resource_owner_key = credentials.get('oauth_token')[0]
resource_owner_secret = credentials.get('oauth_token_secret')[0]
authorize_url = authorize + '?oauth_token=' + resource_owner_key
print ('Please go here and authorize:', authorize_url)
verifier = input('Please input the verifier: ')
oauth = OAuth1(consumer_key, client_secret=secret_key,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier)
r = requests.post(url=access_token, auth=oauth)
credentials = parse_qs(r.text)
resource_owner_key = credentials.get('oauth_token')[0]
resource_owner_secret = credentials.get('oauth_token_secret')[0]
open('oauth.txt', 'w').write(resource_owner_key+'\n'+resource_owner_secret+'\n')
return read_oauth()
def post_link(oauth, date, title, url):
api_url = 'https://api.tumblr.com/v2/blog/{}/post'.format(blog_identifier)
r = requests.post(url=api_url, auth=oauth, data={'type':'link', 'date':date, 'title':title, 'url': url})
print(r.text)
return r.text
def read_links():
try:
rl = open('links.txt', 'r', encoding="utf-8").readlines()
return rl
except FileNotFoundError:
return None
def obtain_links():
links = []
soup = BeautifulSoup(requests.get(delicious_profile).text)
pages = int(soup.select('ul.pagination li')[-2].text)
for page in range(1, pages + 1):
print('\n\nNow doing page',page)
soup = BeautifulSoup(requests.get(delicious_profile + '?&page=' + str(page)).text)
for article in soup.select('div.articleThumbBlockOuter'):
title = article.select('div.articleTitlePan a')[-1].text
href = article.select('div.articleInfoPan > p')[0].a['href']
date = datetime.strptime(article.select('div.articleInfoPan > p')[-1].text.split(' on ')[1].strip(), '%B %d, %Y')
print(' - ', date.date(), href)
links.append( (title, href, str(date.date())) )
open('links.txt', 'w', encoding="utf-8").write('\n'.join(['\t'.join(x) for x in links]))
return read_links()
if __name__ == '__main__':
read_oauth = read_oauth()
if not read_oauth:
read_oauth = obtain_oauth()
print(read_oauth)
oauth = OAuth1(consumer_key, client_secret=secret_key, **read_oauth)
links = read_links()
if not links:
links = obtain_links()
try:
for l in range(len(links)):
link = links[l].strip().split('\t')
if len(link) != 3:
continue
print(' - ', link[1])
r = post_link(oauth, link[2], link[0], link[1])
if '"status":201' in r:
links[l] = links[l].strip() + '\t---DONE---\n'
except:
pass
open('links.txt', 'w', encoding="utf-8").write(''.join(links))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment