From 65bd54b023de1ae360fcabdb09b89618eb6acee0 Mon Sep 17 00:00:00 2001 From: sfyc23 Date: Wed, 27 Mar 2019 03:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=B8=A4=E7=A7=8D=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=E6=A0=BC=E8=A8=80=E6=B8=A0=E9=81=93=E9=80=89=E6=8B=A9?= =?UTF-8?q?=EF=BC=8C=E8=AE=BE=E7=BD=AE=E9=87=8C=E6=9C=89=E6=9B=B4=E7=81=B5?= =?UTF-8?q?=E6=B4=BB=E7=9A=84=E9=80=89=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GFWeather.py | 73 +++++++++++++++++++++++++++++++++------------------- README.md | 44 ++++++++++++++++++++++++++----- _config.yaml | 11 ++++++-- run.py | 15 ++++++++++- 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/GFWeather.py b/GFWeather.py index 7dbe462..ea71146 100644 --- a/GFWeather.py +++ b/GFWeather.py @@ -12,10 +12,10 @@ class gfweather: headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36", } + dictum_channel_name = {1: 'ONE●一个', 2: '词霸(每日英语)'} def __init__(self): - self.girlfriend_list, self.alarm_hour, self.alarm_minute = self.get_init_data() - + self.girlfriend_list, self.alarm_hour, self.alarm_minute, self.dictum_channel = self.get_init_data() def get_init_data(self): ''' @@ -28,6 +28,9 @@ def get_init_data(self): alarm_timed = config.get('alarm_timed').strip() init_msg = f"每天定时发送时间:{alarm_timed}\n" + dictum_channel = config.get('dictum_channel', -1) + init_msg += f"格言获取渠道:{self.dictum_channel_name.get(dictum_channel,'无')}\n" + girlfriend_list = [] girlfriend_infos = config.get('girlfriend_infos') for girlfriend in girlfriend_infos: @@ -36,7 +39,7 @@ def get_init_data(self): city_name = girlfriend.get('city_name').strip() city_code = city_dict.city_dict.get(city_name) if not city_code: - print('您输出城市无法收取到天气信息') + print('您输入的城市无法收取到天气信息') break girlfriend['city_code'] = city_code girlfriend_list.append(girlfriend) @@ -49,7 +52,7 @@ def get_init_data(self): print(init_msg) hour, minute = [int(x) for x in alarm_timed.split(':')] - return girlfriend_list, hour, minute + return girlfriend_list, hour, minute, dictum_channel def is_online(self, auto_login=False): ''' @@ -108,20 +111,26 @@ def run(self): # 定时任务 scheduler = BlockingScheduler() # 每天9:30左右给女朋友发送每日一句 - # scheduler.add_job(self.start_today_info, 'cron', hour=self.alarm_hour, minute=self.alarm_minute) + scheduler.add_job(self.start_today_info, 'cron', hour=self.alarm_hour, minute=self.alarm_minute) # 每隔2分钟发送一条数据用于测试。 - scheduler.add_job(self.start_today_info, 'interval', seconds=30) + # scheduler.add_job(self.start_today_info, 'interval', seconds=30) scheduler.start() - def start_today_info(self): + def start_today_info(self, is_test=False): ''' 每日定时开始处理。 - :return: None + :param is_test: 测试标志,当为True时,不发送微信信息,仅仅获取数据。 + :return: ''' print("*" * 50) print('获取相关信息...') - dictum_msg = self.get_dictum_info() - # dictum_msg = self.get_ciba_info() + + if self.dictum_channel == 1: + dictum_msg = self.get_dictum_info() + elif self.dictum_channel == 2: + dictum_msg = self.get_ciba_info() + else: + dictum_msg = '' for girlfriend in self.girlfriend_list: city_code = girlfriend.get('city_code') @@ -131,15 +140,22 @@ def start_today_info(self): sweet_words=sweet_words) name_uuid = girlfriend.get('name_uuid') wechat_name = girlfriend.get('wechat_name') - print(f'给 {wechat_name} 发送的内容是:\n{today_msg}') - if self.is_online(auto_login=True): - itchat.send(today_msg, toUserName=name_uuid) - # 防止信息发送过快。 - time.sleep(5) + print(f'给『{wechat_name}』发送的内容是:\n{today_msg}') + + if not is_test: + if self.is_online(auto_login=True): + itchat.send(today_msg, toUserName=name_uuid) + # 防止信息发送过快。 + time.sleep(5) print('发送成功..\n') def isJson(self, resp): + ''' + 判断数据是否能被 Json 化。 True 能,False 否。 + :param resp: + :return: + ''' try: resp.json() return True @@ -156,8 +172,8 @@ def get_ciba_info(self): conentJson = resp.json() content = conentJson.get('content') note = conentJson.get('note') - print(f"{content}\n{note}") - return f"{content}\n{note}" + # print(f"{content}\n{note}") + return f"{content}\n{note}\n" else: print("没有获取到数据") return None @@ -173,7 +189,7 @@ def get_dictum_info(self): soup_texts = BeautifulSoup(resp.text, 'lxml') # 『one -个』 中的每日一句 every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text - return every_msg + return every_msg + "\n" def get_weather_info(self, dictum_msg='', city_code='101030100', start_date='2018-01-01', sweet_words='来自最爱你的我'): ''' @@ -211,19 +227,24 @@ def get_weather_info(self, dictum_msg='', city_code='101030100', start_date='201 aqi = today_weather.get('aqi') aqi = f"空气 : {aqi}" - # 在一起,一共多少天了 - start_datetime = datetime.strptime(start_date, "%Y-%m-%d") - day_delta = (datetime.now() - start_datetime).days - delta_msg = f'宝贝这是我们在一起的第 {day_delta} 天' + # 在一起,一共多少天了,如果没有设置初始日期,则不用处理 + if start_date: + start_datetime = datetime.strptime(start_date, "%Y-%m-%d") + day_delta = (datetime.now() - start_datetime).days + delta_msg = f'宝贝这是我们在一起的第 {day_delta} 天。\n' + else: + delta_msg = '' - today_msg = f'{today_time}\n{delta_msg}。\n{notice}。\n{temperature}\n{wind}\n{aqi}\n{dictum_msg}\n{sweet_words}\n' + today_msg = f'{today_time}\n{delta_msg}{notice}。\n{temperature}\n{wind}\n{aqi}\n{dictum_msg}{sweet_words if sweet_words else ""}\n' return today_msg if __name__ == '__main__': - # pass - # gfweather().start_today_info() - gfweather().run() + # 只查看获取数据, + gfweather().start_today_info(True) + + # 直接运行 + # gfweather().run() # gfweather() diff --git a/README.md b/README.md index 0b5c085..ca90ae3 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ scheduler.start() *start_today_info* 是方法处理类。 #### 2.获取每日一句。 -数据来源: [ONE●一个][6] +数据来源 1: [ONE●一个][6] ``` def get_dictum_info(self): ''' @@ -69,6 +69,32 @@ def get_dictum_info(self): every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text return every_msg ``` +数据来源 2: [金山词霸 ● 每日一句](http://open.iciba.com/?c=api) + +有英文和中文翻译,例如: +> When you finally get your own happiness, you will understand the +> previous sadness is a kind of treasure, which makes you better to hold +> and cherish the people you love. +> 等你获得真正属于你的幸福之后,你就会明白一起的伤痛其实是一种财富,它让你学会更好地去把握和珍惜你爱的人。 + +代码实现 : +``` + def get_ciba_info(self): + ''' + 从词霸中获取每日一句,带英文。 + :return: + ''' + resp = requests.get('http://open.iciba.com/dsapi') + if resp.status_code == 200 and self.isJson(resp): + conentJson = resp.json() + content = conentJson.get('content') + note = conentJson.get('note') + # print(f"{content}\n{note}") + return f"{content}\n{note}\n" + else: + print("没有获取到数据") + return None +``` #### 3. 获取今日天气 。 天气数据来源:[SOJSON][7] @@ -100,20 +126,26 @@ itchat.send(today_msg, toUserName=name_uuid) ### 参数配置 config.yaml ``` -#每天的几点开始发送信息 +# 定时时间 alarm_timed: '9:30' + +# 格言渠道 +# 1 : ONE●一个 +# 2 : 词霸(每日英语) +dictum_channel: 2 + girlfriend_infos: - #女友微信昵称 wechat_name: '古典' #女友所在桂林 city_name: '桂林' - # 从那天开始勾搭的 + # 从那天开始勾搭的(可空) start_date: '2017-11-11' - # 谁给你发送的 + # 短句的最后留言(可空) sweet_words: '来自最爱你的我。' - #如果你有多个女友需要发送,则参照这个样式,复制即可 + #如果有你多个人需要发送,则参照这个样式,复制即可 - wechat_name: '陈老师' city_name: '朝阳区' @@ -137,4 +169,4 @@ python run.py [4]: https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html# [5]: https://apscheduler.readthedocs.io/en/latest/ [6]: http://wufazhuce.com/ - [7]: https://www.sojson.com/blog/305.html + [7]: https://www.sojson.com/blog/305.html \ No newline at end of file diff --git a/_config.yaml b/_config.yaml index dd8600f..757a00b 100644 --- a/_config.yaml +++ b/_config.yaml @@ -2,16 +2,23 @@ # https://ansible-tran.readthedocs.io/en/latest/docs/YAMLSyntax.html # http://einverne.github.io/post/2015/08/yaml.html +# 定时时间、 alarm_timed: '9:30' + +# 格言渠道 +# 1 : ONE●一个 +# 2 : 词霸(每日英语) +dictum_channel: 1 + girlfriend_infos: - #女友微信昵称 wechat_name: '古典' #女友所在桂林 city_name: '桂林' - # 从那天开始勾搭的 + # 从那天开始勾搭的(可空) start_date: '2017-11-11' - # 短句的最后留言 + # 短句的最后留言(可空) sweet_words: '来自最爱你的我。' #如果有你多个人需要发送,则参照这个样式,复制即可 diff --git a/run.py b/run.py index 3778ab5..b2a11f5 100644 --- a/run.py +++ b/run.py @@ -1,10 +1,23 @@ from GFWeather import gfweather + def run(): + ''' + 主程序入口 + :return: + ''' gfweather().run() - # gfweather() + + +def test_run(): + ''' + 运行前的测试 + :return: + ''' + gfweather().start_today_info(is_test=True) if __name__ == '__main__': + # test_run() run()