Skip to content

Commit

Permalink
添加了测试方式,两种不同的格言渠道选择,设置里有更灵活的选项。
Browse files Browse the repository at this point in the history
  • Loading branch information
sfyc23 committed Mar 26, 2019
1 parent 10532fc commit 65bd54b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 35 deletions.
73 changes: 47 additions & 26 deletions GFWeather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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):
'''
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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='来自最爱你的我'):
'''
Expand Down Expand Up @@ -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()
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ scheduler.start()
*start_today_info* 是方法处理类。

#### 2.获取每日一句。
数据来源: [ONE●一个][6]
数据来源 1[ONE●一个][6]
```
def get_dictum_info(self):
'''
Expand All @@ -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]

Expand Down Expand Up @@ -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: '朝阳区'
Expand All @@ -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
11 changes: 9 additions & 2 deletions _config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: '来自最爱你的我。'

#如果有你多个人需要发送,则参照这个样式,复制即可
Expand Down
15 changes: 14 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
@@ -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()


Expand Down

0 comments on commit 65bd54b

Please sign in to comment.