在github网站上找出符合以下条件的项目(同时满足),并推送通知到手机上:
1、最近一周内发布的;
2、star数大于200;
3、topic是blockchain
查找repo的API(topic是blockchain,创建日期在2020年3月3日以后的):
https://api.github.com/search/repositories?q=topic:blockchain+created:>2020-03-03&sort=stars
直接查到符合条件的repo,不用在程序中过滤了(topic是blockchain,创建日期在2020年3月3日以后的,star数200以上的):
https://api.github.com/search/repositories?q=topic:blockchain+created:>2019-03-03+stars:>200
查找repo的API(topic是blockchain,按更新日期降序排列,不能按创建日期排序):
https://api.github.com/search/repositories?q=topic:blockchain&sort=updated&order=desc
推送通知到手机是用pushover,只有7天的免费使用,信息如下:
djlapp1,API Token/Key:个人注册后获取
User Key:个人注册后获取
发送通知到手机的API模板(POST方式):
https://api.pushover.net/1/messages.json?token={t}&user={u}&title={title}&message={message}&url={url}
思路:找到repo----构造发送内容----发送通知
import requests def get_repo(date=‘2020-03-03‘,topic=‘blockchain‘): # 找到指定topic的近期好仓库 api_template = ‘https://api.github.com/search/repositories?q=topic:{topic}+created:>{date}&sort=stars‘ repo_api = api_template.format(topic=topic,date=date) print(‘repo_api:‘,repo_api) repos = [] try: repos_info = requests.get(repo_api).json()[‘items‘] for repo in repos_info: if repo[‘stargazers_count‘] > 200: repos.append({‘name‘: repo[‘name‘], ‘id‘: repo[‘id‘],‘description‘:repo[‘description‘], ‘url‘: repo[‘html_url‘]}) except: print(‘github接口获取数据出现问题‘) return repos def make_pushapi(repo): # 构造单个的发送通知的API,repo是一个描述库的字典 pushapi_template = ‘https://api.pushover.net/1/messages.json?token={t}&user={u}&title={title}&message={message}&url={url}‘ title = ‘新上一个关于blockchain的库:‘+ repo[‘name‘] message = ‘name:‘+repo[‘name‘]+‘id:‘+str(repo[‘id‘])+‘description:‘+repo[‘description‘] url = repo[‘url‘] push_api = pushapi_template.format( t = ‘XXXXXXXXXXXXXXXXXX‘, u = ‘XXXXXXXXXXXXXX‘, title = title, message = message, url = url ) return push_api def push_notice(push_api): # 推送单条通知到手机 try: requests.post(push_api) print(‘发送成功一条‘) except: print(‘发送一条失败,可能是pushover服务接口问题‘) def make_and_push_notice(repo): # 另外一种构造发送请求的方法 push_api = ‘https://api.pushover.net/1/messages.json‘ data = { ‘token‘: ‘XXXXXXXXX‘, ‘user‘: ‘XXXX‘, ‘title‘: ‘新上一个关于blockchain的库:‘+ repo[‘name‘], ‘message‘: ‘name:‘+repo[‘name‘]+‘id:‘+str(repo[‘id‘])+‘description:‘+repo[‘description‘], ‘url‘: repo[‘url‘] } requests.post(push_api,data) repos = get_repo(‘2019-03-03‘) print(‘符合条件的仓库数量:‘,len(repos)) print(‘符合条件的仓库:‘,repos) for repo in repos: push_notice(make_pushapi(repo))