-
Notifications
You must be signed in to change notification settings - Fork 85
/
reviews.py
77 lines (67 loc) · 2.43 KB
/
reviews.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
#coding:utf-8
import json
import codecs
from log import getLogger
from exception import NoDatabaseFound
logger = getLogger(__name__)
class Review(object):
def __init__(self,userName,userId,userUrl,userImg,userLevel,userVip,
star,score,reviewId,reviewUrl,reviewTime,reviewShop,reviewShopId,
reviewPics,reviewWords,actions
):
self.userName = userName
self.userId = userId
self.userUrl = userUrl
self.userImg = userImg
self.userLevel = userLevel
self.userVip = userVip
self.star = star
self.score = score
self.id = reviewId
self.url = reviewUrl
self.reviewTime = reviewTime
self.shopName = reviewShop
self.shopId = reviewShopId
self.imgs = reviewPics
self.actions = actions
self.content = reviewWords
@property
def data(self):
data = {
u'点评ID': self.id,
u'点评链接': self.url,
u'点评用户': self.userName,
u'用户ID': self.userId,
u'用户主页': self.userUrl,
u'用户头像': self.userImg,
u'用户等级': self.userLevel,
u'VIP用户': self.userVip,
u'点评商铺': self.shopName,
u'商铺ID': self.shopId,
u'点评星级': self.star,
u'用户评价': self.score,
u'点评时间': self.reviewTime,
u'点评图片': self.imgs,
u'评论事件': self.actions,
u'点评内容': self.content,
}
return data
def save(self,db=None,tname=None):
self.tname = tname if tname else db.table
if db is None:
raise NoDatabaseFound
if db.select({'点评ID':{'=':self.id}},tname=tname):
return
else:
try:
db.save(self.data,tname=tname)
except Exception as e:
logger.error(f'保存点评:{self.id}出错 - {e.__class__.__name__}:{e}')
else:
logger.info(f'点评 ID:{self.id},店铺:{self.shopName} 保存成功,数据表:{self.tname}')
def write(self,path=None, mode='a'):
if path:
with codecs.open(path,mode,encoding='utf-8') as f:
f.write(str(self.data))
f.write('\n')
logger.info(f'点评 ID:{self.id},店铺:{self.shopName} 保存成功,文件路径:{path}')