/
downloadboob_tools_generic.py
216 lines (189 loc) · 6.13 KB
/
downloadboob_tools_generic.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# !/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Tools used for downloadboob
We will put here, simple functions that are not relating to weboob or kodi
"""
from __future__ import print_function
from __future__ import unicode_literals
__author__ = 'Alexandre Poux'
import re
from subprocess import Popen, CalledProcessError, PIPE
import logging
import os
env_utf8 = {'PYTHONIOENCODING': 'utf-8'}
rx = re.compile('[ \\/\\?\\:\\>\\<\\!\\\\\\*]+', re.UNICODE)
exec_wget = ""
exec_curl = ""
exec_avconv = ""
exec_ffmpeg = ""
exec_rtmpdump = ""
exec_mimms = ""
def init_tools():
"""
Find tools that will be usefull
"""
global exec_wget
global exec_curl
global exec_avconv
global exec_ffmpeg
global exec_rtmpdump
global exec_mimms
# Check availability of dependencies
exec_wget = check_exec('wget')
exec_curl = check_exec('curl')
exec_avconv = check_exec('avconv')
exec_ffmpeg = check_exec('ffmpeg')
exec_rtmpdump = check_exec('rtmpdump')
exec_mimms = check_exec('mimms')
def removenonascii(s):
"""
Remove non ASCII character from a string
:rtype : basestring
:param s:
:return:
"""
return "".join(char for char in s if ord(char) < 128)
def removespecial(s):
"""
Remove non ASCII character from a string
:rtype : basestring
:param s:
:return:
"""
return rx.sub(' ', '%s' % s)
def matched(string, regexp):
"""
Check if a string satisfy a regexp
:rtype : logical
:param string:
:param regexp:
:return:
"""
if regexp and string and not string == "Not loaded":
return re.search(regexp, string) is not None
return None
def check_exec(executable):
"""
Check if the executable exist
:rtype : path
:param executable:
:return:
"""
output, error = Popen(['which', executable],
stdout=PIPE, stderr=PIPE).communicate()
if error:
logging.info('%s Not found' % executable)
return ""
logging.info('Path to %s : %s' % (executable, output.replace("\n", "")))
return output
def check_link(links_directory, link_name):
"""
Check existence of the linkfile's target
:rtype : bool
:param links_directory:
:param link_name:
"""
if not os.path.islink(link_name.encode('utf8')):
return True
file_name = os.readlink(link_name.encode('utf8')).decode('utf8')
absolute_file_name = os.path.join(links_directory, file_name)
if os.path.isfile(absolute_file_name.encode('utf8')):
logging.debug("Link still valid : %s -> %s" % (link_name, absolute_file_name))
return True
return False
def purge(links_directory):
"""
remove link if target have been removed
:param links_directory:
"""
if not os.path.isdir(links_directory.encode('utf8')):
return
logging.debug("Purging %s" % links_directory)
dirlist = os.listdir(links_directory.encode('utf8'))
for local_link_name in dirlist:
link_name = links_directory + "/" + local_link_name.decode('utf8')
if not check_link(links_directory, link_name):
logging.info("Removing %s" % link_name)
os.remove(link_name.encode('utf8'))
def do_download(video, filename):
"""
Download video file
:param video:
:param filename:
:return:
"""
if video.url.startswith('rtmp'):
if not exec_rtmpdump:
logging.error('I Need rtmpdump')
return 1
args = ['rtmpdump', '-e', '-r', video.url, '-o', filename]
elif video.url.startswith('mms'):
if not exec_mimms:
logging.error('I Need mimms')
return 1
args = ['mimms', video.url, filename]
else:
if not exec_wget:
if not exec_curl:
logging.error('I Need curl or wget')
return 1
else:
args = ['curl', '-s', video.url, '-o', filename]
else:
args = ['wget', '-q', video.url, '-O', filename]
logging.debug("downloading %s with %s" % (video.title, args[0]))
process = Popen(args, stdout=PIPE, stderr=PIPE)
output, error = process.communicate()
res = process.wait()
logging.info(output)
if error:
logging.error(error)
return res
def do_conv(video, filename):
"""
Convert a streaming file into a video file
:param video:
:param filename:
"""
if video.ext == "m3u" or video.ext == "m3u8":
video.ext = 'avi'
if not exec_avconv:
if not exec_ffmpeg:
logging.error('I Need avconv or ffmpeg')
return 1
else:
args = ['ffmpeg', '-i', video.url, '-vcodec', 'copy',
'-acodec', 'copy', '-loglevel', 'error',
filename] # "-stat" ,'-threads', '8'
else:
args = ['avconv', '-i', video.url, '-c', 'copy', filename]
logging.debug("Converting %s with %s" % (video.title, args[0]))
process = Popen(args, stdout=PIPE, stderr=PIPE)
output, error = process.communicate()
res = process.wait()
logging.info(output)
if error:
logging.debug(error)
return res
else:
logging.debug("Not Converting %s, i don't understand" % video.title)
return 0
def init_logging():
"""
Define the logging file
It's possible to set the level to logging.DEBUG if you want more verbosity
"""
logging.basicConfig(level=logging.DEBUG,
format="%(asctime)s [%(levelname)-5.5s] %(message)s",
datefmt='%m-%d %H:%M',
filename='downloadboob.log')
# Also log on the console
# it's possible to set the level to logging.DEBUG if you want more verbosity
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
# console.setLevel(logging.DEBUG)
logformatter = logging.Formatter("%(message)s")
console.setFormatter(logformatter)
logging.getLogger('').addHandler(console)
logging.warning("Hello")