wangkun 2 jaren geleden
bovenliggende
commit
6246f74995
1 gewijzigde bestanden met toevoegingen van 87 en 98 verwijderingen
  1. 87 98
      main/zqttk_recommend.py

+ 87 - 98
main/zqttk_recommend.py

@@ -224,7 +224,6 @@ class Recommend:
                                 '/*[' + str(cls.i) + ']//*[@class="itemimage videolist--itemimage"]')\
                                 .get_attribute('src')
                         except Exception:
-                            # Common.logger(log_type).error('cover_url异常:{}', e)
                             cover_url = 0
 
                         if ad != 0:
@@ -254,106 +253,97 @@ class Recommend:
                             # video_url
                             video_url = cls.get_url(log_type, driver, video_title, title)
 
-                            Common.logger(log_type).info('play_cnt:{}', play_cnt)
-                            Common.logger(log_type).info('video_url:{}', video_url)
-
-                            # 下载视频
-                            Common.download_method(log_type, 'video', video_title, video_url)
-                            # 获取视频时长
-                            video_info = cls.get_video_info_from_local(
-                                "./videos/" + video_title + "/video.mp4")
-                            download_width = str(video_info[0])
-                            download_height = str(video_info[1])
-                            download_duration = video_info[2]
-                            # 视频时长<60s,直接删除
-                            if int(download_duration) < 60:
-                                # 删除视频文件夹
-                                shutil.rmtree("./videos/" + video_title + "/")
-                                Common.logger(log_type).info("时长:{}<60秒,删除成功\n", int(download_duration))
-                                return
+                            if video_url == '':
+                                Common.logger(log_type).info('无法播放的视频\n')
+                                driver.press_keycode(4)
                             else:
-                                # 下载封面
-                                Common.download_method(log_type, 'cover', video_title, cover_url)
-                                # 保存视频信息至 "./videos/{download_video_title}/info.txt"
-                                with open("./videos/" + video_title
-                                          + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
-                                    f_a.write(str(int(time.time())) + "\n" +
-                                              str(video_title) + "\n" +
-                                              str(int(download_duration)) + "\n" +
-                                              str(int(float(
-                                                  play_cnt.split(' ')[-1].split('万')[0]) * 10000)) + "\n" +
-                                              '0' + "\n" +
-                                              '0' + "\n" +
-                                              '0' + "\n" +
-                                              str(download_width) + '*' + str(download_height) + "\n" +
-                                              str(int(time.time())) + "\n" +
-                                              '知青天天看' + "\n" +
-                                              str(cover_url) + "\n" +
-                                              str(video_url) + "\n" +
-                                              str(cover_url) + "\n" +
-                                              "zhiqingzongqun" + str(int(time.time())))
-                                Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
 
-                                # 上传视频
-                                Common.logger(log_type).info("开始上传视频:{}".format(video_title))
-                                if env == 'dev' and int(download_width) >= int(download_height):
-                                    our_video_id = Publish.upload_and_publish(log_type, env, "width")
-                                    our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
-                                        our_video_id) + "/info"
-                                elif env == 'dev' and int(download_width) < int(download_height):
-                                    our_video_id = Publish.upload_and_publish(log_type, env, "height")
-                                    our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
-                                        our_video_id) + "/info"
-                                elif env == 'prod' and int(download_width) >= int(download_height):
-                                    our_video_id = Publish.upload_and_publish(log_type, env, "width")
-                                    our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
-                                        our_video_id) + "/info"
-                                elif env == 'prod' and int(download_width) < int(download_height):
-                                    our_video_id = Publish.upload_and_publish(log_type, env, "height")
-                                    our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
-                                        our_video_id) + "/info"
+                                Common.logger(log_type).info('play_cnt:{}', play_cnt)
+                                Common.logger(log_type).info('video_url:{}', video_url)
+
+                                # 下载视频
+                                Common.download_method(log_type, 'video', video_title, video_url)
+                                # 获取视频时长
+                                video_info = cls.get_video_info_from_local(
+                                    "./videos/" + video_title + "/video.mp4")
+                                download_width = str(video_info[0])
+                                download_height = str(video_info[1])
+                                download_duration = video_info[2]
+                                # 视频时长<60s,直接删除
+                                if int(download_duration) < 60:
+                                    # 删除视频文件夹
+                                    shutil.rmtree("./videos/" + video_title + "/")
+                                    Common.logger(log_type).info("时长:{}<60秒,删除成功\n", int(download_duration))
+                                    return
                                 else:
-                                    our_video_id = Publish.upload_and_publish(log_type, env, "width")
-                                    our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
-                                        our_video_id) + "/info"
-                                Common.logger(log_type).info("视频上传完成:{}", video_title)
+                                    # 下载封面
+                                    Common.download_method(log_type, 'cover', video_title, cover_url)
+                                    # 保存视频信息至 "./videos/{download_video_title}/info.txt"
+                                    with open("./videos/" + video_title
+                                              + "/" + "info.txt", "a", encoding="UTF-8") as f_a:
+                                        f_a.write(str(int(time.time())) + "\n" +
+                                                  str(video_title) + "\n" +
+                                                  str(int(download_duration)) + "\n" +
+                                                  str(int(float(
+                                                      play_cnt.split(' ')[-1].split('万')[0]) * 10000)) + "\n" +
+                                                  '0' + "\n" +
+                                                  '0' + "\n" +
+                                                  '0' + "\n" +
+                                                  str(download_width) + '*' + str(download_height) + "\n" +
+                                                  str(int(time.time())) + "\n" +
+                                                  '知青天天看' + "\n" +
+                                                  str(cover_url) + "\n" +
+                                                  str(video_url) + "\n" +
+                                                  str(cover_url) + "\n" +
+                                                  "zhiqingzongqun" + str(int(time.time())))
+                                    Common.logger(log_type).info("==========视频信息已保存至info.txt==========")
 
