-
Notifications
You must be signed in to change notification settings - Fork 76
/
repo_mysql.py
105 lines (86 loc) · 3.64 KB
/
repo_mysql.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
import MySQLdb
from settings import db_connet_info as connect_info
def _sql_log_fl(rid, login_id, n_record):
return "INSERT INTO stat_log_friends (rid, login_id, n_record) VALUES ('%s', '%s', %d)" % (rid, login_id, n_record)
def _sql_fl(record, rid):
val_fl = ','.join(["('%s', '%s')" % (rid, item[0]) for item in record if item[0] != rid])
return "INSERT INTO friends (rid1, rid2) VALUES %s" % val_fl
def _sql_name(record):
val_name = ','.join(["('%s', '%s')" % item for item in record])
return "INSERT INTO profile (rid, name) VALUES %s" % val_name
class repo_mysql:
def __init__(self):
self.conn = MySQLdb.connect(**connect_info)
self.cur = self.conn.cursor()
def __del__(self):
self.cur.close()
self.conn.close()
def save_fl(self, login_id, rid, fl_record):
"""save record and return rows affected.save nothing if empty.
return None if input error"""
n_name = 0
try:
if len(fl_record):
n_fl = self.cur.execute(_sql_fl(fl_record, rid))
n_name = self.cur.executemany("INSERT INTO profile (rid, name) VALUES (%s, %s) ON DUPLICATE KEY UPDATE rid=VALUES(rid)", fl_record)
self.cur.execute(_sql_log_fl(rid, login_id, len(fl_record)))
except Exception as e:
print 'Error ID: %s' % rid
print e
else:
self.conn.commit()
return n_name
def get_fl_searched(self):
self.cur.execute("SELECT rid FROM stat_log_friends")
return {item[0] for item in self.cur.fetchall()}
def get_fl(self, rid):
self.cur.execute("SELECT rid2 FROM friends where rid1='%s'" % rid)
return {item[0] for item in self.cur.fetchall()}
def _sql_status(self,record,rid=None):
pageStyle='status'
if record == {}:
return []
sqls=[]
for statusId,stat in record.items():
val_stat="statusId='{}'".format(statusId)
for tag,value in stat.items():
if value is not None:
value=value.replace("\\","\\\\").replace("'","\\'").rstrip('\\')#format ' and \
val_stat += ",{}='{}'".format(tag,value)
sqls.append("insert into {} set {}".format(self.table_name[pageStyle],val_stat))
return sqls
def _sql_profile(self,record,rid=None):
pageStyle='profile'
if len(record) == 0:
return []
pf_map=get_cfg_dict('profile_map',has_default=False)
pf_ignore=pf_map.pop('ignore').split(',')
#construct sql
pfs="renrenId1='{}'".format(rid)
for k,v in record.items():
if k in pf_map.keys():
pfs += ",{}='{}'".format(pf_map[k],v)
elif k in pf_map.values():
pfs += ",{}='{}'".format(k,v)
elif k in pf_ignore:
#print('ignore {}'.format(k))
pass
else:
self.tag_exceed(rid,k,v)
sql_pf="insert into {} set {}".format(self.table_name[pageStyle],pfs)
return [sql_pf]
def tag_exceed(self,rid,k,v):
print('pf tag exceed. tag={},renrenId={},value={}'.format(k,rid,v))
if __name__ == '__main__':
from downloader import renren
test_cookie = raw_input('Input cookie(document.cookie): ')
rr = renren(test_cookie)
rid = rr.renrenId()
target_id = '336283681'
print rid
record = rr.friendList(target_id)
print '%d got' % len(record)
repo = repo_mysql()
print repo.save_fl(rid, target_id, record)
print 'friends of rid: %s' % len(repo.get_fl(target_id))
# print 'searched: %s' % ','.join(repo.get_fl_searched())