Skip to content

Commit

Permalink
Fix wrong requirements "telegram"
Browse files Browse the repository at this point in the history
Support json5 (#2)
Change README.md
  • Loading branch information
lupohan44 committed Jul 12, 2021
1 parent c4d9c3a commit 7313f1e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/config.json
/config.json5
/log.txt
/.idea/
/record.json
/record.json5
/SteamDBFreeGamesClaimer.iml
55 changes: 13 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<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"]
}
}
```

## 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
Expand Down
20 changes: 10 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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!")
Expand Down
9 changes: 8 additions & 1 deletion config.example.json → config.example.json5
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
{
"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",
// 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"]
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
asf-ipc~=2.1.0
json5~=0.9.6

0 comments on commit 7313f1e

Please sign in to comment.