-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.py
55 lines (42 loc) · 1.63 KB
/
import.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
#!/usr/bin/env python
import argparse
import csv
import logging
import sqlite3
from src.database import VideoSn, Popular
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logging_output = logging.StreamHandler()
logging_output.setLevel(logging.INFO)
formatter = logging.Formatter(
"%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
logging_output.setFormatter(formatter)
logger.addHandler(logging_output)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("data", help="csv data to import")
parser.add_argument("-v", "--verbose", dest="verbose", help="Verbose output.")
args = parser.parse_args()
if args.verbose:
logging_output.setLevel(logging.DEBUG)
with open(args.data, encoding="utf-8", mode="r") as csvfile:
csv_reader = csv.reader(csvfile)
line_no = 0
for row in csv_reader:
line_no += 1
if len(row) != 4:
logger.warn(f"line {line_no} isn't valid")
continue
try:
(animeSn, videoSn, date, popular) = row
(animeSn, videoSn, popular) = map(int, (animeSn, videoSn, popular))
VideoSn(video_sn=videoSn, anime_sn=animeSn).save()
Popular(video_sn=videoSn, date=date, popular=popular).save()
logger.debug(f"insert line {line_no} ({animeSn}, {date}) = ({popular})")
except (RuntimeError, sqlite3.Error) as e:
logger.error(f"fail to insert line {line_no}:")
logger.error(e, exc_info=1)
logger.info(f"done.")
if __name__ == "__main__":
main()