aboutsummaryrefslogtreecommitdiffstats
path: root/import.py
blob: 3138e736ab33690f2ab9fa79e66b1882420e0ef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sqlite3
import sys
from datetime import datetime

def convert_date(date):
    return datetime.strptime(date, "%Y-%m-%d %H:%M:%S")

def import_users(old_conn, new_conn):
    for row in old_conn.execute("select * from users"):
        address = old_conn.execute("select * from adress where id=?", (row["adress_id"],)).fetchone()
        new_conn.execute("insert into users (id, user_name, password, birthday, "
                         "nameday, notify, email, phone, address_line1, address_line2, " 
                         "address_city_line) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                         (row["id"], row["name"], row["password"],
                          "/".join((str(row["birthday"]), str(row["birthmonth"]))),
                          "/".join((str(row["feastday"]), str(row["feastmonth"]))),
                          row["notify"], row["email"], row["cellphone"],
                          address["line1"], address["line2"], " ".join((address["zip"],
                              address["city"]))))
    new_conn.commit()

def import_news(old_conn, new_conn):
    for row in old_conn.execute("select * from news"):
        new_conn.execute("insert into news (id, title, date, content, content_cache, "
                         "user_id) VALUES(?, ?, ?, ?, ?, ?)",
                         (row["id"], row["title"], convert_date(row["date"]),
                          row["raw"], row["news"], row["auteur_id"]))
    new_conn.commit()

def import_comments(old_conn, new_conn):
    for row in old_conn.execute("select * from news_comments"):
        new_conn.execute("insert into comments (id, date, content_cache, user_id, news_id) "
                         "values (?, ?, ?, ?, ?)",
                         (row["id"], convert_date(row["date"]), row["comment"],
                          row["auteur_id"], row["news_id"]))
    new_conn.commit()

if __name__ == "__main__":
    old = sys.argv[1]
    new = sys.argv[2]
    old_conn = sqlite3.connect(old)
    old_conn.row_factory = sqlite3.Row
    new_conn = sqlite3.connect(new, detect_types=sqlite3.PARSE_DECLTYPES)
    new_conn.row_factory = sqlite3.Row
    import_users(old_conn, new_conn)
    import_news(old_conn, new_conn)
    import_comments(old_conn, new_conn)
    old_conn.close()
    new_conn.close()