-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_output.py
90 lines (75 loc) · 2.9 KB
/
discord_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import logging
import time
import json
import pytz
from pprint import pprint
from datetime import datetime
from urllib.parse import urljoin, urlparse
from bs4 import BeautifulSoup, Comment
def cleanup(markup):
soup = BeautifulSoup(markup, "html.parser")
begining_element = soup.find(string=lambda text:isinstance(text,Comment) and "SC_OFF" in text)
return "".join([elem.get_text(separator="\n") for elem in begining_element.find_next_siblings('div')]).replace("View Poll", "").strip()
def output(*args, feed=None, item=None, session=None, **kwargs):
webhook_url = feed.get("webhook")
post_user = feed.get("user", "quack")
tzstr = feed.get("timezone", "").strip()
tz = pytz.timezone(tzstr) if tzstr else pytz.utc
if not webhook_url:
logging.error("webhook not set in config, nowhere to post")
return False
if feed.get('catchup'):
return True
url = item.get("link")
title = item.get("title")
author = item.get("author_detail").get("name")
author_link = item.get("author_detail").get("href")
if "media_thumbnail" in item:
image = item["media_thumbnail"][0].get("url", "")
else:
image = ""
published = datetime.fromtimestamp(time.mktime(item.get("published_parsed")))
published_str = published.replace(tzinfo=pytz.utc).astimezone(tz).strftime("%B %d, %Y at %I:%M %p")
redditsub = item["tags"][0]["label"]
summary = item.get("summary", "")
if "SC_OFF" not in summary:
summary = ""
else:
summary = cleanup(summary)
post = {
"username": post_user,
"embeds": [{
"color": 16729344,
"author": {
"name": author,
"url": author_link,
},
"title": title,
"description": summary,
"url": url,
"footer": {
"text": f"{redditsub} • Posted at {published_str}"
}
}]
}
r = session.get(f"https://www.reddit.com/user/{author[3:]}/about.json")
if r.ok:
avatar = r.json()['data'].get("icon_img", None)
logging.debug(f"requested avatar returned {avatar}")
if avatar:
post["embeds"][0]["author"]["icon_url"] = urljoin(avatar, urlparse(avatar).path)
else:
logging.error(f"request for profile failed with code {r.status_code} and content {r.content}")
if image:
post["embeds"][0]["image"] = {"url": image}
r = session.post(webhook_url, data=json.dumps(post), headers={"Content-Type": "application/json"})
if r.ok:
logging.debug("post to discord webhook appears successful")
return True
else:
if r.status_code == 429:
logging.debug("rate limited, skip for now")
return False
else:
logging.error(f"post to discord webhook failed with code {r.status_code} with content {r.content}")
return False