aboutsummaryrefslogtreecommitdiff
path: root/tools/list-missing-unwatched.py
blob: 431054ac02aee89e665191ff68c145a1e0f8527d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3

import os
import subprocess
import sys

def show_notification(title, description, urgency):
    print("Notification: title: %s, description: %s" % (title, description))
    process = subprocess.Popen(["notify-send", "-a", "automedia", "-t", "5000", "-u", urgency, "--", title, description])
    stdout, stderr = process.communicate()
    return process.returncode == 0

def get_downloaded():
    process = subprocess.Popen(["automedia", "downloaded"], stdout=subprocess.PIPE)
    stdout, stderr = process.communicate()
    if process.returncode != 0:
        return None
    return stdout.decode("utf-8").splitlines(False)

def get_seen():
    try:
        with open(os.path.expanduser("~/.config/automedia/seen")) as file:
            return file.read().splitlines()
    except IOError as err:
        return []

def main():
    if len(sys.argv) != 2:
        print("usage: list-missing-unwatched.py <automedia-download-dir>")
        sys.exit(1)

    automedia_dir = sys.argv[1]
    if not os.path.exists(automedia_dir):
        show_notification("list-missing-unwatched", "No such file or directory: " % automedia_dir, "critical")
        sys.exit(2)

    downloaded = get_downloaded()
    if not downloaded:
        show_notification("list-missing-unwatched", "Failed to get downloaded episodes/chapters", "critical")
        sys.exit(2)

    downloaded = dict.fromkeys(downloaded)
    seen = dict.fromkeys(get_seen())
    unwatched = [t for t in downloaded if t not in seen]
    missing = [u for u in unwatched if not os.path.exists(os.path.join(automedia_dir, u))]
    for u in missing:
        print(u)

if __name__ == "__main__":
    main()