~tardypad/wee-most

ref: 1584fc7984f277c882ef269204424445bf3790b2 wee-most/wee_most/file.py -rw-r--r-- 2.0 KiB
1584fc79 — Damien Tardy-Panis Move write_file_lines function to post file 5 months ago
                                                                                
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
import weechat
import wee_most
import os, platform
from wee_most.globals import config

class File:
    def __init__(self, server, **kwargs):
        self.id = kwargs["id"]
        self.name = kwargs["name"]
        self.url = wee_most.http.build_file_url(kwargs["id"], server)

def prepare_download_location():
    location = config.get_download_location()

    if not os.path.exists(location):
        try:
            os.makedirs(location)
        except:
            weechat.prnt('', 'ERROR: Failed to create directory at files_download_location: {}'
                    .format(location))

    return location

def open_file(file_path):
    if platform.system() == 'Darwin':       # macOS
        weechat.hook_process("open \"{}\"".format(file_path), 100, "", "")
    elif platform.system() == 'Windows':    # Windows
        os.startfile(file_path)
        weechat.hook_process(file_path, 100, "", "")
    else:                                   # linux variants
        weechat.hook_process("xdg-open \"{}\"".format(file_path), 100, "", "")

def file_get_cb(file_path, command, rc, out, err):
    if rc != 0:
        weechat.prnt("", "An error occurred while downloading file")
        return weechat.WEECHAT_RC_ERROR

    open_file(file_path)

    return weechat.WEECHAT_RC_OK

def handle_file_click(data, info):
    tags = info["_chat_line_tags"].split(",")

    file_id = find_file_id_in_tags(tags)

    server = wee_most.server.get_server_from_buffer(info["_buffer"])

    file_path = prepare_download_location() + "/" + file_id

    if os.path.isfile(file_path):
        open_file(file_path)
    else:
        wee_most.http.run_get_file(file_id, file_path, server, "file_get_cb", file_path)

def find_file_id_in_tags(tags):
    for tag in tags:
        if tag.startswith("file_id_"):
            return tag[8:]

def get_files_from_post_data(post_data, server):
    if "files" in post_data["metadata"]:
        files = []
        for file_data in post_data["metadata"]["files"]:
            files.append(File(server, **file_data))
        return files

    return []