forked from sxyazi/free-hls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
79 lines (60 loc) · 1.87 KB
/
utils.py
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
import os, re
import requests
import importlib
import subprocess
import shutil, hashlib
from os import getenv as _
from functools import wraps
from constants import VERSION
def api(method, url, **kwargs):
if method == 'POST':
fn = requests.post
else:
fn = requests.get
try:
r = fn('%s/%s' % (_('APIURL'), url), **kwargs, headers={
'API-Token': _('SECRET'),
'API-Version': VERSION}).json()
if not r['err']:
return r['data']
print('Request failed: %s' % r['message'])
except:
print('Request failed: connection error')
def md5(b):
return hashlib.md5(b).hexdigest()
def exec(cmd, timeout=None, **kwargs):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
communicate_kwargs = {}
if timeout is not None:
communicate_kwargs['timeout'] = timeout
out, err = p.communicate(**communicate_kwargs)
if p.returncode != 0:
raise Exception(cmd, out, err.decode('utf-8'))
return out
def execstr(*args, **kwargs):
return exec(*args, **kwargs).decode('utf-8').strip()
def tsfiles(m3u8):
return re.findall(r'^(?:enc\.)?out\d+\.ts$', m3u8, re.M)
def safename(file):
return '"' + file.replace('"', '\\"') + '"'
def sameparams(dir, command):
if not os.path.isdir(dir):
return False
try:
if open('%s/command.sh' % dir, 'r').read() != command:
shutil.rmtree(dir)
return False
except:
shutil.rmtree(dir)
return False
return True
def uploader():
return importlib.import_module('uploader.' + _('UPLOAD_DRIVE')).Uploader
def upload_wrapper(f):
@wraps(f)
def decorated(cls, file):
with open(file, 'rb+') as g:
return f(cls, g)
return decorated
session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11A465 QQLiveBrowser/7.0.8 WebKitCore/UIWebView'})