forked from Smerity/cc-mrjob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_into_db.py
51 lines (41 loc) · 1.62 KB
/
import_into_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
import MySQLdb, csv, os
from collections import Counter
db = MySQLdb.connect(host=os.environ['db_host'],
user=os.environ['db_user'],
passwd=os.environ['db_password'],
db=os.environ['db_name'],
use_unicode=True,
charset='utf8',
)
def getPrefix(number, db):
number = number.strip('+')
q = "SELECT zones_id, prefix FROM prefixes, zones WHERE '"+ number + "' LIKE CONCAT(prefix,'%') and prefixes.retired is null ORDER BY length(prefix) DESC LIMIT 1"
c = db.cursor()
c.execute(q)
result = c.fetchall()
if (len(result) > 0):
return result[0]
return (None, None)
"""
"""
def insertNumber(number, zone_id, prefix, db):
number = number.strip('+')
q = "INSERT INTO numbers_pool (`number`, `lastUsedTime`, `zones_id`, `prefixes_prefix`) VALUES (%s, NOW(), %s, %s);"
c = db.cursor()
c.execute(q, [number, int(zone_id), prefix])
db.commit()
files = 'output_from_s3'
numbers = {}
for root, dirnames, filenames in os.walk(files):
for filename in filenames:
with open(os.path.join(root, filename), 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter="\t", quotechar='"')
for row in reader:
numbers[row[0]] = row[0]
with open('output_for_mysql_load.txt', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter="\t", quotechar='"')
for number in numbers:
zone_id, prefix = getPrefix(number, db)
if (zone_id is not None and prefix is not None):
writer.writerow((number.strip('+'), zone_id, prefix))
insertNumber(number, zone_id, prefix, db)