-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.py
131 lines (99 loc) · 3.64 KB
/
utils.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#-*- coding: utf-8 -*-
import json
import logging
import os
import re
import subprocess
import traceback
import time
import datetime
import redis
import config
from jd_analysis import settings
from sqlhelper import SqlHelper
# 自定义的日志输出
def log(msg, level = logging.DEBUG):
logging.log(level, msg)
print('%s [%s], msg:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), level, msg))
# if level == logging.WARNING or level == logging.ERROR:
# for line in traceback.format_stack():
# print(line.strip())
#
# for line in traceback.format_stack():
# logging.log(level, line.strip())
# 服务器使用,清理端口占用
def kill_ports(ports):
for port in ports:
log('kill %s start' % port)
popen = subprocess.Popen('lsof -i:%s' % port, shell = True, stdout = subprocess.PIPE)
(data, err) = popen.communicate()
log('data:\n%s \nerr:\n%s' % (data, err))
pattern = re.compile(r'\b\d+\b', re.S)
pids = re.findall(pattern, data)
log('pids:%s' % str(pids))
for pid in pids:
if pid != '' and pid != None:
try:
log('pid:%s' % pid)
popen = subprocess.Popen('kill -9 %s' % pid, shell = True, stdout = subprocess.PIPE)
(data, err) = popen.communicate()
log('data:\n%s \nerr:\n%s' % (data, err))
except Exception, e:
log('kill_ports exception:%s' % e)
log('kill %s finish' % port)
time.sleep(1)
# 创建文件夹
def make_dir(dir):
log('make dir:%s' % dir)
if not os.path.exists(dir):
os.makedirs(dir)
def arglist_to_dict(arglist):
"""Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
dict
"""
return dict(x.split('=', 1) for x in arglist)
def get_visiter_ip(request):
if request.META.has_key('HTTP_X_FORWARDED_FOR'):
ip = request.META['HTTP_X_FORWARDED_FOR']
else:
ip = request.META['REMOTE_ADDR']
return ip
def get_save_image_path():
if settings.DEBUG == False:
return '%s/media/images' % settings.BASE_DIR
else:
return '%s/jd/static/images' % settings.BASE_DIR
def get_image_src(filename):
if settings.DEBUG == False:
result = '![](/media/images/%s)' % filename
else:
result = '![](/static/images/%s)' % filename
return result
red = redis.StrictRedis(host = config.redis_host, port = config.redis_part, db = config.redis_db,
password = config.redis_pass)
sql = SqlHelper()
def push_redis(guid, product_id, info, type = 'word', save_to_mysql = True):
data = {
'id': None,
'product_id': product_id,
'info': info,
'type': type,
'guid': guid,
'save_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
}
if save_to_mysql:
sql.insert_json(data, config.analysis_item_table, commit = True)
red.rpush(guid, json.dumps(data))
def create_analysis_table(product_id):
# 创建分析商品评论结果表
command = (
"CREATE TABLE IF NOT EXISTS {} ("
"`id` INT(5) NOT NULL AUTO_INCREMENT," # 自增 id
"`product_id` BIGINT(15) DEFAULT NULL ," # 商品 id
"`info` CHAR(255) DEFAULT NULL," # 分析结果的信息
"`type` CHAR(10) DEFAULT NULL," # 分析结果类型
"`guid` CHAR(40) NOT NULL," # guid
"`save_time` TIMESTAMP NOT NULL," # 分析数据的时间
"PRIMARY KEY(id)"
") ENGINE=InnoDB".format(config.analysis_item_table + '_' + product_id))
sql.create_table(command)