|
| 1 | +# compares the current day's adoption list to the previous days to check if anything new has been added |
| 2 | +# new additions are added to adoption-newly-added.json |
| 3 | +# new additions have the `is_recent` property with `True` or `False` values |
| 4 | +# `True` means it was added as a new news (more recent than the most recent additon from the previous day) |
| 5 | +# `False` means it was an old event that was missing from the list |
| 6 | + |
| 7 | +import os |
| 8 | +import time |
| 9 | +from datetime import datetime, timezone |
| 10 | +import json |
| 11 | +import yaml |
| 12 | +import requests |
| 13 | +from urllib.parse import quote |
| 14 | +from dotenv import load_dotenv |
| 15 | + |
| 16 | + |
| 17 | +load_dotenv() |
| 18 | +TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN") |
| 19 | + |
| 20 | + |
| 21 | +current_time = round(time.time()) # seconds |
| 22 | +date = datetime.now(timezone.utc).strftime('%Y-%m-%d') # yyyy-mm-dd |
| 23 | +print(f"Epoch: {current_time}") |
| 24 | +print(f"Date: {date}") |
| 25 | + |
| 26 | + |
| 27 | +# load/sort old adoption list |
| 28 | +repo_root = os.path.abspath(__file__ + '/../../') |
| 29 | +old_adoption_list_path = repo_root + '/_data/adoption-old.json' |
| 30 | +with open(old_adoption_list_path, 'r') as old_adoption_list_file: |
| 31 | + old_adoption_list = json.load(old_adoption_list_file) |
| 32 | + old_adoption_list = sorted(old_adoption_list, key=lambda x: x['date'], reverse=True) |
| 33 | +print(f"old_adoption_list count: {len(old_adoption_list)}") |
| 34 | + |
| 35 | + |
| 36 | +# load/sort current adoption list |
| 37 | +request = requests.get('https://ethereumadoption.com/adoption.json') |
| 38 | +current_adoption_list = request.json() |
| 39 | +current_adoption_list = sorted(current_adoption_list, key=lambda x: x['date'], reverse=True) |
| 40 | +print(f"current_adoption_list count: {len(current_adoption_list)}") |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +# create list of new entries |
| 45 | +new_entries = [] |
| 46 | +is_recent = True |
| 47 | +for current_entry in current_adoption_list: |
| 48 | + has_match = False |
| 49 | + for old_entry in old_adoption_list: |
| 50 | + if current_entry == old_entry: |
| 51 | + has_match = True |
| 52 | + is_recent = False |
| 53 | + if has_match == False: |
| 54 | + new_entry = dict(current_entry) |
| 55 | + new_entry['is_recent'] = is_recent |
| 56 | + new_entries.append(new_entry) |
| 57 | +print(f"new_entries count: {len(new_entries)}") |
| 58 | + |
| 59 | + |
| 60 | +# save new entries to file |
| 61 | +new_entries_path = repo_root + '/_data/adoption-new-entries.json' |
| 62 | +with open(new_entries_path, "w") as new_entries_file: |
| 63 | + json.dump(new_entries, new_entries_file) |
| 64 | + |
| 65 | + |
| 66 | +# save current list as old list for next update |
| 67 | +with open(old_adoption_list_path, "w") as old_adoption_list_file: |
| 68 | + json.dump(current_adoption_list, old_adoption_list_file) |
| 69 | + |
| 70 | + |
| 71 | +# post new entries to telegram |
| 72 | +new_entries.reverse() |
| 73 | +print(new_entries) |
| 74 | + |
| 75 | + |
| 76 | +for entry in new_entries: |
| 77 | + if entry['is_recent']: |
| 78 | + status = '' |
| 79 | + # if entry['status'] == 'dev': |
| 80 | + # status = '[dev] ' |
| 81 | + entities = '/'.join(entry['entities']) |
| 82 | + products = ', '.join(entry['products']) |
| 83 | + source = entry['sources'][0] |
| 84 | + support = '' |
| 85 | + if entry['chains'][0] != None: |
| 86 | + chains = ', '.join(entry['chains']) |
| 87 | + support = f" with support for {chains}" |
| 88 | + # use endpoint to get channel id after interacting with bot (/start) |
| 89 | + # https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/getUpdates |
| 90 | + chat_list = [ |
| 91 | + "-1002400345737" # @ethereumadoption |
| 92 | + ] |
| 93 | + message = f"{status}{entities} announces {products}{support}" |
| 94 | + message = f"{message.upper()}\n\n{source}" |
| 95 | + for chat_id in chat_list: |
| 96 | + url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage" |
| 97 | + params = { |
| 98 | + 'chat_id': chat_id, |
| 99 | + 'text': message, |
| 100 | + 'reply_markup': json.dumps({ |
| 101 | + 'inline_keyboard': [[{ |
| 102 | + 'text': 'EthereumAdoption.com', |
| 103 | + 'url': 'https://ethereumadoption.com' |
| 104 | + }]] |
| 105 | + }) |
| 106 | + } |
| 107 | + print(requests.get(url=url, params=params).json()) |
| 108 | + time.sleep(1) |
| 109 | + |
| 110 | + |
0 commit comments