IMDB watchlist to todo.txt

I'm a big fan of todo.txt. It's a super simple format for tasklists, basically one line per task, start with x to mark a task as done, use (A), (B), (C)... to give a priority, use @Something to add tags, due:2021-10-18 to indicate a due date, etc. Everything is saved into one simple text file (named todo.txt, as you might have guessed).

I have it show the most urgent task on my desktop's task bar, synchronize with my phone through nextcloud, integrate in my calendar (using simpletask). The possibilities are endless. I also use it to keep lists of movies I want to watch. But I also use IMDB for that, so here is a little script that fetches my IMDB watchlist and updates the entries in my todo list with it. Enjoy!

#!/usr/bin/python

from urllib import request
import re
import json

# fetch the watchlist
r = request.urlopen("https://www.imdb.com/user/ur118635647/watchlist")
b = r.read().decode("utf8")

# fortunately for us, everything is stored as a big json string there!!
j = re.findall("IMDbReactInitialState\.push\((.*?)\)\;", b)[0]
d = json.loads(j)
t = d["titles"]

# build a list of movies
movies = {}
for k, v in t.items():
    m = v["primary"]
    tt = m["title"]
    tr = str(v["ratings"]["rating"])
    tu = "https://www.imdb.com"+m["href"]
    ty = v["type"]
    if ty == "featureFilm":
        ty = "movie"
    tp = v["plot"]
    # build the todo.txt entry
    s = " - ".join([tt, ty, tr, tu, tp]) + " @Watchlist"
    # print(s)
    movies[tt] = s

# read existing todo.txt file
todos = []
with open("/home/yorik/.todo/todo.txt") as f:
    for rl in f:
        todos.append(rl.strip())

# check if movie is there already
for i in range(len(todos)):
    todo = todos[i]
    if "@Watchlist" in todo:
        found = None
        for key in movies.keys():
            if todo.lower().startswith(key.lower()):
                if todo != movies[key]:
                    todos[i] = movies[key]
                found = key
                break
        if found:
            del movies[found]
            found = None

# add all the remaining movies
for k in movies.keys():
    todos.append(movies[k])

# write the file
with open("/home/yorik/.todo/todo.txt", "w") as f:
    f.write("\n".join(todos))

Comment on this post on Twitter