-                                # 保存视频 ID 到已下载表
-                                Common.logger(log_type).info("保存视频至已下载表:{}", video_title)
-                                # 视频ID工作表,插入首行
-                                Feishu.insert_columns(log_type, "zhiqingzongqun", "1a88b3", "ROWS", 1, 2)
-                                # 视频ID工作表,首行写入数据
-                                upload_time = int(time.time())
-                                values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
-                                           "推荐榜",
-                                           video_title,
-                                           our_video_link,
-                                           play_cnt,
-                                           int(download_duration),
-                                           str(download_width) + '*' + str(download_height),
-                                           cover_url,
-                                           video_url]]
-                                time.sleep(1)
-                                Feishu.update_values(log_type, "zhiqingzongqun", "1a88b3", "F2:V2", values)
-                                Common.logger(log_type).info("视频:{},下载/上传成功\n", video_title)
-                                driver.press_keycode(4)
+                                    # 上传视频
+                                    Common.logger(log_type).info("开始上传视频:{}".format(video_title))
+                                    if env == 'dev' and int(download_width) >= int(download_height):
+                                        our_video_id = Publish.upload_and_publish(log_type, env, "width")
+                                        our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
+                                            our_video_id) + "/info"
+                                    elif env == 'dev' and int(download_width) < int(download_height):
+                                        our_video_id = Publish.upload_and_publish(log_type, env, "height")
+                                        our_video_link = "https://testadmin.piaoquantv.com/cms/post-detail/" + str(
+                                            our_video_id) + "/info"
+                                    elif env == 'prod' and int(download_width) >= int(download_height):
+                                        our_video_id = Publish.upload_and_publish(log_type, env, "width")
+                                        our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
+                                            our_video_id) + "/info"
+                                    elif env == 'prod' and int(download_width) < int(download_height):
+                                        our_video_id = Publish.upload_and_publish(log_type, env, "height")
+                                        our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
+                                            our_video_id) + "/info"
+                                    else:
+                                        our_video_id = Publish.upload_and_publish(log_type, env, "width")
+                                        our_video_link = "https://admin.piaoquantv.com/cms/post-detail/" + str(
+                                            our_video_id) + "/info"
+                                    Common.logger(log_type).info("视频上传完成:{}", video_title)
 
-                            # Feishu.insert_columns(log_type, 'zhiqingzongqun', 'Z48hlq', 'ROWS', 1, 2)
-                            # get_feeds_time = int(time.time())
-                            # values = [[time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(get_feeds_time)),
-                            #            '推荐榜',
-                            #            str(video_title),
-                            #            play_cnt,
-                            #            cover_url,
-                            #            video_url]]
-                            # time.sleep(1)
-                            # Feishu.update_values(log_type, 'zhiqingzongqun', 'Z48hlq', 'A2:Z2', values)
-                            # Common.logger(log_type).info('视频信息写入飞书文档成功\n')
-                            # driver.press_keycode(4)
-                            # cls.download_publish(log_type, env)
-                            break
+                                    # 保存视频 ID 到已下载表
+                                    Common.logger(log_type).info("保存视频至已下载表:{}", video_title)
+                                    # 视频ID工作表,插入首行
+                                    Feishu.insert_columns(log_type, "zhiqingzongqun", "1a88b3", "ROWS", 1, 2)
+                                    # 视频ID工作表,首行写入数据
+                                    upload_time = int(time.time())
+                                    values = [[time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(upload_time)),
+                                               "推荐榜",
+                                               video_title,
+                                               our_video_link,
+                                               play_cnt,
+                                               int(download_duration),
+                                               str(download_width) + '*' + str(download_height),
+                                               cover_url,
+                                               video_url]]
+                                    time.sleep(1)
+                                    Feishu.update_values(log_type, "zhiqingzongqun", "1a88b3", "F2:V2", values)
+                                    Common.logger(log_type).info("视频:{},下载/上传成功\n", video_title)
+                                    driver.press_keycode(4)
+
+                                break
                     except Exception:
-                        # Common.logger(log_type).error('switch_to.window(recommend_handle)异常:{}', e)
                         pass
 
                 if cls.i == 2000:
@@ -380,11 +370,10 @@ class Recommend:
             for info_handle in info_handles:
                 try:
                     driver.switch_to.window(info_handle)
-                    url = driver.find_element(
+                    video_url = driver.find_element(
                         By.XPATH,
                         '//*[@class="wx-swiper-slide-frame"]'
-                        '/*[2]//*[@class="video_item videoswiper--video_item"]')
-                    video_url = url.get_attribute('src')
+                        '/*[2]//*[@class="video_item videoswiper--video_item"]').get_attribute('src')
                     return video_url
                 except NoSuchElementException:
                     pass