forked from ckorn/livestreamer-ui
/
stream.py
112 lines (64 loc) · 2.67 KB
/
stream.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
import json
from PySide.QtCore import QProcess, Qt
from PySide.QtGui import QColor, QBrush
class Stream:
ALL_STREAMS = []
def __init__( self, arguments ):
self.arguments = arguments
def start( self, messageElement ):
Stream.clear_streams()
process = QProcess()
self.process = process
self.messageElement = messageElement
process.setProcessChannelMode( QProcess.MergedChannels )
process.start( 'livestreamer', self.arguments )
process.readyReadStandardOutput.connect( self.show_messages )
Stream.ALL_STREAMS.append( self )
def is_online( self, tableWidgetItem ):
Stream.clear_streams()
process = QProcess()
self.process = process
self.table_widget_item = tableWidgetItem
arguments = [ '--json' ] + self.arguments
process.setProcessChannelMode( QProcess.MergedChannels )
process.start( 'livestreamer', arguments )
process.readyReadStandardOutput.connect( self.is_online_callback )
brush=QBrush(Qt.SolidPattern)
color=QColor(255,255,255) #white
brush.setColor(color)
tableWidgetItem.setBackground( brush )
tableWidgetItem.setText( 'Checking..' )
Stream.ALL_STREAMS.append( self )
def is_online_callback( self ):
outputBytes = self.process.readAll().data()
outputUnicode = outputBytes.decode( 'utf-8' )
try:
outputObject = json.loads( outputUnicode )
except ValueError as errorMessage:
print( errorMessage )
return
brush=QBrush(Qt.SolidPattern)
if outputObject.get( 'error' ):
color=QColor(255,0,0) #red
onlineStatus = 'Off'
else:
color=QColor(0,255,0) #green
onlineStatus = 'On'
brush.setColor(color)
itemWidget = self.table_widget_item
itemWidget.setBackground( brush )
itemWidget.setText( onlineStatus )
def show_messages( self ):
outputBytes = self.process.readAll().data()
outputUnicode = outputBytes.decode( 'utf-8' )
self.messageElement.append( outputUnicode )
@staticmethod
def clear_streams():
"""
Remove the streams that have ended (process is not running anymore) from the list that contains all the stream objects
"""
streams = []
for stream in Stream.ALL_STREAMS:
if stream.process.state() != QProcess.NotRunning:
streams.append( stream )
Stream.ALL_STREAMS = streams