コード例 #1
0
def add(db, anime):
    """Add an anime (or update existing).

    anime is an AnimeTree instance.

    """
    aid = anime.aid
    values = {
        'aid': aid,
        'title': anime.title,
        'type': anime.type,
        'episodecount': anime.episodecount,
    }
    if anime.startdate is not None:
        values['startdate'] = timestamp(anime.startdate)
    if anime.enddate is not None:
        values['enddate'] = timestamp(anime.enddate)
    with db:
        upsert(db, 'anime', ['aid'], values)
        our_anime = lookup(db, anime.aid, episode_fields=ALL)
        our_episodes = our_anime.episodes
        for episode in anime.episodes:
            add_episode(db, aid, episode)
            our_episodes = [
                ep for ep in our_episodes
                if not (ep.type == episode.type and ep.number == episode.number)
            ]
        # Remove extra episodes that we have.
        for episode in our_episodes:
            delete_episode(db, aid, episode)
コード例 #2
0
def migrate2(database):
    with database:
        cur = database.cursor()

        # Alter anime.
        cur.execute("""
        CREATE TABLE anime_new (
            aid INTEGER,
            title TEXT NOT NULL UNIQUE,
            type TEXT NOT NULL,
            episodecount INTEGER NOT NULL,
            startdate INTEGER,
            enddate INTEGER,
            PRIMARY KEY (aid)
        )""")
        cur.execute("""
        INSERT INTO anime_new (aid, title, type, episodecount)
        SELECT aid, title, type, episodes
        FROM anime
        """)
        # This is done in Python instead of SQL because f**k timezones.
        row = database.cursor()
        row = row.execute('SELECT aid, startdate, enddate FROM anime')
        cur.executemany(
            """UPDATE anime_new
            SET startdate=?, enddate=?
            WHERE aid=?""",
            ([timestamp(parse_date(startdate)) if startdate else None,
              timestamp(parse_date(enddate)) if enddate else None,
              aid]
             for aid, startdate, enddate in row),
        )
        cur.execute('DROP TABLE anime')
        cur.execute('ALTER TABLE anime_new RENAME TO anime')

        # Add title index.
        cur.execute("""
        CREATE INDEX anime_titles ON anime (title)
        """)

        # Add file_priority.
        cur.execute("""
        CREATE TABLE file_priority (
            id INTEGER PRIMARY KEY,
            regexp TEXT NOT NULL,
            priority INTEGER NOT NULL
        )""")