Home > FantaLega, python, sqlalchemy > Fantalight e Fantagazzetta: update valutazioni calciatori

Fantalight e Fantagazzetta: update valutazioni calciatori

27 Settembre 2011

I file scaricati da Fantagazzetta, anche se con estensione .xls, sono in
realtà in formato html.
Per aggiornare le valutazioni dei calciatori ho scritto questo codice,
che permette l’update dei giocatori, direttamente sul database utilizzato da
Fantalight.
Ho utilizzato BeautifulSoup per il parsing del file html.

'''
Utilizzare con FantaGazzetta-Quotazioni.
Vengono aggiornate solo le valutazioni dei giocatori
pre-esistenti.
Anche se con estensione xls, il file in ingresso e'
un HTML, quindi per il parsing viene utilizzato
Beautifulsoup
'''
import wx, os
from BeautifulSoup import BeautifulSoup
from DataLight import *
from sqlalchemy import and_

class FileBrowser(wx.FileDialog):
    '''Class for file browser'''
    def __init__(self):
        self.fin = None
        wildcard = "File Fantagazzetta (*.xls)|*.xls|" 
            "Tutti i files (*.*)|*.*"
        wx.FileDialog.__init__(self, None, "scegli il file", os.getcwd(),
                               "", wildcard, wx.OPEN | wx.CHANGE_DIR)
        if self.ShowModal() == wx.ID_OK:  
            print(self.GetPath())
            self.file = self.GetPath()
            self.fin = open(self.file, 'r')
        else:
            print "operazione apertura annullata"
            self.Destroy()
        self.Destroy()

def parser():
    '''Parse the siracusa txt'''
    model = Model()
    f_b = FileBrowser()
    html = f_b.fin.read()
    soup = BeautifulSoup(''.join(html))
    soup.prettify()    
    table = soup.find('table')
    rows = table.findAll('tr')
    for tr in rows:
        columns = tr.findAll('td')
        values = []
        for td in columns:
            line = td.find(text = True)
            values.append(line)
        try: # scarto l'intestazione del file
            int(values[0])
            nome = values[2]
            sq = (values[3])[0:3]
            val = values[4]
            player = model.session.query(Giocatore).filter(and_(
                     Giocatore.nome == nome, Giocatore.squadra == sq)).one()
            player.valore = val
            model.session.commit()
            print player, " ++++ value updated to %s" % val
        except ValueError:
            pass #passo se il valore non e' un cod. giocatore

    f_b.fin.close()

def main():
    '''app starter'''
    app = wx.PySimpleApp()
    app.MainLoop()
    parser()

if __name__ == '__main__':
    main()

Eseguito nella stessa dir di fantalight, funziona egregiamente

<Giocatore ('DI NATALE: attaccante')>  ++++ value updated to 37
<Giocatore ('CAVANI: attaccante')>  ++++ value updated to 36
<Giocatore ('IBRAHIMOVIC: attaccante')>  ++++ value updated to 34
<Giocatore ('PALACIO: attaccante')>  ++++ value updated to 31
<Giocatore ('TOTTI: attaccante')>  ++++ value updated to 30
<Giocatore ('PAZZINI: attaccante')>  ++++ value updated to 29
<Giocatore ('MATRI: attaccante')>  ++++ value updated to 28
<Giocatore ('FORLAN: attaccante')>  ++++ value updated to 28
...

link utili:
fantalightbeautifulsoup

Categorie:FantaLega, python, sqlalchemy Tag:
I commenti sono chiusi.