from PySide.QtCore import *
from PySide.QtGui import *
from lxml import html
import requests
import sys
class browserWindow(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle("All Live Matches")
self.mygroupbox = QGroupBox('Live Matches')
self.myform = QFormLayout()
self.display_info()
self._want_to_close = False
def fetch_live_matches(self):
page = requests.get('http://www.espncricinfo.com/ci/engine/match/index.html?view=live')
tree = html.fromstring(page.text)
b1 = tree.cssselect(".innings-info-1")
b2 = tree.cssselect(".innings-info-2")
matchInfo = []
i = 0
for item in b1:
team, score= ((item.text_content()).strip("\n")).split(" ")
score.strip()
team2, score2 = (b2[i].text_content().strip("\n")).split(" ")
score2.strip()
matchInfo.append({team : score.split("\n")[0], team2 : score2.split("\n")[0],"id":str(i)})
i += 1
matchID = i
# fr = open("live_matches.txt",'wb')
# fr.write(str(matchInfo))
# fr.close()
return matchInfo
def display_info(self):
data = self.fetch_live_matches()
c = 0
self.lableList = []
self.NLlable = QLabel("\n")
for match in data:
for key, val in match.iteritems():
v = key + " : " + val
self.lableList.append(QLabel(v))
self.myform.addRow(self.lableList[c])
c += 1
self.myform.addRow(self.NLlable)
self.mygroupbox.setLayout(self.myform)
self.scroll = QScrollArea()
self.scroll.setWidget(self.mygroupbox)
self.scroll.setWidgetResizable(True)
self.scroll.setFixedHeight(400)
self.layout = QVBoxLayout(self)
self.layout.addWidget(self.scroll)
def closeApp(self):
self._want_to_close = True
self.close()
def closeEvent(self, evnt):
if self._want_to_close:
super(browserWindow, self).closeEvent(evnt)
else:
evnt.ignore()
self.hide()
class cricAPI(QDialog):
def __init__(self, parent = None):
super(cricAPI,self).__init__(parent,Qt.WindowMinimizeButtonHint)#|Qt.WindowMaximizeButtonHint)
self.setWindowTitle("Live Score")
self.grid = QGridLayout()
self.setLayout(self.grid)
self.matchID_lbl = QLabel("<b>MATCH ID : </b>")
self.grid.addWidget(self.matchID_lbl,0,0)
self.matchID_input = QLineEdit()
self.matchID_input.setPlaceholderText("Enter match id")
self.grid.addWidget(self.matchID_input,0,1)
self.get_score_btn = QPushButton("Get Score")
self.grid.addWidget(self.get_score_btn,1,1)
self.get_score_btn.clicked.connect(self.fetchScore)
self.browseLable = QLabel("<b>Browse to get match id</b>")
self.grid.addWidget(self.browseLable,2,0)
self.browse = QPushButton("Browse")
self.grid.addWidget(self.browse,2,1)
# bw = browserWindow()
self.browse.clicked.connect(self.showMe)
# r = Render()
self.quitAppl = QPushButton("Quit App")
self.grid.addWidget(self.quitAppl,3,1)
self.quitAppl.clicked.connect(self.closeApp)
self.setFocus()
systray_icon = QIcon("cric_icon.png")
self.systray = QSystemTrayIcon(systray_icon, self)
contextMenu = QMenu()
hide = QAction("Hide",self)
show = QAction("Show",self)
close = QAction("Quit",self)
contextMenu.addActions([show,hide,close])
self.systray.setContextMenu(contextMenu)
self.systray.setToolTip("Cricket Score")
self.systray.show()
close.triggered.connect(self.closeApp)
show.triggered.connect(self.show)
hide.triggered.connect(self.hide)
self.systray.activated.connect(self.systrayAction)
self._want_to_close = False
def systrayAction(self,reason):
if reason == QSystemTrayIcon.DoubleClick:
self.show()
elif reason == QSystemTrayIcon.MiddleClick:
self.close()
else:
pass
def fetchScore(self):
ID = self.matchID_input.text()
bw = browserWindow()
data = bw.fetch_live_matches()
matchinfo = (item for item in data if item["id"] == str(ID)).next()
print matchinfo
mi = ""
for key, value in matchinfo.iteritems():
if key != "id":
mi = mi + key + ":" + value + "\n"
self.systray.showMessage("SCORE", mi, int = 5000)
self.hide()
self.timer = QTimer()
self.timer.timeout.connect(self.fetchScore)
self.timer.start(10000)
# bw.close()
# QTimer.singleShot(20000, lambda: self.fetchScore())
# self.fetchScore()
def showMe(self):
self.myOtherWindow = browserWindow()
self.myOtherWindow.show()
# Dialog.exec_()
# self.show()
# def myExitHandler(self):
# self.hide()
def closeApp(self):
self._want_to_close = True
self.close()
def closeEvent(self, evnt):
if self._want_to_close:
super(cricAPI, self).closeEvent(evnt)
else:
evnt.ignore()
# self.setWindowState(Qt.WindowMinimized)
self.hide()
self.systray.showMessage("STILL ACTIVE", "If you want to quit, right click me to and click quit",int = 1000)
if __name__ == "__main__":
app = QApplication(sys.argv)
csGUI = cricAPI()
#app.aboutToQuit.connect(csGUI.myExitHandler)
csGUI.show()
app.exec_()
Make sure to "pip install" pyside, lxml, cssselect, requests and also place a cric_icon.png in the same folder as this code
This is a notification app.. you can change the timing of notification interval in the code as you like.
If any of you can package it into exe using pyinstaller ,py2exe etc.. please let me know. I have tried using pyinstaller. seems requests module and some other are facing issues. Thanks