aboutsummaryrefslogtreecommitdiff
path: root/tools/remove-watched.py
blob: a2a5b00dbc617f0c726e28d8cfad4636e379a92f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3

import json
import os
import sys

def file_get_content_json(filepath):
    with open(filepath) as f:
        return json.load(f)

def get_watch_progress(time, duration):
    if duration == 0:
        return 0
    return time / duration

def get_quickmedia_watched_episodes(downloads_dir):
    watched_episodes = []
    local_anime_watch_progress_json = file_get_content_json(os.path.expanduser("~/.config/quickmedia/watch-progress/local-anime"))
    for filename, anime_item in local_anime_watch_progress_json.items():
        episode_filepath = os.path.join(downloads_dir, filename)
        time = int(anime_item["time"])
        duration = int(anime_item["duration"])
        watch_progress = get_watch_progress(time, duration)
        if watch_progress >= 0.9:
            watched_episodes.append(episode_filepath)
    return watched_episodes

def main(args):
    if len(args) != 2:
        print("usage: remove-watched.py <downloads-dir>")
        print("")
        print("This script removes anime episodes that you have finished watching with QuickMedia")
        exit(1)

    downloads_dir = args[1]
    watched_episodes = get_quickmedia_watched_episodes(downloads_dir)

    print("Files to remove:")
    removable_files = []
    for watched_episode in watched_episodes:
        if os.path.exists(watched_episode):
            print("  %s" % watched_episode)
            removable_files.append(watched_episode)

    while True:
        response = input("Are you sure you want to remove the above %d episode(s)? (y/n): " % len(removable_files))
        if len(response) < 1:
            continue

        if response[0] == 'Y' or response[0] == 'y':
            for filepath in removable_files:
                try:
                    os.remove(filepath)
                    print("Removed %s" % filepath)
                except OSError:
                    pass
            return

        if response[0] == 'N' or response[0] == 'n':
            print("Cancelled deletion of episodes")
            return

main(sys.argv)