Skip to content

Commit

Permalink
Fix wrong requirements
Browse files Browse the repository at this point in the history
Change loop delay default value to 600 seconds
Support MarkDown format in telegram notification
Support multiple chat_id in telegram
Change README.md
  • Loading branch information
lupohan44 committed Jul 12, 2021
1 parent 526f5e7 commit 4c7ae27
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 deletions.
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,76 @@ This project is inspired by [SteamDB-FreeGames](https://github.com/azhuge233/Ste
- python3
- playwright
- bs4(lxml)
- telegram
- python-telegram-bot

## Usage
1. Clone repository

```shell
git clone https://github.com/lupohan44/SteamDBFreeGamesClaimer.git
```
2. Install requirements

```shell
pip3 install -r requirements.txt
playwright install
```
3. Copy [config.example.json](config.example.json) to config.json, change [settings](#configjson) in it.

4. Run
```shell
python3 app.py
```
```shell
python3 app.py
```

## Config.json

Notice: **DO NOT** copy paste from below
```json
{
"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"
},
"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",
"chat_id": "CHAT_ID_FROM_API",
"notification_format": "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{game}</a>\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time}",
// Required
"chat_id": [
"CHAT_ID_FROM_API"
],
"format": {
"markdown": false,
"message": "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{game}</a>\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"]
}
}
```

*"telegram"* and *"asf"* field can be deleted if you don't want this feature, but you can't delete both fields.
## Known issue
1. Playwright does not support CentOS. ([issue](https://github.com/microsoft/playwright/issues/6219))

When *"telegram"* is enabled, *"token"* and *"chat_id"* is required.
## Changelog
###2021/7/12
1. Fix wrong requirements
2. Change loop delay default value to 600 seconds
3. Support MarkDown format in telegram notification
4. Support multiple chat_id in telegram
5. Change README.md

When *"asf"* is enabled, *"ipc"* and *"ipc_password"* is required.
**Notice**: This upgrade change config.json
###2021/7/11
1. Initial release
37 changes: 24 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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
AT_LEAST_ENABLE_ONE_FUNCTION_ERROR_MSG = "Both telegram and asf are not enabled!"
CONFIG_FILE_NEEDS_TO_BE_UPDATED = "%s's format has been changed, please change it accordingly!" % CONFIG_PATH
GET_PAGE_SOURCE_ERROR_MSG = "Get page source error!"

# log format
Expand Down Expand Up @@ -64,9 +65,11 @@ class Telegram:
def __init__(self):
self.enable = False
self.token = ""
self.chat_id = ""
self.notification_format = "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{" \
"game}</a>\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time} "
self.chat_id_list = []
self.markdown = False
self.notification_message = "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{" \
"game}</a>\nfree type: {free_type}\nstart time: {start_time}\nend time: {" \
"end_time}\n!redeem asf {sub_id} "
self.notification_free_type = []
self.delay = 1

Expand All @@ -82,7 +85,7 @@ def __init__(self):
class Config:
def __init__(self):
self.loop = True
self.loop_delay = 60
self.loop_delay = 600
self.time_format = TimeFormat()
self.telegram = Telegram()
self.asf = ASF()
Expand Down Expand Up @@ -141,8 +144,10 @@ def send_telegram_notification(msg_list): # use telegram bot to send message
try:
tb = telegram.Bot(config.telegram.token)
for msg in msg_list:
tb.send_message(chat_id=config.telegram.chat_id, text=msg, parse_mode="HTML")
time.sleep(config.telegram.delay)
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")
time.sleep(config.telegram.delay)
except Exception as ex:
logger.error("Send message error!")
raise ex
Expand Down Expand Up @@ -236,10 +241,11 @@ def process_steamdb_result(previous, steamdb_result):
'''get game details end'''

notification_str = config.telegram. \
notification_format.format(game=game_name, sub_id=sub_id, steam_url=steam_url,
start_time=start_time, end_time=end_time,
free_type=free_type)
if ("ALL" in config.telegram.notification_free_type) or (free_type in config.telegram.notification_free_type):
notification_message.format(game=game_name, sub_id=sub_id, steam_url=steam_url,
start_time=start_time, end_time=end_time,
free_type=free_type)
if ("ALL" in config.telegram.notification_free_type) or (
free_type in config.telegram.notification_free_type):
telegram_push_message.append(notification_str)
if free_type not in config.asf.redeem_type_blacklist:
asf_redeem_list.append(sub_id)
Expand Down Expand Up @@ -289,10 +295,15 @@ def parse_config():
raise Exception(TELEGRAM_REQUIRE_TOKEN_ERROR_MSG)
if "chat_id" not in config_json["telegram"]:
raise Exception(TELEGRAM_REQUIRE_CHAT_ID_ERROR_MSG)
if type(config_json["telegram"]["chat_id"]) != list:
raise Exception(CONFIG_FILE_NEEDS_TO_BE_UPDATED)
config.telegram.token = config_json["telegram"]["token"]
config.telegram.chat_id = config_json["telegram"]["chat_id"]
if "notification_format" in config_json["telegram"]:
config.telegram.notification_format = config_json["telegram"]["notification_format"]
config.telegram.chat_id_list = config_json["telegram"]["chat_id"]
if "format" in config_json["telegram"]:
if "markdown" in config_json["telegram"]["format"]:
config.telegram.markdown = config_json["telegram"]["format"]["markdown"]
if "message" in config_json["telegram"]["format"]:
config.telegram.notification_message = config_json["telegram"]["format"]["message"]
if "notification_free_type" in config_json["telegram"]:
config.telegram.notification_free_type = config_json["telegram"]["notification_free_type"]
if "delay" in config_json["telegram"]:
Expand Down
9 changes: 7 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
},
"telegram": {
"token": "TOKEN_FROM_BOT_FATHER",
"chat_id": "CHAT_ID_FROM_API",
"notification_format": "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{game}</a>\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time}",
"chat_id": [
"CHAT_ID_FROM_API"
],
"format": {
"markdown": false,
"message": "<b>{game}</b>\nSub ID: <i>{sub_id}</i>\nlink: <a href=\"{steam_url}\" >{game}</a>\nfree type: {free_type}\nstart time: {start_time}\nend time: {end_time}\n!redeem asf {sub_id}"
},
"notification_free_type": ["ALL"],
"delay": 1
},
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
playwright~=1.12.1
beautifulsoup4~=4.9.3
telegram~=0.0.1
python-telegram-bot~=13.4.1
lxml~=4.6.3
asf-ipc~=2.1.0

0 comments on commit 4c7ae27

Please sign in to comment.