-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidge.py
More file actions
205 lines (166 loc) · 6.47 KB
/
widge.py
File metadata and controls
205 lines (166 loc) · 6.47 KB
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
from gi.repository import WebKit, Gtk, Gdk, GObject
import signal
import requests
import time
import re
class BackgroundPaneCallbacks:
pass
class BackgroundPaneWebview(WebKit.WebView):
def __init__(self, score, title, commentary):
WebKit.WebView.__init__(self)
self.set_transparent(False)
self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0,0,0,0))
score = ParseScore.split_scores(score)
self.load_html_string(ParseScore.generate_html(score, title, commentary), '')
print("Webview loaded")
GObject.timeout_add_seconds(10, self.callback)
def callback(self):
score, title, commentary = ParseScore.get_scores()
score = ParseScore.split_scores(score)
self.load_html_string(ParseScore.generate_html(score, title, commentary),'')
return True
class ParseScore():
@classmethod
def get_scores(cls):
r = requests.get('http://cricapi.com/api/cricket/')
if r.status_code != 200:
return None
match_list = r.json()
teams_list = ['Delhi Daredevils',
'Gujarat Lions',
'Kings XI Punjab'
'Kolkata Knight Riders',
'Mumbai Indians',
'Rising Pune Supergiants',
'Royal Challengers Bangalore']
titles = [(i['unique_id'], i['title']) for i in match_list['data']]
unique_ids = []
for team in teams_list:
for title in titles:
if team in title[1]:
unique_ids.append(title)
unique_id = unique_ids[0][0]
#print '\033[92m' + unique_id + '\033[0m'
payload = {'unique_id': unique_id}
out = requests.get('http://cricapi.com/api/cricketScore/', params=payload)
match_info = out.json()
comment = requests.get('http://cricapi.com/api/cricketCommentary/', params=payload)
commentary = comment.json()['commentary']
commentary = re.sub(r'\s*\n\s*', '', commentary)
list_comm = re.split(r'\.(?!\d)', commentary)
final_comment = '.'.join(list_comm[:5])
return match_info['score'], unique_ids[0][1], final_comment
@classmethod
def split_scores(cls, score):
if '*' in score:
m = re.search(r'^([^()]*)\(([^,]+),\s*([^,]+),\s*([^,]+),\s*([^)]+)', score)
score = m.group(1)
overs = m.group(2)
bat1 = m.group(3)
bat2 = m.group(4)
bowl = m.group(5)
return score, overs, bat1, bat2, bowl
return score
@classmethod
def generate_html(cls, score, title, commentary):
if type(score) == unicode:
return '''<HTML>
<STYLE type="text/css">
BODY {
background: rgba(0,0,0,0);
background-color: #009688;
}
</STYLE>
<BODY>
<h2>''' + title + '''</h2>
<p style="color:yellow">''' + score +'''</p>
''' + commentary + '''
</BODY>
</HTML>'''
else:
return '''<HTML>
<STYLE type="text/css">
BODY { background: rgba(0,0,0,0);
background-color: #009688;
}
</STYLE>
<BODY>
<h2>''' + title + '''</h2>
<p style="color:Blue">Score: ''' + score[0] + '''</p>
<p style="color: #cddc39">overs: ''' + score[1] +'''</p>
<p style="color:Orange">''' + score[2] +'''</p>
<p style="color:yellow">''' + score[3] +'''</p>
<p style="color:Brown">''' + score[4] +'''</p>
''' + commentary + '''
</BODY>
</HTML>'''
class BackgroundPaneWin(Gtk.Window):
def __init__(self, address="127.0.0.1", port=54541):
Gtk.Window.__init__(self, title="Live cricket Scores")
self.set_title("Live IPL cricket Scores")
self.set_size_request(400,200)
#Set transparency
screen = self.get_screen()
rgba = screen.get_rgba_visual()
self.set_visual(rgba)
self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0,0,0,0))
#parse matchlist and score
score, title, commentary = ParseScore.get_scores()
self.view = BackgroundPaneWebview(score, title, commentary)
#Add all the parts
box = Gtk.Box()
#self.add(box)
box.pack_start(self.view, True, True, 0)
self.set_decorated(True)
self.connect("destroy", lambda q: Gtk.main_quit())
f = Gtk.Frame()
f.add(box)
self.add(f)
#Configure
#Show all the parts
self.show_all()
print("Win loaded")
class BackgroundPane:
def __init__(self, params=False):
#Add all the parts
self.root = params['root']
self.win = BackgroundPaneWin()
print("Pane loaded")
def init(self):
pass
def add_widget(self, widget):
pass
class Logger:
def __init__(self, root):
self.root = root
self.log("Logger loaded")
def log(self, msg, level='console'):
if level=='console':
print msg
class Handlers:
pass
class App:
def __init__(self, params={}):
#Get screen geometry
s = Gdk.Screen.get_default()
params['w'] = s.get_width()
params['h'] = s.get_height()
#Store params
self.params = params
self.log = Logger(self).log
self.handlers = Handlers()
#Get all components
bg = BackgroundPane({'root':self,
})
#Store all components
self.components = {}
self.components['bg'] = bg
#Make sure everything is started
#Make sure Ctl-C works
signal.signal(signal.SIGINT, signal.SIG_DFL)
def run(self):
Gtk.main()
if __name__ == '__main__':
print("Loading app...")
app = App()
app.run()