Skip to content

Instantly share code, notes, and snippets.

Created October 14, 2012 03:29
Show Gist options
  • Save knutwalker/3887143 to your computer and use it in GitHub Desktop.
Save knutwalker/3887143 to your computer and use it in GitHub Desktop.
Migrate movies from a public watchlist from IMDb into your private watchlist on trakt.
#!/usr/bin/env python
import csv
import collections
import re
import json
import requests
IMDB_URL = "{imdb_user}"
TRAKT_API = "{apikey}"
def stream_imdb(username='username'):
type_name = re.compile(r'[^a-zA-Z0-9_]')
first_letter = re.compile(r'[^a-zA-Z]')
def to_python_var(s):
header_name = type_name.sub('_', s).lower()
while first_letter.match(header_name[0]):
header_name = header_name[1:]
return header_name
url = IMDB_URL.format(imdb_user=username)
r = requests.get(url)
reader = csv.reader(r.iter_lines())
headers = [to_python_var(s) for s in next(reader)]
model = collections.namedtuple('IMDb', headers)
for line in reader:
yield model(*line)
def post_trakt(movies, username='username', password='password', apikey='apikey'):
def make_movie(model):
data = {
"imdb_id": model.const,
"title": model.title
if '?' not in model.year:
data['year'] = model.year
return data
payload = {
"movies" : [ make_movie(m) for m in movies ]
url = TRAKT_API.format(apikey=apikey)
r =, data=json.dumps(payload), auth=(username, password))
return r
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--imdb-user', required=True)
parser.add_argument('-u', '--trakt-user', required=True)
parser.add_argument('-p', '--trakt-pass', required=True)
parser.add_argument('-a', '--trakt-apikey', required=True)
arguments = parser.parse_args()
print post_trakt(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment