Commit 997e6ca2 by zhaoyanchao

修改目录

parent 154f1c70
# - coding: utf-8 -
import requests
import codecs
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
file_path = r'cateword_but_no_hit.txt'
user_profile_file = r'user_brand_favor.txt'
brand_cache = {}
user_brand_favor_str_cache = {}
class FavorBrands:
def __init__(self):
self.top5_brand_ids = ''
self.top5_brand_names = ''
self.other_brand_ids = ''
self.other_brand_names = ''
def trans_brand_id_2_name(brand_id):
if brand_id in brand_cache:
return brand_cache[brand_id]
solr_brand_query_url = 'http://solr3.secoo-inc.com:15000/solr/search_product/select?wt=json&fl=brandCName&rows=1&q=brandId:'
try:
res = requests.get(solr_brand_query_url + str(brand_id))
result = res.json()
return result.get('response')['docs'][0]['brandCName']
except Exception as err:
print(err)
def get_favor_brands_str_from_api(device_id):
res = requests.get('http://rcmdprofile.secoolocal.com/soprofile?orginFeild=brand_favor&deviceId='+device_id)
result = res.json()
brand_favor = result.get('brand_favor')
return brand_favor
def get_favor_brands_str_from_cache(device_id):
if device_id in user_brand_favor_str_cache:
return user_brand_favor_str_cache[device_id]
return ''
def get_favor_brands(device_id):
favor_brands = FavorBrands()
try:
# brand_favor = get_favor_brands_str_from_api(device_id)
brand_favor = get_favor_brands_str_from_cache(device_id)
brand_arr = brand_favor.split('%')
brand_favor_len = len(brand_arr)
min_len = brand_favor_len if brand_favor_len < 5 else 5
for i in range(min_len):
brand_tup = brand_arr[i]
brand_id = brand_tup.split(',')[0]
favor_brands.top5_brand_ids += str(brand_id)+ ", "
favor_brands.top5_brand_names += trans_brand_id_2_name(brand_id) +", "
if brand_favor_len <= 5:
return favor_brands
for brand_tup in brand_arr[5:]:
brand_id = brand_tup.split(',')[0]
favor_brands.other_brand_ids += str(brand_id)+", "
favor_brands.other_brand_names += trans_brand_id_2_name(brand_id) + ", "
except Exception as err:
print(err)
return favor_brands
def init_user_brand_favor_str_cahce():
with open(user_profile_file,'r') as f:
for line in f:
str_arr = line.split('\t')
if len(str_arr) != 2:
continue
device_id = str_arr[0]
brand_favor = str_arr[1].strip()
user_brand_favor_str_cache[device_id] = brand_favor
def get_user_brand_cache_detail():
with codecs.open(file_path,'r','utf-8') as f:
for line in f:
str_arr = line.split('\t')
if len(str_arr) != 2:
continue
device_id = str_arr[0]
search_word = str_arr[1].strip()
favor_brands = get_favor_brands(device_id)
print(device_id + '\t' + search_word + '\t' + favor_brands.top5_brand_ids + '\t'
+ favor_brands.top5_brand_names + '\t' + favor_brands.other_brand_ids + '\t'
+ favor_brands.other_brand_names)
def main():
init_user_brand_favor_str_cahce()
get_user_brand_cache_detail()
if __name__ == "__main__":
main()
......@@ -26,7 +26,7 @@ def trans_brand_id_2_name(brand_id):
try:
res = requests.get(solr_brand_query_url + str(brand_id))
result = res.json()
print(type(result.get('response')['docs'][0]['brandCName']))
return result.get('response')['docs'][0]['brandCName']
except Exception as err:
print(err)
......@@ -78,7 +78,7 @@ def init_user_brand_favor_str_cahce():
continue
device_id = str_arr[0]
brand_favor = str_arr[1]
brand_favor = str_arr[1].strip()
user_brand_favor_str_cache[device_id] = brand_favor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment