aboutsummaryrefslogtreecommitdiff
path: root/open_media.py
blob: 2d333a145bff85efb95230e1c7eaa59a169d8cc2 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python

import os
import sys
import subprocess

script_dir = os.path.dirname(os.path.realpath(sys.argv[0]))

def run_dmenu(input):
    process = subprocess.Popen(["dmenu", "-l", "15", "-i", "-p", "Select media"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    stdout, stderr = process.communicate(input.encode())
    if process.returncode == 0:
        return stdout
    else:
        print("Failed to launch dmenu, error: {}".format(stderr))
    return None

def add_seen(seen_filepath, media_name, seen_list):
    if media_name in seen_list:
        return
    with open(seen_filepath, "a") as seen_file:
        seen_file.write(media_name + "\n")

def sort_images(filename):
    idx = filename.find(".")
    if idx != -1:
        return int(filename[0:idx])
    return 0

def get_downloaded_list():
    process = subprocess.Popen(["automedia", "downloaded"], stdout=subprocess.PIPE)
    stdout, stderr = process.communicate()
    if process.returncode == 0:
        return stdout.decode().splitlines()
    else:
        print("Failed to list downloaded items, error: {}".format(stderr))
    return []

def main():
    if len(sys.argv) < 2:
        print("usage: open_media.py <download_dir>")
        print("example: open_media.sh /home/user/Downloads/automedia")
        exit(1)

    download_dir = sys.argv[1]
    if not os.path.isdir(download_dir):
        print("No such directory: " % (download_dir))
        exit(2)

    downloaded_list = get_downloaded_list()
    filtered_downloaded_list = []
    for item in downloaded_list:
        media_path = os.path.join(download_dir, item)
        if os.path.exists(media_path) and not os.path.exists(os.path.join(media_path, ".in_progress")) and (os.path.isfile(media_path) or (os.path.isdir(media_path) and os.path.exists(os.path.join(media_path, ".finished")))):
            filtered_downloaded_list.append(item)
    downloaded_list = filtered_downloaded_list
    
    seen_filepath = os.path.expanduser("~/.config/automedia/seen")
    seen_list = []
    try:
        with open(seen_filepath, "r") as seen_file:
            seen_list = seen_file.read().splitlines()
    except OSError as e:
        print("Failed to open {}, reason: {}".format(seen_filepath, str(e)))

    for seen in seen_list:
        for i, downloaded in enumerate(downloaded_list):
            if seen == downloaded:
                downloaded_list[i] = "✓ {}".format(downloaded)

    selected_media = run_dmenu("\n".join(downloaded_list[::-1]))
    if not selected_media:
        exit(0)
    selected_media = selected_media.decode().replace("✓ ", "").rstrip()
    
    media_path = os.path.join(download_dir, selected_media)
    if os.path.isdir(media_path):
        add_seen(seen_filepath, selected_media, seen_list)
        files = []
        for filename in os.listdir(media_path):
            if filename not in (".finished", ".session_id"):
                files.append(filename)

        files = sorted(files, key=sort_images)
        process = subprocess.Popen(["sxiv", "-i", "-f"], stdin=subprocess.PIPE)
        files_fullpath = []
        for filename in files:
            files_fullpath.append(os.path.join(media_path, filename))
        process.communicate("\n".join(files_fullpath).encode())
    elif os.path.isfile(media_path):
        add_seen(seen_filepath, selected_media, seen_list)
        subprocess.Popen(["mpv", "--", media_path])

main()