Created
January 6, 2019 01:20
-
-
Save cleverdevil/d6290a023c28bd8f6aee4ef136a3ba98 to your computer and use it in GitHub Desktop.
Quick script to publish my Trakt history to my website
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
from dateutil.tz import UTC | |
from dateutil.parser import parse | |
from datetime import datetime | |
import conf | |
import json | |
import os | |
import feedparser | |
import requests | |
def make_id(seed): | |
return seed.replace('/', '-').replace(':', '-').replace(',', '-') | |
feed = feedparser.parse(conf.FEED_URL) | |
now = datetime.utcnow().astimezone(UTC) | |
for entry in feed['items']: | |
data = { | |
'title': entry['title'], | |
'summary': entry['summary'], | |
'link': entry['link'] | |
} | |
if len(data['summary']) == 0: | |
data['summary'] = entry['title'] | |
when = parse(entry['published']) | |
poster_url = entry['media_content'][0]['url'] | |
if 'Episode' in entry['id']: | |
data['type'] = 'tv' | |
elif 'Movie' in entry['id']: | |
data['type'] = 'movie' | |
media_id = make_id(entry['id']) | |
footprint = conf.HISTORY_PATH + '/' + media_id | |
if os.path.exists(footprint): | |
print('Skipping ->', media_id, '->', data['title']) | |
continue | |
print('Publishing ->', media_id, '->', data['title']) | |
poster_response = requests.get(poster_url) | |
response = requests.post('https://cleverdevil.io/watching/trakt/', data={ | |
'payload': json.dumps(data) | |
}, files={ | |
'photo': ( | |
poster_url.rsplit('/', 1)[1], | |
poster_response.content, | |
poster_response.headers['Content-Type'], | |
{'Expires': '0'} | |
) | |
}) | |
if response.status_code in (200, 201, 202): | |
open(footprint, 'w').write(json.dumps(data)) | |
print('Successfully published ->', media_id) | |
else: | |
print('Failed to publish ->', media_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment