Compare commits
5 commits
8683d08d1c
...
5c91dccc9e
Author | SHA1 | Date | |
---|---|---|---|
5c91dccc9e | |||
a188f8ee5d | |||
87193d0f94 | |||
bd693f6f42 | |||
ec88faa437 |
2 changed files with 12 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
*.conf
|
*.conf
|
||||||
*.txt
|
*.txt
|
||||||
log/
|
log/
|
||||||
|
*.bak
|
15
discorss.py
15
discorss.py
|
@ -27,6 +27,7 @@ log_file_name = r"/app.log"
|
||||||
# Yes, I know you "can't parse HTML with regex", but
|
# Yes, I know you "can't parse HTML with regex", but
|
||||||
# just watch me.
|
# just watch me.
|
||||||
html_filter = re.compile(r"\<\/?([A-Za-z \"\=])*\>")
|
html_filter = re.compile(r"\<\/?([A-Za-z \"\=])*\>")
|
||||||
|
success_codes = ['200', '201', '202', '203', '204', '205', '206']
|
||||||
|
|
||||||
|
|
||||||
def get_description(feed):
|
def get_description(feed):
|
||||||
|
@ -59,14 +60,18 @@ def main():
|
||||||
with open(config_file_path, "r") as config_file:
|
with open(config_file_path, "r") as config_file:
|
||||||
app_config = json.load(config_file)
|
app_config = json.load(config_file)
|
||||||
now = time.mktime(time.localtime())
|
now = time.mktime(time.localtime())
|
||||||
last_check = app_config["lastupdate"]
|
try:
|
||||||
|
last_check = app_config["lastupdate"]
|
||||||
|
except KeyError:
|
||||||
|
last_check = now - 21600 # first run, no lastupdate, check up to 6 hours ago
|
||||||
for i, hook in enumerate(app_config["feeds"]):
|
for i, hook in enumerate(app_config["feeds"]):
|
||||||
# Get the feed
|
# Get the feed
|
||||||
feed = feedparser.parse(hook["url"])
|
feed = feedparser.parse(hook["url"])
|
||||||
published_time = time.mktime(feed.entries[0]["published_parsed"])
|
published_time = time.mktime(feed.entries[0]["published_parsed"])
|
||||||
published_time = published_time + hook["offset"]
|
published_time = published_time + hook["offset"]
|
||||||
print("Parsing feed {}...".format(hook["name"]))
|
print("Parsing feed {}...".format(hook["name"]))
|
||||||
new_hash = hashlib.sha3_512(feed.entries[0]["title"])
|
# Hash the title of the latest post and use that to determine if it's been posted
|
||||||
|
new_hash = hashlib.sha3_512(bytes(feed.entries[0]["title"], 'utf-8')).hexdigest()
|
||||||
try:
|
try:
|
||||||
if hook["lasthash"] != new_hash:
|
if hook["lasthash"] != new_hash:
|
||||||
app_config["feeds"][i]["lasthash"] = new_hash
|
app_config["feeds"][i]["lasthash"] = new_hash
|
||||||
|
@ -101,12 +106,12 @@ def main():
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
}
|
}
|
||||||
webhook_string = json.dumps(webhook)
|
webhook_string = json.dumps(webhook)
|
||||||
# print(webhook_string)
|
|
||||||
if published_time > last_check and published_time < now:
|
if published_time > last_check:
|
||||||
r = requests.post(
|
r = requests.post(
|
||||||
hook["webhook"], data=webhook_string, headers=custom_header
|
hook["webhook"], data=webhook_string, headers=custom_header
|
||||||
)
|
)
|
||||||
if r.status_code != "200":
|
if r.status_code not in success_codes:
|
||||||
print(
|
print(
|
||||||
"Error {} while trying to post {}".format(
|
"Error {} while trying to post {}".format(
|
||||||
r.status_code, hook["webhook"]
|
r.status_code, hook["webhook"]
|
||||||
|
|
Loading…
Add table
Reference in a new issue