Exemplo n.º 1
0
class ParserFetcher:
    def __init__(self, tmp, list_formations = False):
        verb("Initializing...     ")
        self.fetcher = UMonsTimesheetFetcher(MAX_RETRIES, RETRY_DELAY)
        verb("done\n")

        self.list_formations = list_formations
        self.formations = self.get_formations()
        self.periods    = self.get_periods()
        self.tmp        = tmp

    def get_formations(self):
        verb("Fetch formations... ")
        formations = {}
        flist = self.fetcher.formations.items()
        for fid, fname in flist:
            formations[fid] = fname

        if self.list_formations:
            sort_func = lambda (a,b), (c,d): cmp(b.lower(), d.lower())
            flist.sort(sort_func)

            for fid, fname in flist:
                print str(fid) + " - " + fname

        verb("done: got " + str(len(flist)) + " formations\n")

        return formations

    def get_periods(self):
        verb("Fetch periods...    ")
        periods = {}
        plist = self.fetcher.periods.items()
        for pid, pname in plist:
            periods[pid] = pname
        verb("done: got " + str(len(plist)) + " periods\n")

        return periods

    def fetch(self, fid):
        for pid in self.periods.keys():
            html = self.fetcher.fetch_post_form(fid, [], pid)
            filename = path.join(self.tmp, "html",
                                 str(fid) + "-" + str(pid) + ".html")
            try:
                fd = codecs.open(filename, "w", "utf8")
                fd.write(unescape_html(html))
                fd.close()
            except IOError, err:
                print >> sys.stderr, err