Last active
April 23, 2019 17:11
-
-
Save nichochar/d34767001f15ca93d0f9a8a844f4c8f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import time | |
| import requests | |
| import feedparser | |
| def read_secrets(): | |
| with open('secret.json', 'r+') as f: | |
| data = json.load(f) | |
| return data | |
| SECRETS = read_secrets() | |
| BASE_URL = "https://api.telegram.org/bot%s/" % SECRETS.get('token') | |
| CSGO_CHAT_ID = SECRETS.get('chat_id') | |
| TEST_CHAT_ID = SECRETS.get('test_chat_id') | |
| POLL_FREQUENCY_SECS = 60 | |
| def bot_info(): | |
| url = BASE_URL + "getMe" | |
| response = requests.get(url) | |
| return response.json() | |
| def get_updates(): | |
| url = BASE_URL + "getUpdates" | |
| response = requests.get(url) | |
| return response.json() | |
| def send_message(message): | |
| url = BASE_URL + "sendMessage" | |
| params = { | |
| 'chat_id': CSGO_CHAT_ID, | |
| 'text': message, | |
| # 'parse_mode': 'markdown', | |
| } | |
| response = requests.post(url, json=params) | |
| return response.json() | |
| def scan_HLTV(): | |
| url = 'https://www.hltv.org/rss/news' | |
| feed = feedparser.parse(url) | |
| return feed | |
| def message_from_entry(entry): | |
| return '{title}\n----\n{link}'.format(title=entry.title, link=entry.link) | |
| if __name__ == '__main__': | |
| done = set() | |
| while True: | |
| print("Scaning HLTV for new entries") | |
| feed = scan_HLTV() | |
| print("Skipping entries: %s" % done) | |
| for entry in feed.entries: | |
| if entry.id in done: | |
| continue | |
| else: | |
| msg = message_from_entry(entry) | |
| print(msg) | |
| resp = send_message(msg) | |
| if resp['ok'] is True: | |
| done.add(entry.id) | |
| else: | |
| print("Error logging entry: %s" % entry) | |
| print(resp) | |
| time.sleep(POLL_FREQUENCY_SECS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment