예제 #1
0
파일: main.py 프로젝트: mpenkov/cal2google
def main():
    from optparse import OptionParser

    parser = OptionParser("usage: %progname [options]")
    parser.add_option("-d", "--debug", dest="debug", action="store_true", help="enable debugging output")
    options, args = parser.parse_args()
    if args:
        parser.error("invalid number of arguments")

    curr_dir, _ = P.split(P.abspath(__file__))
    lmd_user, lmd_pass = open(P.join(curr_dir, "gitignore", "login.txt")).read().strip().split("\n")

    if options.debug:
        print "Logging in to LMD"

    fetcher = HTMLFetcher(lmd_user, lmd_pass)
    fetcher.login()

    if options.debug:
        print "Logging in to Google Calendar"

    calendar_service = login()
    for goog_name, lmd_name, regex in CALENDARS:
        if options.debug:
            print "Copying entries from LMD calendar (%s) to Google calendar (%s)" % (goog_name, lmd_name)
        google_id = get_calendar_id(calendar_service, goog_name)
        cid = CALENDAR_ID[lmd_name]
        if options.debug:
            print "Fetching LMD calendar (%s)" % cid
        stream = fetcher.get_calendar(cid)
        utf = codecs.decode(stream.read(), CALENDAR_ENCODING)
        if options.debug:
            print "Parsing calendar entries"
        entries = parse_entries(cid, utf)
        if options.debug:
            print "%d total entries parsed. Filtering entries" % len(entries)
        filtered = filter(lambda f: regex.search(f.title), entries)
        if options.debug:
            print "%d filtered entries. Parsing entry details" % len(filtered)
        for i, entry in enumerate(filtered):
            stream = fetcher.get_details(entry.cid, entry.event_id)
            html = codecs.decode(stream.read(), CALENDAR_ENCODING)
            entry.details = parse_details(html)
            if options.debug:
                print "Processed %d/%d entries" % (i, len(filtered))

        if entries:
            time_min = sorted(entries, key=lambda e: e.start_datetime)[0].start_datetime
        else:
            time_min = datetime.datetime.now().replace(tzinfo=JST())

        if options.debug:
            print "Fetching Google Calendar entries"
        google_entries = get_entries(calendar_service, google_id, time_min)

        if options.debug:
            print "all entries in LMD calendar `%s':" % lmd_name
            for e in entries:
                print "\t", e
            print "relevant entries in LMD calendar `%s':" % lmd_name
            for e in filtered:
                print "\t", e
            print "entries in Google calendar `%s':" % goog_name
            for e in sorted(google_entries):
                print "\t", e

        added, updated, deleted = sync(calendar_service, google_id, filtered, google_entries)

        if added or updated or deleted:
            print "from: %s to: %s total: %d filtered: %d added: %d updated: %d deleted: %d" % (
                lmd_name,
                goog_name,
                len(entries),
                len(filtered),
                added,
                updated,
                deleted,
            )
예제 #2
0
파일: parse.py 프로젝트: mpenkov/cal2google
def parse_details(utf):
    """
    Parses entry details given HTML as a unicode string.  Returns a dictionary
    of details.
    """
    ep = EntryParser()
    ep.feed(utf)
    result = {}
    for x, y in filter(lambda (x,y): x and y and x in PROP_NAMES, ep.details):
        result[x] = y
    return result

if __name__ == '__main__':
    import os.path as P
    curr_dir, _ = P.split(P.abspath(__file__))
    lmd_user, lmd_pass = open(P.join(
        curr_dir, 'gitignore', 'login.txt')).read().strip().split('\n')    

    from fetch import HTMLFetcher, CALENDAR_ENCODING

    f = HTMLFetcher(lmd_user, lmd_pass)
    f.login()
    import codecs
    html = codecs.decode(f.get_details(52, 30349).read(), CALENDAR_ENCODING)

    details = parse_details(html)

    for e,d in details.items():
        print e, '-->', d