This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
kget-integrator
executable file
·112 lines (85 loc) · 3.63 KB
/
kget-integrator
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# KGet integrator based on the uGet integrator
# This more an adaptation. The code was cleaned to remove features not supported by KGet
# Based on https://github.com/ugetdm/uget-integrator/blob/master/bin/uget-integrator
# Author: Nicolas Guilloux
# Email: [email protected]
# Website: https://nicolasguilloux.eu
import os
import struct
import sys
import threading
import logging
import json
import subprocess
from os.path import join, expanduser
KGET_COMMAND = 'kget'
KGET_DIRECT_DOWNLOAD = ['qdbus', 'org.kde.kget', '/KGet', 'org.kde.kget.main.addTransfer', 'URL', 'DOWNLOAD_FOLDER', 'true']
VERSION = "1.1.0"
creation_flags = 0
logger = logging.getLogger()
# log_file_path = join(expanduser('~'), 'kget-integrator.log')
# logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', filename=log_file_path, filemode='a', level=logging.DEBUG)
logger.propagate = False
def send_message(message):
"""
Send a message to the webapp.
"""
logger.info('Sending message: ' + str(message))
try:
# Write message size.
sys.stdout.buffer.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
except Exception as e:
logger.error('Error in sending message: ' + str(e))
def read_message():
"""
Read messages from the webapp.
"""
logger.info('kget-integrator is reading the message')
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.buffer.read(4)
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
logger.debug('Message length: ' + str(text_length))
# Read the text (JSON object) of the message.
text = sys.stdin.buffer.read(text_length).decode('utf-8')
logger.debug('Received message: ' + str(text))
if text:
# Return information about the config
if not 'URL' in text:
kget_version = subprocess.Popen(
[KGET_COMMAND + " --version"], shell=True, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0]
kget_version = kget_version.replace('kget', '').strip()
logger.debug('KGet version: ' + kget_version)
send_message('{"Status": "Available", "Version": "' +
VERSION + '", "KGet": "' + kget_version + '"}')
return
send_message('{"Status": "Available", "Version": "' + VERSION + '", "KGet": ""}')
data = json.loads(text)
url = data['URL']
direct = False
if 'direct' in data:
direct = data['direct']
logger.debug('Direct Download: ' + str(direct))
if url != "":
# Execute the command
if (direct):
download_folder = subprocess.Popen(['xdg-user-dir DOWNLOAD'], shell=True, stdout=subprocess.PIPE, universal_newlines=True).communicate()[0].strip()
logger.debug('Download folder: ' + download_folder)
command = KGET_DIRECT_DOWNLOAD
command[4] = url
command[5] = download_folder + '/'
else:
command = [KGET_COMMAND]
command.append(url)
# Debug
logger.debug('Send command: ' + str(command))
# Pass the parameters to KGet
subprocess.Popen(command, creationflags=creation_flags).wait()
sys.exit(0)
if __name__ == '__main__':
read_message()