Δημοσιεύτηκε: 24 Αύγ 2009, 11:55
από DarkLord
Livescores superleage
ένα ωραίο scripaki για να βλέπεις τα σκορ της superleage

Κώδικας: Επιλογή όλων
#!/usr/bin/env python
# encoding: utf-8
#====================================================================
# Name :
# livescore.py
# Version :
# v0.1c
# Author :
# WorldCitizeN
# Date :
# 2009-05-26
# Description :
# Shows the Results of Football Games
# using data from
#
# http://livescores.com/default.dll?page=greece
#
#====================================================================
import sys
import urllib2
import HTMLParser

__author__ = "WorldCitizeN"
__version__ = "0.1c"

def help_():
return """# livescore.py v0.1c
# Author :
# WorldCitizeN
# Description :
# A simple Python Script which
# shows the live results using data from
# http://livescores.com/default.dll?page=greece

-help
Self Explanatory
-list
Shows the results available, default is the Greek Super Liga
live
Shows all the live results
england
Shows all the results in the English Leagues
"""



# Change color_ to 0 if you do not want
# a colored output.
color_ = 1

url = 'http://livescores.com/default.dll?page='

def Beautify(data, length_):
return (data.strip() + " "*100)[:length_]

def urldata(url_, data_=None):
"""Returns HTML code from url."""
return urllib2.urlopen(urllib2.Request(url_, data_)).read()


class Spider(HTMLParser.HTMLParser):

def __init__(self, fed, tag = 'td', fn = None):
HTMLParser.HTMLParser.__init__(self)
self.data = [ [], [], [], [] ] # [ [Time], [Home Team], [Score], [Guest Team] ]
self.tabs = [ 8, 20, 8, 20 ] # Identation for self.data
self.ps = 0 # Type of self.data currently read
self.NGames = 0 # Number of Games Currently paresed
self.TmpStr = "" # Temporary String
self.GotStr = 0 # Identifier for League and Date

self.Leagues = dict()
self.Dates = dict()
self.Info = [self.Leagues, self.Dates]

self.Links = []
self.GotLinks= 0
self.feed(fed)
self.data = zip(*self.data)

self.Links = self.Links[2:-3]

tmp = sorted(self.Leagues.keys())[::-1]

for i,t in enumerate(tmp):
s2c = "\033[1m"*color_ + self.Leagues[t] + "\033[0m"*color_
try:
s2c += "\n\033[1m"*color_ + self.Dates[t] + "\033[0m"*color_
except:
pass
middle = " "*int( (sum(self.tabs) - len(self.Leagues[t]))/2 )
self.data = self.data[:t] + [middle + s2c] + self.data[t:]

def handle_starttag(self, tag, attrs):
if tag == 'td':
if attrs == [('width', '45'), ('height', '18')]:
self.ps = 1
elif attrs == [('align', 'right'), ('width', '118')]:
self.ps = 2
elif attrs == [('align', 'center'), ('width', '50')]:
self.ps = 3
elif attrs == [('width', '118')]:
self.ps = 4
# League Identifier
elif attrs == [('class', 'title'), ('colspan', '4'), ('height', '18')]:
self.GotStr = 1
# Date Identifier
elif attrs[:2] == [('class', 'match-light'), ('align', 'right')]:
self.GotStr = 2
self.tdata = []
if tag == 'a':
if attrs[0][1][:18] == '/default.dll?page=':
self.Links += [[attrs[0][1][18:], ""]]
self.GotLinks= 1

def handle_data(self, data):
if self.ps != 0:
self.data[self.ps-1] += [ (data.strip() + " "*20)[:self.tabs[self.ps - 1]] ]
if self.ps == 1:
self.NGames += 1
self.ps = 0
if self.GotStr != 0:
self.TmpStr += data
if self.GotLinks == 1:
self.Links[-1][1] = data
self.GotLinks = 0

def handle_endtag(self,tag):
if tag == 'td' :
if self.GotStr == 1:
self.Leagues[self.NGames] = self.TmpStr
elif self.GotStr == 2:
self.Dates[self.NGames] = self.TmpStr
if self.GotStr != 0:
self.TmpStr = ""
self.GotStr = 0
pass


def ShowGames(country):
for iG, Game in enumerate(Spider(urldata(url + country)).data):
print( "".join(Game))
return

def GameList_():
links_ = Spider(urldata(url)).Links
for link_ in links_:
l0, l1 = Beautify(link_[0],12), Beautify(link_[1],12)
print( l1 +" : "+ "\033[1m"*color_ + l0 + color_*"\033[0m")
print( "eg. \"livescore.py soccer\" for All Soccer Games ")
return

def main():
if len(sys.argv) > 1:
if sys.argv[1] == '-list' or sys.argv[1] == '-l':
GameList_()
elif sys.argv[1] == '-help' or sys.argv[1] == '-h':
print( help_())
else:
ShowGames(sys.argv[1])
else:
ShowGames("greece")




if __name__ == '__main__':
# print("Data taken from%s"%url)
main()