-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUserInfo.py
More file actions
118 lines (99 loc) · 4.64 KB
/
UserInfo.py
File metadata and controls
118 lines (99 loc) · 4.64 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
reload(sys)
# if(sys.platform == 'win32'):
# encoding = "gb2312"
# else:
# encoding = "utf8"
sys.setdefaultencoding("utf8")
import time
from Global import DBInfo
from _mysql_exceptions import DatabaseError
from DBConnection import DBConnection
create_tbl = 'create table weibo_user(id varchar(20), screen_name varchar(50), gender varchar(4), verify_type varchar(20), labels varchar(200), edu_label varchar(50), career_label varchar(50), location varchar(20), province varchar(10), city varchar(10), description varchar(500), follower_count int, friends_count int, weibo_count int, url varchar(100), PRIMARY KEY (id))'
class UserInfo():
def __init__(self):
self.id = ''
self.screen_name = ''
self.gender = ''
self.verify_type =''
self.labels = ''
self.edu_label = ''
self.career_label = ''
self.province = ''
self.city = ''
self.location = ''
self.description = ''
self.follower_count = 0
self.friends_count = 0
self.weibo_count = 0
self.url = ''
def insert_table(self):
try:
#sql = "insert into weibo_user values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d,%d,'%s')" % \
#(self.id, self.screen_name, self.gender, self.verify_type, self.labels, self.edu_label, self.career_label,
#self.location, self.province, self.city, self.description, int(self.follower_count),
#int(self.friends_count), int(self.weibo_count), self.url)
sql = "insert into weibo_user values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
query_sql = "select * from weibo_user where id = %s"
delete_sql = "delete from weibo_user where id = %s"
db = DBConnection()
conn = db.get_conn()
conn.select_db(DBInfo.db)
cur = db.get_cursor()
#首先检查数据库中是否已经存在, 如果存在则首先删除,然后插入新的微博用户
count = cur.execute(query_sql, (self.id,))
if(count != 0):
cur.execute(delete_sql, (self.id,))
ret = cur.execute(sql, (self.id, self.screen_name, self.gender, self.verify_type, \
self.labels, self.edu_label, self.career_label, self.location, \
self.province, self.city, self.description, self.follower_count, \
self.friends_count, self.weibo_count, self.url))
conn.commit()
db.close()
if(ret == 1):
return True
return False
except DatabaseError, e:
print "insert error", e
def printIt(self):
print "用户信息:"
print "ID:", self.id
print "昵称:", self.screen_name
print "认证类型:", self.verify_type
print "性别:", self.gender
print "标签:", self.labels
print "教育信息:", self.edu_label
print "职业信息:", self.career_label
print "省份:", self.province
print "城市:", self.city
print "所在地:", self.location
print "简介:", self.description
print "粉丝:", self.follower_count
print "关注:", self.friends_count
print "微博:", self.weibo_count
print "博客:", self.url
def writeFile(self, filename):
wfile = open(filename, 'a')
try:
wfile.write(self.id + str(chr(94)))
wfile.write(self.screen_name + str(chr(94)))
wfile.write(self.verify_type + str(chr(94)))
wfile.write(self.gender + str(chr(94)))
wfile.write(self.labels + str(chr(94)))
wfile.write(self.edu_label + str(chr(94)))
wfile.write(self.career_label + str(chr(94)))
wfile.write(self.province + str(chr(94)))
wfile.write(self.city + str(chr(94)))
wfile.write(self.location + str(chr(94)))
wfile.write(self.description + str(chr(94)))
wfile.write(str(self.follower_count) + str(chr(94)))
wfile.write(str(self.friends_count) + str(chr(94)))
wfile.write(str(self.weibo_count) + str(chr(94)))
wfile.write(self.url)
wfile.write('\n')
except IOError, e:
print 'file error:', e
finally:
wfile.close()