Repository navigation
Expand file tree
/
Copy path110_api_pidvid_getitemcat.py
More file actions
177 lines (156 loc) · 5.55 KB
/
Copy path110_api_pidvid_getitemcat.py
File metadata and controls
177 lines (156 loc) · 5.55 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
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
#!/usr/bin/python
# encoding:utf8
# All competitors
# Copyright(c)2012 vsu@opensuse.org
from __future__ import print_function
import sys,os,MySQLdb
import urllib2,urllib,time,re,socket,cookielib,random
import json
from BeautifulSoup import BeautifulSoup
import multiprocessing
import top.api
#import yaml
def getDate():
date=time.strftime("%Y-%m-%d",time.localtime())
return date
def getTask(host,port,user,password,database,tablename,itemstage = 1):
db=MySQLdb.connect(host=host,user=user,passwd=password,db=database,charset="utf8")
dbconn=db.cursor()
try:
queryTask = "SELECT `dbname`,r_table,w_table,keyword,rule FROM %s.%s WHERE taskid = '%s'"
print(queryTask % (database,tablename,itemstage))
dbconn.execute(queryTask % (database,tablename,itemstage))
result = dbconn.fetchall()
db.commit()
except:
result = []
dbconn.close()
return result
def getSubTask(host,port,user,password,database,read_table,query_rule):
# keyword,pagenumber = 1,ratesum = "",location = "",cat = "",brand = "",ppath = "",sort = "sale-desc")
db=MySQLdb.connect(host=host,port=port,user=user,passwd=password,db=database,charset="utf8")
dbconn=db.cursor()
try:
queryTask = "SELECT cid FROM %s.%s %s" % (database,read_table,query_rule)
print(queryTask)
dbconn.execute(queryTask)
result = dbconn.fetchall()
db.commit()
except:
result = []
print("Error!SubTask")
dbconn.close()
return result
def getPidvid(parent_cid,appkey,secret,level):
## url = "http://localhost"
## port = "80"
## appkey = "21786895"
## secret = "934fe5351c6d2c2a506aa97fdbd445b1"
req=top.api.ItemcatsGetRequest()
req.set_app_info(top.appinfo(appkey,secret))
req.parent_cid=parent_cid
req.fields="cid,parent_cid,name,is_parent"
try:
resp= req.getResponse()
print("Fetched")
dataset = resp.get('itemcats_get_response').get('item_cats').get('item_cat')
except Exception,e:
print('resp error:',e)
dataset = []
data = []
for i in range(0,len(dataset)):
cid = dataset[i].get('cid')
name = dataset[i].get('name')
parent_cid = dataset[i].get('parent_cid')
is_parent = dataset[i].get('is_parent')
datatemp = (cid,name,parent_cid,is_parent,level+1)
data.append(datatemp)
return data
def api_pidvid_data(info):
host = info[0]
port = info[1]
user = info[2]
password = info[3]
database = info[4]
tbname = info[5]
appkey = info[6]
secret = info[7]
cid = info[8]
level = info[9]
try:
pidviddata = getPidvid(cid,appkey,secret,level)
if pidviddata == []:
is_end = 1
else:
is_end = 0
db=MySQLdb.connect(host=host,port=port,user=user,passwd=password,db=database,charset="utf8")
dbconn=db.cursor()
ItemDataInsert = "REPLACE INTO "+str(tbname)+" (cid,name,parent_cid,is_parent,level) VALUES (%s,%s,%s,%s,%s)"
dbconn.executemany(ItemDataInsert,pidviddata)
db.commit()
db.close()
except Exception,e:
print(e)
print("No Attrib:",str(cid))
return is_end
def initialTable(host,port,user,password,database,tbname):
initial_str = '''CREATE TABLE IF NOT EXISTS '''+str(tbname)+'''(cid bigint,name varchar(50),parent_cid bigint,is_parent varchar(10),level int(1),INDEX `cid` (cid),INDEX `level` (level)) COLLATE='utf8_general_ci' ENGINE=InnoDB;'''
db=MySQLdb.connect(host=host,port=port,user=user,passwd=password,db=database,charset="utf8")
dbconn=db.cursor()
try:
dbconn.execute(initial_str)
result = dbconn.fetchall()
db.commit()
except Exception,e:
print(e)
dbconn.close()
return
def do_api_pidvid(host,port,user,password,database,write_table,appkey,secret,level):
query_rule = '''where is_parent = '1' and level = %s and cid not in (select parent_cid from %s.%s) group by cid''' % (level,database,write_table)
taskList = getSubTask(host,port,user,password,database,write_table,query_rule)
## multiTask(taskList,host,user,password,database,write_table,appkey,secret)
tasktemp = taskList
z = len(tasktemp)
print("Total_CID:",str(z))
if z==0:
print("yes")
z = 1
tasktemp = ((0,),)
for j in range(0,z):
ptask = (host,port,user,password,database,write_table,appkey,secret,tasktemp[j][0],level)
is_end = api_pidvid_data(ptask)
def main():
## conf_file = open('/etc/crawl/item_conf')
## conf = yaml.load(conf_file)
## host = conf['host']
## user = conf['user']
## password = conf['password']
## database = conf['database']
## tablename = conf['tablename']
host = '192.168.1.110'
user = 'kettle'
port = 33060
password = 'root'
database = 'etc_calc'
tablename = 'dim_itemcat_r20150401'
## appkey = "21786895"
## secret = "934fe5351c6d2c2a506aa97fdbd445b1"
appkey = "23113820"
secret = "dd3e0ad2b2cff904bfccfc823aba7ad4"
try:
itemstage = sys.argv[1]
except:
itemstage = 6
projectlist = (0,1,2,3,4,5,6)
for level in projectlist:
print(level)
initialTable(host,port,user,password,database,tablename)
do_api_pidvid(host,port,user,password,database,tablename,appkey,secret,level)
if __name__ == "__main__":
main()
## cid = '120878006'
## appkey = "21786895"
## secret = "934fe5351c6d2c2a506aa97fdbd445b1"
## jim = getPidvid(cid,appkey,secret)
## for x in jim:
## print(x)