def initiator(logger_name): DDingWarn.logger = log.logger_generator(logger_name=logger_name) music_play.logger = log.logger_generator(logger_name=logger_name) heart_beats.logger = log.logger_generator(logger_name=logger_name) Text2Speech.logger = log.logger_generator(logger_name=logger_name) DDingWarn.request_ding(['你的音乐闹钟项目正在启动!']) light_breath_th = threading.Thread(target=LightBreath.LightBreath, args=(11, )) light_breath_th.start()
def get_article_content(url, retry_time=0): logger.info('尝试读取网页……') try: resp = requests.request(method='get', url=url) except Exception as e: retry_time += 1 if retry_time >= 10: logger.info(f'读取次数达到{retry_time},放弃读取。') return template(message=f'读取次数达到{retry_time},放弃读取。') logger.info(f'读取网页失败{retry_time}次。报错:{e.__str__()}') return get_article_content(url, retry_time) logger.info('读取网页成功,对网页进行解析') resp.encoding = 'utf-8' soup = BeautifulSoup(resp.text, 'html5lib') try: title = soup.find('div', class_='title') title = title.h1.text title = title.replace(' ', '') article_content = soup.find('div', class_='texts') article_content = article_content.text article_content = cut_needless_text( original_text=article_content, cut_keywords=[ '图片版', '简谱:', '\n\xa0\n\t\t\t\t\n\t\t\t\t\t', '以上视频由天声传播协会录制', '分享到:' ], delete_keywords=[ '\n\n\n\n\n\n点击下载MP3文件\t\t\t\t\n', '\n\n\t\t\t\t', '\n\n点击下载MP3文件\t\t\t\t' ]) article_content = article_content.replace('\n\n', '\n') scripture_place = article_content.find('\n') scripture = article_content[0:scripture_place] if '恩典365' in title: song_url = get_song_url(url=url, title=title) article_content += '\n音频链接:' + song_url[key_data][ 'song_url'] if song_url[key_success] else '找不到音频链接' except Exception as e: DDingWarn.request_ding(result=[f'网页结构已经发生变化。', f'解析中报错:\n {e}']) return template(message='网页结构已经发生变化。') logger.info('获取文章成功') return template(True, data={ 'url': url, 'title': title, 'content': article_content, 'scripture': scripture })
def text2speech(text, file_location=config.tts_location, file_name=None): global auth read_token() print(auth) tok = auth['access_token'] cuid = 'abc' # 用户唯一标识 ctp = '1' # 客户端类型选择 lan = 'zh' # 中文。固定值 spd = 2 # 语速 pit = 6 # 音调 vol = 10 # 音量 per = 106 # 度小宇=1,度小美=0,度逍遥=3,度丫丫=4,度博文=106,度小童=110,度小萌=111,度米朵=103,度小娇=5 aue = 3 # 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); if not os.path.exists(file_location): os.mkdir(file_location) speech_url = 'http://tsn.baidu.com/text2audio' logger.info(f'The Text Is:{text}') try: res = requests.post(url=speech_url, params={ 'tex': text, 'tok': tok, 'cuid': cuid, 'lan': lan, 'ctp': ctp, 'spd': spd, 'pit': pit, 'vol': vol, 'per': per, 'aue': aue }) except Exception as e: DDingWarn.request_ding(result=[f'请求百度失败!请检查网络连接~ {e}']) return template(message=f'请求百度失败!请检查网络连接~ {e}') if res.content[2:12] == b'err_detail': DDingWarn.request_ding(['请求百度成功,但秘钥错误!现在尝试获取新秘钥!']) get_token() return template(message='请求百度成功,但秘钥错误!已经尝试获取新秘钥!等待下一次请求结果!') if file_name is None: file_name = text[:5] file_name = os.path.abspath( os.path.join(file_location, file_name) + '.mp3') with open(file_name, 'wb') as fo: fo.write(res.content) return template(success=True, data=file_name)
def get_song_url(url, title, request_time=0): logger.info('从网页读取音乐链接...') try: resp = requests.get(url) except Exception as e: request_time += 1 logger.info(f'读取网页失败{request_time}次,错误:{e.__str__()}') if request_time >= 5: return template(message=f'读取网页失败达到{request_time}次,终止读取') return get_song_url(url, request_time) resp.encoding = 'utf-8' try: soup = BeautifulSoup(resp.text, 'html5lib') nrydown = soup.find('div', class_='nrydown') song_url = nrydown.a['href'] except Exception as e: DDingWarn.request_ding(result=[f'网页结构已经发生变化,请尽快修改{e}']) return template(message=f'网页结构已经发生变化,获取内容失败{e}') return template(success=True, data={'song_url': song_url, 'title': title})
def send_today_grace365(test=False): logger.debug('获取今日恩典365') grace365_result = jdjzww_daily.get_very_day_article( page_url_lists=[SpiritualFood.grace_365_url, SpiritualFood.grace_365_url_2], root_path=grace365_path) # 当需要补提交某日经文时候,使用下面的语句,时间格式为 ‘yyyy-mm-dd’ # grace365_result = get_very_day_article(date='2020-09-29') if not grace365_result[key_success]: DDingWarn.request_ding(result=['获取今日恩典365内容失败:', grace365_result[key_message]]) return grace365 = grace365_result[key_data] if test: DDingWarn.request_ding(result=['测试!', grace365['article']], ding_url=Dingding.pi_alert) return # logger.info('通过钉钉发送群聊') # DDingWarn.request_ding(result=[grace365['article']], ding_url=Dingding.morning_pi) logger.debug('将恩典365内容通过邮箱发送OneNote和网易邮箱') send_result = send_email.send_mail(aim_list=[mail_onenote, mail_peter_163, mail_wn], subject=grace365['title'], content=grace365['article']) if not send_result[key_success]: DDingWarn.request_ding(result=[send_result[key_message]]) send_email.close_server()
def send_today_stream_in_desert(date: list = []): if not date: now = datetime.datetime.now() today, month = now.day, now.month logger.debug(f'{month}月{today}日荒漠甘泉推送') else: today, month = date[0], date[1] aim_path = os.path.join(streams_in_desert_path, f'{month}月{today}日.md') with open(aim_path, 'r+', encoding='utf-8') as fo: article_content = fo.read() logger.debug('发送给卫娜') result = ServerChanWarn.server_chan_post(server_chan_url=Dingding.server_chan_url_wn, content=article_content, title=f'''荒漠甘泉{month}月{today}日''') if not result[key_success]: DDingWarn.request_ding(result=[result[key_message]]) logger.debug('将灵修内容通过邮箱发送OneNote和网易邮箱') article_content.strip('# |[|]') # re.sub('\(http\:\/\/*\)', '', article_content) send_result = send_email.send_mail(aim_list=[mail_onenote, mail_peter_163, mail_wn], subject=f'''荒漠甘泉{month}月{today}日''', content=article_content) if not send_result[key_success]: DDingWarn.request_ding(result=[send_result[key_message]]) send_email.close_server()
def send_calvinism_speech(): logger.debug('开始获取加尔文主义讲座文章并推送') logger.debug('读取文章-链接列表') article_list = get_contents.read_article_list(file_path=calvinism_article_list_file_path) title = get_contents.get_next_article_title(SentArticleTitlePath=calvinism_next_article_path) logger.debug(f'当次发送文章标题:{title}') if not title: DDingWarn.request_ding(result='没有文章了') return contents = get_contents.get_article_content(url=article_list[title]) if not contents[key_success]: DDingWarn.request_ding(result=f'今日推送失败:{contents[key_message]}') return contents = contents[key_data] get_contents.write_down_article(contents=contents, file_path=calvinism_speech_path, title=title) send_result = send_email.send_mail(aim_list=[mail_peter_163, mail_wn, mail_onenote], subject=f'''加尔文主义讲座-{title}''', content='\n'.join(contents)) if not send_result[key_success]: DDingWarn.request_ding(result=[send_result[key_message]]) send_email.close_server()
def send_today_anthem(): logger.debug('获取今日赞美诗') get_url_result = jdjzww_daily.get_very_day_anthem_url_lyrics() # print(get_url_result) if get_url_result[key_success]: logger.debug('获取成功,今日赞美诗发送卫娜:') data = get_url_result[key_data] # DDingWarn.request_ding(result=[data['song_url'], data['lyric']], ding_url=Dingding.morning_pi) song_url, lyric = data['song_url'], data['lyric'].replace('\n', '\n\n') result1 = ServerChanWarn.server_chan_post(server_chan_url=Dingding.server_chan_url_wn, title=f"{data['title']}", content=f"{song_url}\n\n{lyric}") if not result1[key_success]: DDingWarn.request_ding(result=[result1[key_message]], ding_url=Dingding.pi_alert) else: logger.info('今日赞美诗发送给卫娜成功。') # 发送邮箱 send_result = send_email.send_mail( aim_list=[mail_peter_163, mail_wn], subject=f"{data['title']}", content=f"{song_url}\n{lyric}") if not send_result[key_success]: DDingWarn.request_ding(result=[send_result[key_message]]) send_email.close_server() else: DDingWarn.request_ding(result=['今日赞美诗获取失败,未发送给卫娜', get_url_result[key_message]], ding_url=Dingding.pi_alert)
def send_today_scripture(test=False): logger.debug(f'开始获取今日灵修内容') soul_bread_result = jdjzww_daily.get_very_day_article(page_url_lists=[SpiritualFood.daily_food_url, SpiritualFood.daily_food_url_2], root_path=soul_bread_path) # 当需要补提交某日经文时候,使用下面的语句,时间格式为 ‘yyyy-mm-dd’ # soul_bread_result = get_very_day_article(date='2020-09-29') if not soul_bread_result[key_success]: DDingWarn.request_ding(result=['获取今日经文失败:', soul_bread_result[key_message]]) return soul_bread = soul_bread_result[key_data] scripture = soul_bread['scripture'].replace('\n', '') if test: DDingWarn.request_ding(result=['测试!', str(scripture)], ding_url=Dingding.pi_alert) DDingWarn.request_ding(result=['测试!', scripture.replace('今日经文:', ''), '', soul_bread['article']], ding_url=Dingding.pi_alert) return logger.debug('通过ServerChan 发送给朋友:') logger.debug('发送给卫娜') title, article = soul_bread['title'], soul_bread['article'].replace('\n', '\n\n') result1 = ServerChanWarn.server_chan_post(server_chan_url=Dingding.server_chan_url_wn, title=f"{title}", content=f"{scripture}\n\n{article}") if not result1[key_success]: DDingWarn.request_ding(result=[result1[key_message]]) else: logger.info('发送给卫娜成功。') logger.debug('发送给温琦') result2 = ServerChanWarn.server_chan_post(server_chan_url=Dingding.server_chan_url_vinky, title='今日经文', content=str(scripture)) if not result2[key_success]: DDingWarn.request_ding(result2[key_message]) else: logger.info('发送给温琦成功。') # logger.info('通过钉钉发送给自己') # DDingWarn.request_ding(result=[scripture.replace('今日经文:', ''), '', soul_bread['article']], # ding_url=Dingding.morning_pi) logger.debug('将灵修内容通过邮箱发送OneNote和网易邮箱') send_result = send_email.send_mail(aim_list=[mail_onenote, mail_peter_163, mail_wn], subject=soul_bread['title'], content=scripture + '\n' + soul_bread['article']) if not send_result[key_success]: DDingWarn.request_ding(result=[send_result[key_message]]) send_email.close_server()