forked from cnaafhvk/roc_oct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roc_oct.py
executable file
·220 lines (182 loc) · 7.57 KB
/
roc_oct.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/home/longen/.pyenv/shims/python
# -*- coding:utf-8 -*-
"""
暂时废弃
"""
import os
import json
import bs4
import logging
import datetime
from argparse import ArgumentParser
from urllib.parse import urlencode, urljoin
from requests import get
from logging import handlers
from utils import retry_wrapper
project_path = os.path.dirname(__file__)
logger = logging.getLogger("roc_oct")
file_handler = handlers.RotatingFileHandler(
os.path.join(project_path, "roc_oct.log"), maxBytes=1024*1024*10, backupCount=5)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
get = retry_wrapper(5, error_handler=lambda *args, **kwargs: logger.debug("error in requests.get: %s"%str(args)))(get)
oct_adr = "http://octopus.app.jinanlongen.com/store_tasks/tb_new_arrival_search"
oct_params = dict(tuple(line.strip().split(":", 1)) for line in """utf8:✓
upload_date:计划上新时间
planned_upload_step_end_at:
store_id:
status:请选择
cycle_grade:请选择
store_task_id:""".split("\n"))
oct_headers = {
"Cookie": "_session_id=47d56eeb9d0b817e0a9766cae411d81b"
}
roc_adr = "http://roc.app.jinanlongen.com/explore_tasks"
roc_params = dict(tuple(line.strip().split(":", 1)) for line in """utf8:✓
q[id]:
q[source_site_id]:1
q[gender_id]:2
q[brand_id]:1
q[taxon_id]:50
q[created_at_start]:
q[created_at_end]:
q[last_exec_at_start]:
q[last_exec_at_end]:""".split("\n"))
roc_headers = {
"Cookie": "_roc_session=UGhURXc1c2YvdVpnL3dOVWdVU016S0ZMSjJoMk1UeVpjK2xibVF3WTBFc2hCTUhueDZZTGhUeTdnOHFXMEJpWjQzV3R2MkN4ZlBObmNTKzU3b1BFdCtGa2J3QlN3M3NkMnVYT1NSL0U3L0ZSM0VacWtPU3BFaHNlZzJkVjA4SlJwaXlSN2pmT0pGcC9hNVdRWkp6Z0VXVVpldytjRXB0KzFTdEl6ZmNjalRYVVF2alZvaVl4YmlCMGk0YThPdEN0LS1vdFNCUTZveVptNVNISTRueTVKcUJRPT0%3D--fb66482f7ba276eadfa51b454e4be5dcfe385ee4"
}
oct_total_adr = "http://192.168.200.94:10000/store_tasks.json"
taxon_adr = "http://roc.app.jinanlongen.com/taxons/%s/children.json"
def get_task_ids(date):
oct_params["planned_upload_step_end_at"] = "%s-%s-%s"%(date[:4], date[4:6], date[6:])
first_url = "%s?%s"%(oct_adr, urlencode(oct_params))
links = []
task_ids = []
links.append(first_url)
while links:
url = links.pop()
resp = get(url, headers=oct_headers)
soup = bs4.BeautifulSoup(resp.text, "lxml")
tbody = soup.find(id="body")
a = tbody.select("tr td a")
for _a in a:
text = _a.text
if text.isdigit():
task_ids.append(text)
next = soup.select(".next a")
if next:
links.append(urljoin(url, next.pop().get("href")))
return task_ids
def get_jd_task_ids(date=""):
"""应该是废弃了"""
return json.loads(get("http://jdnew.net.jinanlongen.com/gettaskontime.ashx?startDate=%s&endDate=%s"%(date, date)).text)
def get_total_tasks():
try:
return json.loads(get(url=oct_total_adr).text)
except Exception:
return []
def get_gender_brand_site_taxon():
resp = get(roc_adr, headers=roc_headers)
soup = bs4.BeautifulSoup(resp.text, "lxml")
taxons, genders, sites, brands = dict(), dict(), dict(), dict()
for option in soup.select('select[class="taxon-select input-sm search1"] option'):
value = option.get("value")
if value == "0":
continue
taxons[option.text] = (int(value), dict())
for option in soup.select('#q_gender_id option'):
value = option.get("value")
if not value:
continue
genders[option.text] = int(value)
for option in soup.select('#q_source_site_id option'):
value = option.get("value")
if not value:
continue
sites[option.text] = int(value)
for option in soup.select('#q_brand_id option'):
value = option.get("value")
if not value:
continue
brands[option.text] = int(value)
return taxons, genders, sites, brands
def get_crawl_tasks(taxons, genders, sites, brands, **kwargs):
if not kwargs.get("taxon", ""):
return []
ts = kwargs.pop("taxon", "").split("|")
source_site_name = kwargs.pop("source_site_name", "")
brand = kwargs.pop("brand", "")
gender = kwargs.pop("gender", "")
taxon_code = "0"
children = taxons
while True:
try:
taxon_code, children = children[ts.pop(0)]
if not children:
resp = get(taxon_adr%taxon_code, headers=roc_headers)
if resp.status_code == 200:
for child in json.loads(resp.text):
children[child["name"]] = child["id"], dict()
else:
break
except (KeyError, IndexError):
break
roc_params["q[source_site_id]"] = source_site_name and sites[source_site_name]
roc_params["q[gender_id]"] = gender and genders[gender]
roc_params["q[brand_id]"] = brand and brands[brand]
roc_params["q[taxon_id]"] = taxon_code
resp = get(url="%s?%s"%(roc_adr, urlencode(roc_params)), headers=roc_headers)
soup = bs4.BeautifulSoup(resp.text, "lxml")
trs = soup.select("tbody tr")
crawl_tasks = []
for tr in trs:
tds = tr.select("td")
crawl_tasks.append((tds[1].text, tds[2].text, tds[3].text, tds[4].text, tds[5].text, tds[9].text))
logger.debug("Oct task id: %s, Roc task count: %s. "%(kwargs["store_task_id"], len(crawl_tasks)))
return crawl_tasks
def start():
parser = ArgumentParser()
parser.add_argument("-t", "--total", action="store_true")
parser.add_argument("-i", "--id", required=True)
logger.debug("Start to get total tasks. ")
total_tasks = get_total_tasks()
args = parser.parse_args()
if not args.total:
logger.debug("Start to get task ids of %s. "%args.id)
task_ids = get_task_ids(args.id)
tasks = [task for task in total_tasks if str(task["store_task_id"]) in task_ids]
else:
tasks = total_tasks
logger.debug("Start to get taxons, genders, sites, brands. ")
taxons, genders, sites, brands = get_gender_brand_site_taxon()
crawl_tasks = dict()
unregist_tasks = list()
logger.debug("Enrich crawl tasks and unregist tasks. ")
for task in tasks:
find_crawl_tasks = get_crawl_tasks(taxons, genders, sites, brands, **task)
if find_crawl_tasks:
for crawl_task in find_crawl_tasks:
crawl_tasks[crawl_task[0]] = crawl_task
else:
logger.debug("Oct task id: %s found crawl task failed. "%task["store_task_id"])
unregist_tasks.append(task)
crawl_tasks_file = open("crawl_tasks_%s.csv"%args.id, "w")
unregist_tasks_file = open("unregist_tasks_%s.csv"%args.id, "w")
crawl_tasks_file.write("ROC任务ID,来源网站名称,分类名称,性别名称,品牌名称,最后执行时间,最后执行时间差/天\n")
for crawl_task in crawl_tasks.values():
crawl_tasks_file.write((",".join(crawl_task)))
try:
days = ",%s\n" % (datetime.datetime.now() - datetime.datetime.strptime(crawl_task[-1], "%Y/%m/%d %H:%M:%S")).days
except ValueError:
days = ",%s\n" % 0
crawl_tasks_file.write(days)
unregist_tasks_file.write("编号,来源网站,品牌,性别,分类,店铺/任务id\n")
for index, unregist_task in enumerate(unregist_tasks):
unregist_tasks_file.write(
"%s,%s,%s,%s,%s,%s/%s\n"%(index+1, unregist_task["source_site_name"],
unregist_task["brand"], unregist_task["gender"], unregist_task["taxon"],
unregist_task["store_name"], unregist_task["store_task_id"]))
crawl_tasks_file.close()
unregist_tasks_file.close()
if __name__ == "__main__":
print(get_task_ids("2017-11-29"))