From 7313f1e957ff1fc31a8b6b9522ed375a2fac8c32 Mon Sep 17 00:00:00 2001 From: lupohan44 Date: Mon, 12 Jul 2021 22:55:00 +0800 Subject: [PATCH] Fix wrong requirements "telegram" Support json5 (#2) Change README.md --- .gitignore | 4 +- README.md | 55 +++++---------------- app.py | 20 ++++---- config.example.json => config.example.json5 | 9 +++- requirements.txt | 5 +- 5 files changed, 36 insertions(+), 57 deletions(-) rename config.example.json => config.example.json5 (69%) diff --git a/.gitignore b/.gitignore index 3b713a1..324f4e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -/config.json +/config.json5 /log.txt /.idea/ -/record.json +/record.json5 /SteamDBFreeGamesClaimer.iml diff --git a/README.md b/README.md index d60a060..b0370eb 100644 --- a/README.md +++ b/README.md @@ -27,57 +27,28 @@ This project is inspired by [SteamDB-FreeGames](https://github.com/azhuge233/Ste pip3 install -r requirements.txt playwright install ``` -3. Copy [config.example.json](config.example.json) to config.json, change [settings](#configjson) in it. +3. Copy [config.example.json5](config.example.json5) to config.json5, change settings in it according to the comment. 4. Run ```shell python3 app.py ``` -## Config.json - -Notice: **DO NOT** copy paste from below -```json5 -{ - "loop": true, - "loop_delay": 600, - "headers": { - "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69" - }, - "time_format": { - "format_str": "%Y/%m/%d %H:%M UTC", - "utc_offset": 0 - }, - // Remove this field if you don't need it, but you can't remove it and "asf" at the same time - "telegram": { - // Required - "token": "TOKEN_FROM_BOT_FATHER", - // Required - "chat_id": [ - "CHAT_ID_FROM_API" - ], - "format": { - "markdown": false, - "message": "{game}\nSub ID: {sub_id}\nlink: {game}\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time}\n!redeem asf {sub_id}" - }, - // "ALL" will include all free types - "notification_free_type": ["ALL"], - // Delay after each message sent by telegram bot - "delay": 1 - }, - // Remove this field if you don't need it, but you can't remove it and "telegram" at the same time - "asf": { - // Required - "ipc": "http://127.0.0.1:1242", - "ipc_password": "", - "redeem_type_blacklist": ["Weekend"] - } -} -``` - ## Known issue 1. Playwright does not support CentOS. ([issue](https://github.com/microsoft/playwright/issues/6219)) ## Changelog +###2021/7/12-2 +1. Fix wrong requirements "telegram" +2. Support json5 (#2) +3. Change README.md + +**Notice**: This upgrade change config.json to config.json5 and change record.json to record.json5 + +Run +```shell +pip3 uninstall telegram +``` +should remove the wrong requirements ###2021/7/12 1. Fix wrong requirements 2. Change loop delay default value to 600 seconds diff --git a/app.py b/app.py index 4f81397..e4b6a9f 100644 --- a/app.py +++ b/app.py @@ -1,23 +1,23 @@ import asyncio import datetime -import json import logging import os import selectors import time from urllib import request -import telegram +from telegram.ext import Updater +import json5 from ASF import IPC from bs4 import BeautifulSoup from playwright.sync_api import sync_playwright '''Static Variables''' STEAM_DB_FREE_GAMES_URL = "https://steamdb.info/upcoming/free/" -CONFIG_PATH = "config.json" -RECORD_PATH = "record.json" +CONFIG_PATH = "config.json5" +RECORD_PATH = "record.json5" FIRST_DELAY = 8 -CONFIG_NOT_EXIST_ERROR_MSG = "Cannot read config.json!" +CONFIG_NOT_EXIST_ERROR_MSG = "Cannot read %s!" % CONFIG_PATH TELEGRAM_REQUIRE_TOKEN_ERROR_MSG = "Cannot get token of telegram from %s!" % CONFIG_PATH TELEGRAM_REQUIRE_CHAT_ID_ERROR_MSG = "Cannot get chat_id of telegram from %s!" % CONFIG_PATH ASF_REQUIRE_IPC_ERROR_MSG = "Cannot get ipc of asf from %s!" % CONFIG_PATH @@ -99,13 +99,13 @@ def load_json(path, method="r", init_str="{}"): with open(path, "w", encoding='utf-8') as f: f.write(init_str) with open(path, method, encoding='utf-8') as f: - data = json.load(f) + data = json5.load(f) return data def write_json(path, data, method="w"): with open(path, method, encoding='utf-8') as f: - json.dump(data, f, indent=4) + json5.dump(data, f, indent=4) def get_url_single(url, headers=None, decode='utf-8'): @@ -142,11 +142,11 @@ def playwright_get_url(url, delay=0, headless=True): def send_telegram_notification(msg_list): # use telegram bot to send message if len(msg_list) != 0: try: - tb = telegram.Bot(config.telegram.token) + tb = Updater(config.telegram.token) for msg in msg_list: for chat_id in config.telegram.chat_id_list: - tb.send_message(chat_id=chat_id, text=msg, - parse_mode="Markdown" if config.telegram.markdown else "HTML") + tb.bot.send_message(chat_id=chat_id, text=msg, + parse_mode="Markdown" if config.telegram.markdown else "HTML") time.sleep(config.telegram.delay) except Exception as ex: logger.error("Send message error!") diff --git a/config.example.json b/config.example.json5 similarity index 69% rename from config.example.json rename to config.example.json5 index 61664aa..8d5cf94 100644 --- a/config.example.json +++ b/config.example.json5 @@ -1,6 +1,6 @@ { "loop": true, - "loop_delay": 60, + "loop_delay": 600, "headers": { "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69" }, @@ -8,8 +8,11 @@ "format_str": "%Y/%m/%d %H:%M UTC", "utc_offset": 0 }, + // Remove this field if you don't need it, but you can't remove it and "asf" at the same time "telegram": { + // Required "token": "TOKEN_FROM_BOT_FATHER", + // Required "chat_id": [ "CHAT_ID_FROM_API" ], @@ -17,10 +20,14 @@ "markdown": false, "message": "{game}\nSub ID: {sub_id}\nlink: {game}\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time}\n!redeem asf {sub_id}" }, + // "ALL" will include all free types "notification_free_type": ["ALL"], + // Delay after each message sent by telegram bot "delay": 1 }, + // Remove this field if you don't need it, but you can't remove it and "telegram" at the same time "asf": { + // Required "ipc": "http://127.0.0.1:1242", "ipc_password": "", "redeem_type_blacklist": ["Weekend"] diff --git a/requirements.txt b/requirements.txt index 0c92f03..508fc7e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ playwright~=1.12.1 beautifulsoup4~=4.9.3 -python-telegram-bot~=13.4.1 +python-telegram-bot~=13.7 lxml~=4.6.3 -asf-ipc~=2.1.0 \ No newline at end of file +asf-ipc~=2.1.0 +json5~=0.9.6 \ No newline at end of file