-
Notifications
You must be signed in to change notification settings - Fork 0
/
streampy.py
executable file
·129 lines (102 loc) · 3.34 KB
/
streampy.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
#!/usr/bin/python3
from __future__ import division
from subprocess import Popen, PIPE
import pyperclip as pc
import sys
import urllib.parse
import urllib.request
import re
#html redirect following taken from torskbot
#https://github.com/sebth/torskbot
class FinalURLHTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
def __init__(self, *args, **kwargs):
self.final_url = None
super().__init__(*args, **kwargs)
def redirect_request(self, req, fp, code, msg, hdrs, newurl):
self.final_url = newurl
return super().redirect_request(req, fp, code, msg, hdrs, newurl)
def quote_nonascii_path(path):
return re.sub(
b'[\x80-\xff]',
lambda match: '%{:x}'.format(ord(match.group())).encode('ascii'),
path.encode()).decode('ascii')
def urlquote(url):
parts = urllib.parse.urlsplit(url)
return urllib.parse.urlunsplit(
(parts[0], parts[1].encode('idna').decode('ascii'),
quote_nonascii_path(parts[2])) + parts[3:])
def final_url(url):
url = quote_nonascii_path(url)
rh = FinalURLHTTPRedirectHandler()
opener = urllib.request.build_opener(rh)
url=urlquote(url)
opener.open(url)
final_url = rh.final_url
return rh.final_url if rh.final_url else url
#end html redirect
#
def read_url_from_clipboard():
clipboard = pc.paste()
if isinstance(clipboard, bytes):
clipboard = clipboard.decode('UTF-8','ignore')
return clipboard
def is_url(text):
#rudimentary check, replace with proper regexp.
return '.' in text
def conditional_print(arguments, verbose=False):
if verbose:
print(arguments)
def main():
arguments = sys.argv
url = None
verbose = False
for arg in sys.argv[1:]:
if is_url(arg):
url = arg
elif arg == '--verbose' or arg == '-v':
verbose=True
conditional_print('verbose output on', verbose)
if not url:
url = read_url_from_clipboard()
try:
conditional_print("trying to folllow http redirects",verbose)
url = final_url(url)
conditional_print("playing", verbose)
play_url(url, verbose)
except Exception as e:
print(e)
pass
def play(url, youtube_args, verbose):
conditional_print('youtube url', verbose)
args = youtube_args.format(url).split(' ')
if verbose:
print(args)
p = Popen(args, stdout=PIPE, stderr=PIPE)
outmsg, errmsg = p.communicate()
if verbose:
print("process output\n", outmsg)
if errmsg:
print("error?", errmsg)
return outmsg, errmsg
def play_url(url, verbose=False):
CLI_args = 'mpv {}'
if isinstance(url, bytes):
url = url.decode('UTF-8')
if 'twitch.tv' or 'youtube.com' in url:
play(url, CLI_args, verbose)
else:
#fallback: hope livestreamer works, output to stdout
args = CLI_args.format(url).split(' ')
if verbose:
print(args)
p = Popen(args)
p.communicate()
if __name__ == '__main__':
main()
#a couple of example links to test whether it works
"""
url = 'http://www.twitch.tv/forgg'
url = 'https://www.youtube.com/watch?v=QcqC4jtrR9Y' #regular
url = 'https://www.youtube.com/watch?v=NJ3aiM8K6D0' #protected
url = 'http://tinyurl.com/comeonnn' #tinyurl
"""