Advertisement
Guest User

LoLhistory.py

a guest
Jul 22nd, 2013
28,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. LoLhistory
  4. Created on Mon Jul 22 15:12:52 2013
  5.  
  6. @author: ArcticTurtle
  7. """
  8.  
  9. import os
  10. import sys
  11. import time
  12. import datetime
  13. from PyQt4 import QtCore, QtGui, QtSql
  14.  
  15.  
  16. class ParserForm(QtGui.QDialog):
  17.     def __init__(self, parent=None):
  18.         super(ParserForm, self).__init__(parent)
  19.         self.browser = QtGui.QTextBrowser()
  20.         self.browser.setFixedWidth(900)
  21.         self.line = QtGui.QLineEdit()
  22.         self.progress = QtGui.QProgressBar()
  23.         self.progress.setMaximum(100)
  24.         self.progress.setValue(0)
  25.         layout = QtGui.QVBoxLayout()
  26.         layout.addWidget(self.browser)
  27.         layout.addWidget(self.line)
  28.         layout.addWidget(self.progress)
  29.         self.setLayout(layout)
  30.         self.line.setFocus()
  31.         self.connect(self.line, QtCore.SIGNAL("returnPressed()"), self.updateUI)
  32.         self.setWindowTitle("Load League of Legends history")
  33.         self.browser.append("0 + Enter to quit.")
  34.         self.browser.append("Summoner name + Enter to parse")
  35.         self.thread = parserthread()
  36.         self.connect(self.thread, QtCore.SIGNAL("updateProgress(int, int, QString)"), self.updateProgress)
  37.         self.connect(self.thread, QtCore.SIGNAL("updateFinished()"), self.updateFinished)
  38.         self.connect(self.thread, QtCore.SIGNAL("showtext(QString)"), self.showtext)
  39.         self.connect(self.thread, QtCore.SIGNAL("addchamp(QString)"), self.addchamp)
  40.  
  41.         self.champlist = []
  42.  
  43.     def updateUI(self):
  44.         text = self.line.text()
  45.         if text == '0':
  46.             app.exit()
  47.         else:
  48.             self.thread.setname(text)
  49.             self.browser.append("Started at " + str(datetime.datetime.now()))
  50.             self.thread.begin()
  51.  
  52.     def showtext(self, text):
  53.         self.browser.append(text)
  54.         app.processEvents()
  55.  
  56.     def addchamp(self, text):
  57.         self.champlist.append(text)
  58.  
  59.     def updateProgress(self, progress, total, filename):
  60.         try:
  61.             if filename == "ERROR!":
  62.                 self.progress.setValue((progress/(total+1.0))*100)
  63.                 self.browser.append("An error occured, parsing was not completed.")
  64.                 self.progress.update()
  65.                 app.processEvents()
  66.             else:
  67.                 self.progress.setValue((progress/(total+1.0))*100)
  68.                 self.progress.update()
  69.                 val = self.progress.value()
  70.                 app.processEvents()
  71.         except:
  72.             None
  73.  
  74.     def updateFinished(self):
  75.         try:
  76.             self.champlist.sort()
  77.             nc = 0
  78.             for c in self.champlist:
  79.                 if nc == 0:
  80.                     c1 = c
  81.                     nc += 1
  82.                 elif c == c1:
  83.                     nc += 1
  84.                 elif c != c1:
  85.                     self.browser.append(c1 + "\t played \t" + str(nc) + "\t times.")
  86.                     c1 = c
  87.                     nc = 1
  88.             self.progress.setValue(100)
  89.             self.browser.append("Done.")
  90.             self.browser.append("Ended at " + str(datetime.datetime.now()))
  91.         except:
  92.             None
  93.  
  94. ########################################################################
  95. class parserthread(QtCore.QThread):
  96.     """"""
  97.     #----------------------------------------------------------------------
  98.     def __init__(self):
  99.         """Constructor"""
  100.         super(parserthread, self).__init__()
  101.         self.myname = "N/A"
  102.  
  103.     def setname(self,name):
  104.         self.emit(QtCore.SIGNAL("showtext(QString)"), "Name was set")
  105.         self.myname = name
  106.  
  107.     def run(self):
  108.         n = 0
  109.         totn = 0
  110.         mypath = "C:\\Riot Games\\League of Legends\\Logs\\Game - R3d Logs"
  111.         self.emit(QtCore.SIGNAL("showtext(QString)"),mypath)
  112.         try:
  113.             for files in os.listdir(mypath):
  114.                 totn += 1
  115.             self.emit(QtCore.SIGNAL("showtext(QString)"), "Total games played: " + str(totn))
  116.             for files in os.listdir(mypath):
  117.                 if os.path.isfile(os.path.join(mypath,files)):
  118.                     n += 1
  119.                     self.findinfo(os.path.join(mypath,files))
  120.                     self.emit(QtCore.SIGNAL("updateProgress(int, int, QString)"), n, totn, files)
  121.             self.emit(QtCore.SIGNAL("updateFinished()"))
  122.         except e:
  123.             self.emit(QtCore.SIGNAL("updateProgress(int, int, QString)"), 100, 100, "ERROR!")
  124.             self.emit(QtCore.SIGNAL("updateFinished()"))
  125.  
  126.     def findinfo(self, thisfile):
  127.         readfile = open(thisfile, 'r')
  128.         for line in readfile:
  129.             if str(self.myname).lower() in line.lower():
  130.                 if line.find("Hero ") > 0:
  131.                     champ = line[line.find("Hero ")+4:line.find(" created for ")]
  132.                     champ = champ.replace("(0)","")
  133.                     #self.emit(QtCore.SIGNAL("showtext(QString)"),champ)
  134.                     self.emit(QtCore.SIGNAL("addchamp(QString)"),champ)
  135.     def begin(self):
  136.         self.start()
  137.  
  138.  
  139. app = QtGui.QApplication(sys.argv)
  140. form = ParserForm()
  141. form.show()
  142. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement