-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaceproxy_player.py
executable file
·159 lines (125 loc) · 4.23 KB
/
aceproxy_player.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""AceProxy Player: Open acestream links with any media player"""
import sys
import time
import socket
import urllib
import argparse
import psutil
import notify2
class AceProxyPlayer(object):
"""AceProxy Player"""
def __init__(self):
parser = argparse.ArgumentParser(
prog='aceproxy-player',
description='Open acestream links with any media player'
)
parser.add_argument(
'url',
metavar='URL',
help='the acestream url to play'
)
parser.add_argument(
'--host',
help='the aceproxy server host (default: localhost)',
default='localhost'
)
parser.add_argument(
'--port',
help='the aceproxy server port (default: 8000)',
default='8000'
)
parser.add_argument(
'--player',
help='the media player to use (default: vlc)',
default='vlc'
)
self.appname = 'Acestream Proxy Player'
self.args = parser.parse_args()
notify2.init(self.appname)
self.notifier = notify2.Notification(self.appname)
self.parse_url()
self.start_proxy()
self.start_session()
self.start_player()
self.close_player()
def notify(self, message):
"""Show player status notifications"""
icon = self.args.player
messages = {
'running': 'Acestream local proxy running.',
'missing': 'Acestream local proxy not installed!',
'waiting': 'Waiting for channel response...',
'started': 'Streaming started. Launching player.',
'unavailable': 'Acestream channel unavailable!'
}
print(messages[message])
self.notifier.update(self.appname, messages[message], icon)
self.notifier.show()
def parse_url(self):
"""Parse the given url"""
url = self.args.url
host = self.args.host
port = self.args.port
if 'acestream' in url:
url = url.split('//')[1]
url = 'http://' + host + ':' + port + '/pid/' + url + '/acestream.mp4'
elif 'http' in url:
url = urllib.parse.quote_plus(url)
url = 'http://' + host + ':' + port + '/torrent/' + url + '/torrent.mp4'
else:
url = 'http://' + host + ':' + port + '/' + url
self.url = url
def start_proxy(self):
"""Start aceproxy if not running"""
if self.args.host not in ['localhost', '127.0.0.1', '0.0.0.0']:
return
for process in psutil.process_iter():
if 'aceproxy' in process.name():
process.kill()
if 'acestreamengine' in process.name():
process.kill()
try:
self.proxy = psutil.Popen('aceproxy')
self.notify('running')
time.sleep(5)
except FileNotFoundError:
self.notify('missing')
self.close_player(1)
def start_session(self):
"""Start player socket session"""
self.notify('waiting')
time.sleep(15)
try:
session = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
session.settimeout(30)
session.connect((self.args.host, int(self.args.port)))
session.close()
self.notify('started')
except socket.error:
self.notify('unavailable')
self.close_player(1)
def start_player(self):
"""Start the media player"""
self.player = psutil.Popen([self.args.player, self.url])
self.player.wait()
def close_player(self, code=0):
"""Close aceproxy and media player"""
try:
self.proxy.terminate()
except (AttributeError, psutil.NoSuchProcess):
print('AceProxy not running...')
try:
self.player.terminate()
except (AttributeError, psutil.NoSuchProcess):
print('Media Player not running...')
sys.exit(code)
def main():
"""Start AceProxy Player"""
try:
AceProxyPlayer()
except (KeyboardInterrupt, EOFError):
print('AceProxy Player exiting...')
sys.exit(0)
main()