-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-db-from-newsdiff-backup.py
executable file
·107 lines (86 loc) · 3.27 KB
/
import-db-from-newsdiff-backup.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
#encoding=utf-8
import sys
import json
import mysql.connector
import getopt
import ast
set_ids = set()
is_dryrun = False
action = ''
opts, args = getopt.getopt(sys.argv[1:], '', ["action=", 'file=', 'dryrun', 'insert-diff'])
for opt in opts:
if opt[0] == '--action':
action = opt[1]
elif opt[0] == '--file':
filename = opt[1]
elif opt[0] == '--dryrun':
is_dryrun = True
elif opt[0] == '--insert-diff':
is_insert_diff = True
if len(action) == 0:
sys.stderr.write("--action is required\n");
if action == 'import':
f = open(filename, 'r')
counter = 0
total = 0
title = ""
content = ""
obj = None
cnx = mysql.connector.connect(host='127.0.0.1', user='root', password='', database='newsdiffreport', charset='utf8')
cursor = cnx.cursor()
is_exists = False
update_count = 0
insert_count = 0
for line in f:
line = line.strip('\n')
counter = counter+1
if total % 1000 == 0 and counter % 3 == 0:
print("count: %s, insert_count: %s, update_count: %s" % (total, insert_count, update_count))
if counter == 1:
obj = None
obj = json.loads(line)
id = obj['id']
if id in set_ids:
is_exists = True
else:
set_ids.add(id)
is_exists = False
continue
if counter == 2:
line = line.strip('"').replace('\\n', '\n').replace('\\r', '\r').replace('\\/', '/')
title = line
continue
if counter == 3:
total = total + 1
line = line.strip('"').replace('\\n', '\n').replace('\\r', '\r')
line = line.strip('"').replace('\\n', '\n').replace('\\r', '\r').replace('\\/', '/')
content = line
counter = 0
if not is_exists:
sql = "insert into news(id, url, normalized_id, normalized_crc32, source, created_at, last_fetch_at, last_changed_at, error_count) values(%s, %s, %s, %s, %s, %s, %s, %s, %s);"
data = (obj['id'], obj['url'], obj['normalized_id'], obj['normalized_crc32'], obj['source'], obj['created_at'], obj['last_fetch_at'], obj['last_changed_at'], obj['error_count'])
if is_dryrun:
print(sql)
print(data)
else:
cursor.execute(sql, data)
sql = "insert into news_info(news_id, time, title, body) values(%s, %s, %s, %s);"
data = (obj['id'], obj['version'], title, content)
cursor.execute(sql, data)
insert_count = insert_count + 1
cnx.commit()
else:
if is_insert_diff:
if is_dryrun:
print('id exists, insert diff')
else:
sql = "insert into news_info(news_id, time, title, body) values(%s, %s, %s, %s);"
data = (obj['id'], obj['version'], title, content)
cursor.execute(sql, data)
insert_count = insert_count + 1
cnx.commit()
print("Finished: " + str(total))
f.close()
cursor.close()
cnx.close()