|
| 1 | +import asyncio |
| 2 | +import nodriver as uc |
| 3 | +import markdown |
| 4 | +import argparse |
| 5 | +import yaml |
| 6 | + |
| 7 | +from termcolor import cprint |
| 8 | +from bs4 import BeautifulSoup |
| 9 | + |
| 10 | +from .listings import ( |
| 11 | + Listing, |
| 12 | + normalize_soup, |
| 13 | + TopGg, |
| 14 | + DiscordsCom, |
| 15 | + WumpusStore, |
| 16 | + DiscordAppDirectory, |
| 17 | + DiscordBotListCom, |
| 18 | + DisforgeCom, |
| 19 | + DiscordBotsGg, |
| 20 | + DiscordMe, |
| 21 | + NotFoundError, |
| 22 | +) |
| 23 | + |
| 24 | +COMPLETED = False |
| 25 | + |
| 26 | + |
| 27 | +async def async_main(args): |
| 28 | + with open("description.md", "r", encoding="utf-8") as f: |
| 29 | + description: str = f.read() |
| 30 | + with open(args.config, "r", encoding="utf-8") as f: |
| 31 | + config: dict = yaml.safe_load(f) |
| 32 | + application_id = ( |
| 33 | + args.application_id if args.application_id else config["application_id"] |
| 34 | + ) |
| 35 | + |
| 36 | + description = markdown.markdown(description) |
| 37 | + description = normalize_soup(BeautifulSoup(description, "html.parser")) |
| 38 | + |
| 39 | + browser = await uc.start() |
| 40 | + listings = [ |
| 41 | + DiscordsCom(browser, application_id), |
| 42 | + WumpusStore(browser, application_id), |
| 43 | + DiscordAppDirectory(browser, application_id), |
| 44 | + TopGg(browser, application_id), |
| 45 | + DiscordBotsGg(browser, application_id), |
| 46 | + ] |
| 47 | + |
| 48 | + if url := config.get("DiscordBotListCom", {}).get("url"): |
| 49 | + listings.append(DiscordBotListCom(browser, url)) |
| 50 | + |
| 51 | + if url := config.get("DisforgeCom", {}).get("url"): |
| 52 | + listings.append(DisforgeCom(browser, url)) |
| 53 | + |
| 54 | + if url := config.get("DiscordMe", {}).get("url"): |
| 55 | + listings.append(DiscordMe(browser, url)) |
| 56 | + |
| 57 | + for listing in listings: |
| 58 | + try: |
| 59 | + its_description = await listing.fetch_raw_description() |
| 60 | + except NotFoundError: |
| 61 | + cprint(f"{listing.name} not published", "black", "on_light_red") |
| 62 | + continue |
| 63 | + except asyncio.TimeoutError: |
| 64 | + cprint(f"{listing.name} timed out") |
| 65 | + continue |
| 66 | + if description == its_description: |
| 67 | + cprint(f"{listing.name} matches", "black", "on_green") |
| 68 | + else: |
| 69 | + cprint(f"{listing.name} does not match", "black", "on_yellow") |
| 70 | + global COMPLETED |
| 71 | + COMPLETED = True |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + parser = argparse.ArgumentParser( |
| 76 | + prog="Listings checker", |
| 77 | + description="Check the published status of your discord listings", |
| 78 | + ) |
| 79 | + parser.add_argument("-i", "--application_id", required=False, default=None) |
| 80 | + parser.add_argument("-c", "--config", required=False, default="listings.yaml") |
| 81 | + |
| 82 | + args = parser.parse_args() |
| 83 | + try: |
| 84 | + asyncio.get_event_loop().run_until_complete(async_main(args)) |
| 85 | + except Exception as e: # noqa |
| 86 | + if not COMPLETED: |
| 87 | + raise |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments