Commit af05f4f6 authored by 王浩's avatar 王浩

Merge branch 'haow/dev' into 'test'

fix code

See merge request !88
parents 2b919606 7fd8ac80
......@@ -20,7 +20,7 @@ class CreatePictorial(BaseView):
"""
画报爬取接口
"""
user_id_start = 241806255 # end 241757306
user_id_start = 241757306 # end 241806255
del_cache_keys = []
def del_cache(self):
......@@ -84,6 +84,7 @@ class CreatePictorial(BaseView):
ret = []
for obj in comments:
obj['from_id'] = from_id
obj['content'] = obj.get('comment')
reply = obj.pop('reply', None)
if not reply:
ret.append(obj)
......@@ -97,27 +98,93 @@ class CreatePictorial(BaseView):
ret.append(info)
return ret
def create_topic(self, topics, platform):
for topic in topics:
topic_comments = topic.pop('comments', None)
images = topic.pop('image')
topic['images'] = self.image_info(images)
topic['user_id'] = self.get_user_id(id_=topic.get('id'), platform=platform)
error, topic_obj = self.call_rpc('venus/community/crawl/topic', data=topic, platform=platform, pictorial_id=None)
if error:
self.del_cache()
return self.error(error=error)
if not topic_comments:
continue
from_id = topic.get('id')
if platform == GRAP_PLATFORM.XIAOHONGSHU:
topic_comments = self.revise_comments(topic_comments, from_id)
for obj in topic_comments:
obj['user_id'] = self.get_user_id(id_=obj.get('id'), platform=platform)
error, _ = self.call_rpc('venus/community/crawl/replys', data=topic_comments, platform=platform, topic_id=topic_obj.get('id'))
if error:
self.del_cache()
return self.error(error=error)
self.del_cache()
return
def create_pictorial(self, pictorial, platform):
topics = []
pictorial_id = None
if pictorial:
pictorial_comments = pictorial.pop('comments', None)
images = self.image_info(pictorial.pop('image'))
index = 0
for obj in images:
index += 1
topics.append({
'id': pictorial.get('id') + str(index),
'content': pictorial.get('content'),
'image': obj,
'create_time': pictorial.get('create_time'),
'user_id': self.get_user_id(id_=obj.get('url'), platform=platform)
})
pictorial['user_id'] = self.get_user_id(id_=pictorial.get('id'), platform=platform)
pictorial['description'] = pictorial.get('content')
# 榜单名称取爬取内容的前20字符
index_end = 20
if len(pictorial.get('content')) < index_end:
index_end = len(pictorial.get('content')) - 1
pictorial['name'] = pictorial.get('content')[:index_end]
error, pictorial_obj = self.call_rpc('venus/community/crawl/pictorial', data=pictorial, platform=platform)
if error:
self.del_cache()
return error, None
pictorial_id = pictorial_obj.get('id')
if topics:
for obj in topics:
error, id_ = self.call_rpc('venus/community/crawl/topic', data=obj, platform=platform, pictorial_id=pictorial_id)
if error:
self.del_cache()
return error, None
if pictorial_comments:
if platform == GRAP_PLATFORM.XIAOHONGSHU:
pictorial_comments = self.revise_comments(pictorial_comments, pictorial.get('id'))
for obj in pictorial_comments:
obj['user_id'] = self.get_user_id(id_=obj.get('id'), platform=platform)
obj.pop('user')
error, _ = self.call_rpc('venus/community/crawl/replys', data=pictorial_comments, platform=platform, pictorial_id=pictorial_id)
if error:
self.del_cache()
return error, None
self.del_cache()
return None, None
def post(self, request):
pictorial = json.loads(request.POST.get('pictorial', '{}'))
topics = json.loads(request.POST.get('topics', '[]'))
platform = int(request.POST.get('platform', GRAP_PLATFORM.XIAOHONGSHU))
is_pictorial = request.POST.get('is_pictorial', None)
if not topics:
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if pictorial:
if not pictorial.get('id'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if not pictorial.get('name'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if not pictorial.get('description'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if not pictorial.get('create_time'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if not pictorial.get('user'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
if not pictorial.get('user').get('id'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
for topic in topics:
if not topic.get('id'):
......@@ -131,41 +198,16 @@ class CreatePictorial(BaseView):
if not topic.get('user').get('id'):
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
pictorial_id = None
if pictorial:
pictorial_comments = pictorial.pop('comments', None)
pictorial['user_id'] = self.get_user_id(id_=pictorial.get('id'), platform=platform)
error, pictorial_obj = self.call_rpc('venus/community/crawl/pictorial', data=pictorial, platform=platform)
if is_pictorial:
is_pictorial = int(is_pictorial)
if is_pictorial:
for obj in topics:
error, _ = self.create_pictorial(pictorial=obj, platform=platform)
if error:
self.del_cache()
return self.error(error=error)
pictorial_id = pictorial_obj.get('id')
if pictorial_comments:
error, _ = self.call_rpc('venus/community/crawl/replys', data=pictorial_comments, platform=platform, pictorial_id=pictorial_id)
if error:
self.del_cache()
return self.error(error=error)
for topic in topics:
topic_comments = topic.pop('comments', None)
images = topic.pop('image')
topic['images'] = self.image_info(images)
topic['user_id'] = self.get_user_id(id_=topic.get('id'), platform=platform)
error, topic_obj = self.call_rpc('venus/community/crawl/topic', data=topic, platform=platform, pictorial_id=pictorial_id)
if error:
self.del_cache()
return self.error(error=error)
if not topic_comments:
continue
from_id = topic.get('id')
for obj in topic_comments:
if platform == GRAP_PLATFORM.XIAOHONGSHU:
topic_comments = self.revise_comments(topic_comments, from_id)
obj['user_id'] = self.get_user_id(id_=obj.get('id'), platform=platform)
error, _ = self.call_rpc('venus/community/crawl/replys', data=topic_comments, platform=platform, topic_id=topic_obj.get('id'))
else:
error, _ = self.create_topic(topics=topics, platform=platform)
if error:
self.del_cache()
return self.error(error=error)
self.del_cache()
......
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