|  | @@ -0,0 +1,151 @@
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +import random
 | 
	
		
			
				|  |  | +import sys
 | 
	
		
			
				|  |  | +import time
 | 
	
		
			
				|  |  | +import uuid
 | 
	
		
			
				|  |  | +from datetime import datetime
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import requests
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from application.common import Feishu
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +sys.path.append(os.getcwd())
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from application.items import VideoItem
 | 
	
		
			
				|  |  | +from application.pipeline import PiaoQuanPipeline
 | 
	
		
			
				|  |  | +from application.common.messageQueue import MQ
 | 
	
		
			
				|  |  | +from application.common.proxies import tunnel_proxies
 | 
	
		
			
				|  |  | +from application.common.log import AliyunLogger
 | 
	
		
			
				|  |  | +from application.common.mysql import MysqlHelper
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class JXXFRecommend(object):
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +    吉祥幸福-欢快吉祥早安祝福
 | 
	
		
			
				|  |  | +    """
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def __init__(self, platform, mode, rule_dict, user_list, env="prod"):
 | 
	
		
			
				|  |  | +        self.limit_flag = False
 | 
	
		
			
				|  |  | +        self.platform = platform
 | 
	
		
			
				|  |  | +        self.mode = mode
 | 
	
		
			
				|  |  | +        self.rule_dict = rule_dict
 | 
	
		
			
				|  |  | +        self.user_list = user_list
 | 
	
		
			
				|  |  | +        self.env = env
 | 
	
		
			
				|  |  | +        self.download_cnt = 0
 | 
	
		
			
				|  |  | +        self.mq = MQ(topic_name="topic_crawler_etl_" + self.env)
 | 
	
		
			
				|  |  | +        self.expire_flag = False
 | 
	
		
			
				|  |  | +        self.aliyun_log = AliyunLogger(mode=self.mode, platform=self.platform)
 | 
	
		
			
				|  |  | +        self.mysql = MysqlHelper(mode=self.mode, platform=self)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def get_recommend_list(self):
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        获取推荐页视频
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        headers = {
 | 
	
		
			
				|  |  | +            'Host': 'api.huanqiwl.top',
 | 
	
		
			
				|  |  | +            'Content-Type': 'application/json',
 | 
	
		
			
				|  |  | +            'Accept-Language': 'zh-cn',
 | 
	
		
			
				|  |  | +            'Accept': '*/*',
 | 
	
		
			
				|  |  | +            'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E217 MicroMessenger/6.8.0(0x16080000) NetType/WIFI Language/en Branch/Br_trunk MiniProgramEnv/Mac',
 | 
	
		
			
				|  |  | +            'Referer': 'https://servicewechat.com/wx6692a24ad2a88bfb/3/page-frame.html'
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        for i in range(10):
 | 
	
		
			
				|  |  | +            time.sleep(random.randint(1, 10))
 | 
	
		
			
				|  |  | +            url = f"https://api.huanqiwl.top/index.php?s=mobile/Video/getList&cid=1&page={i}&api_version=4&appid=wx6692a24ad2a88bfb&version=1.9.5&env_version=release&scene=1053"
 | 
	
		
			
				|  |  | +            payload = {}
 | 
	
		
			
				|  |  | +            response = requests.request("GET", url, headers=headers, data=payload, proxies=tunnel_proxies())
 | 
	
		
			
				|  |  | +            for index, video_obj in enumerate(response.json()['data']['list'], 1):
 | 
	
		
			
				|  |  | +                try:
 | 
	
		
			
				|  |  | +                    self.aliyun_log.logging(
 | 
	
		
			
				|  |  | +                        code="1001", message="扫描到一条视频", data=video_obj
 | 
	
		
			
				|  |  | +                    )
 | 
	
		
			
				|  |  | +                    self.process_video_obj(video_obj)
 | 
	
		
			
				|  |  | +                except Exception as e:
 | 
	
		
			
				|  |  | +                    self.aliyun_log.logging(
 | 
	
		
			
				|  |  | +                        code="3000",
 | 
	
		
			
				|  |  | +                        message="抓取单条视频失败, 该视频位于第{}页第{}条报错原因是{}".format(
 | 
	
		
			
				|  |  | +                            i, index, e
 | 
	
		
			
				|  |  | +                        ),
 | 
	
		
			
				|  |  | +                    )
 | 
	
		
			
				|  |  | +                except Exception as e:
 | 
	
		
			
				|  |  | +                    self.aliyun_log.logging(
 | 
	
		
			
				|  |  | +                        code="3000", message="抓取第{}页的时候失败, 报错原因是{}".format(i, e)
 | 
	
		
			
				|  |  | +                    )
 | 
	
		
			
				|  |  | +            if self.limit_flag:
 | 
	
		
			
				|  |  | +                return
 | 
	
		
			
				|  |  | +            time.sleep(random.randint(5, 10))
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def process_video_obj(self, video_obj):
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        处理视频
 | 
	
		
			
				|  |  | +        :param video_obj:
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        time.sleep(random.randint(3, 8))
 | 
	
		
			
				|  |  | +        trace_id = self.platform + str(uuid.uuid1())
 | 
	
		
			
				|  |  | +        our_user = random.choice(self.user_list)
 | 
	
		
			
				|  |  | +        item = VideoItem()
 | 
	
		
			
				|  |  | +        item.add_video_info("video_id", video_obj["id"])
 | 
	
		
			
				|  |  | +        item.add_video_info("video_title", video_obj["title"])
 | 
	
		
			
				|  |  | +        item.add_video_info("play_cnt", 0)
 | 
	
		
			
				|  |  | +        item.add_video_info("publish_time_stamp", int(time.time()))
 | 
	
		
			
				|  |  | +        item.add_video_info("out_user_id", video_obj["id"])
 | 
	
		
			
				|  |  | +        item.add_video_info("cover_url", video_obj["images"])
 | 
	
		
			
				|  |  | +        item.add_video_info("like_cnt", 0)
 | 
	
		
			
				|  |  | +        item.add_video_info("video_url", video_obj["video_url"])
 | 
	
		
			
				|  |  | +        item.add_video_info("out_video_id", video_obj["id"])
 | 
	
		
			
				|  |  | +        item.add_video_info("platform", self.platform)
 | 
	
		
			
				|  |  | +        item.add_video_info("strategy", self.mode)
 | 
	
		
			
				|  |  | +        item.add_video_info("session", "{}-{}".format(self.platform, int(time.time())))
 | 
	
		
			
				|  |  | +        item.add_video_info("user_id", our_user["uid"])
 | 
	
		
			
				|  |  | +        item.add_video_info("user_name", our_user["nick_name"])
 | 
	
		
			
				|  |  | +        # 获取当前时间
 | 
	
		
			
				|  |  | +        current_time = datetime.now()
 | 
	
		
			
				|  |  | +        formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
 | 
	
		
			
				|  |  | +        values = [[
 | 
	
		
			
				|  |  | +            video_obj["id"],
 | 
	
		
			
				|  |  | +            formatted_time,
 | 
	
		
			
				|  |  | +            video_obj["title"],
 | 
	
		
			
				|  |  | +            video_obj["images"],
 | 
	
		
			
				|  |  | +            video_obj["video_url"]
 | 
	
		
			
				|  |  | +        ]]
 | 
	
		
			
				|  |  | +        Feishu.insert_columns(self.platform, 'jixiangxingfu', "L0KXHh", "ROWS", 1, 2)
 | 
	
		
			
				|  |  | +        time.sleep(0.5)
 | 
	
		
			
				|  |  | +        Feishu.update_values(self.platform, 'jixiangxingfu', "L0KXHh", "A2:Z2", values)
 | 
	
		
			
				|  |  | +        mq_obj = item.produce_item()
 | 
	
		
			
				|  |  | +        pipeline = PiaoQuanPipeline(
 | 
	
		
			
				|  |  | +            platform=self.platform,
 | 
	
		
			
				|  |  | +            mode=self.mode,
 | 
	
		
			
				|  |  | +            rule_dict=self.rule_dict,
 | 
	
		
			
				|  |  | +            env=self.env,
 | 
	
		
			
				|  |  | +            item=mq_obj,
 | 
	
		
			
				|  |  | +            trace_id=trace_id,
 | 
	
		
			
				|  |  | +        )
 | 
	
		
			
				|  |  | +        if pipeline.process_item():
 | 
	
		
			
				|  |  | +            self.download_cnt += 1
 | 
	
		
			
				|  |  | +            self.mq.send_msg(mq_obj)
 | 
	
		
			
				|  |  | +            self.aliyun_log.logging(code="1002", message="成功发送至 ETL", data=mq_obj)
 | 
	
		
			
				|  |  | +            if self.download_cnt >= int(
 | 
	
		
			
				|  |  | +                    self.rule_dict.get("videos_cnt", {}).get("min", 200)
 | 
	
		
			
				|  |  | +            ):
 | 
	
		
			
				|  |  | +                self.limit_flag = True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    def run(self):
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        先抓取推荐列表的视频, 等待 2 分钟后抓取 detail 页面,等待 5 分钟后,抓取账号视频
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        self.get_recommend_list()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +if __name__ == '__main__':
 | 
	
		
			
				|  |  | +    J = JXXFRecommend(
 | 
	
		
			
				|  |  | +        platform="jixiangxingfu",
 | 
	
		
			
				|  |  | +        mode="recommend",
 | 
	
		
			
				|  |  | +        rule_dict={},
 | 
	
		
			
				|  |  | +        user_list=[{'uid': "123456", 'nick_name': "xiaoxiao"}],
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    )
 | 
	
		
			
				|  |  | +    J.get_recommend_list()
 |