-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_db.py
executable file
·65 lines (50 loc) · 1.4 KB
/
clean_db.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import logging
import sqlite3
# from https://docs.python.org/3/library/sqlite3.html
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def update(row):
sql = "UPDATE movie SET summary = ?, imdb_url = ? WHERE id = ?"
print(f"{row['title']}\n{row['imdb_url']}")
imdb_url = input("IMDb URL: ")
imdb_url = imdb_url.strip()
summary = input("Summary: ")
summary = summary.strip()
do_update = imdb_url or summary
if imdb_url:
imdb_url = imdb_url.replace("reference", "")
else:
imdb_url = row["imdb_url"]
if not summary:
summary = "N/A"
if do_update:
cur = conn.cursor()
cur.execute(
sql,
(
summary,
imdb_url,
row["id"],
),
)
conn.commit()
logging.debug("Updated %s", row["title"])
else:
logging.debug("No update for %s", row["title"])
def main():
sql = "SELECT * FROM movie WHERE summary = 'N/A'"
cursor = conn.cursor()
cursor.execute(sql)
for row in cursor.fetchall():
update(row)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()
conn = sqlite3.connect("movies.db")
conn.row_factory = dict_factory
main()
conn.close()