2020-12-22 18:03:48 +00:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
# Copyright (c) 2020, The Monero Project.
|
|
|
|
# Copyright (c) 2020, dsc@xmr.pm
|
|
|
|
|
2020-12-30 07:25:47 +00:00
|
|
|
import html
|
2020-12-22 18:03:48 +00:00
|
|
|
import settings
|
2021-04-05 17:49:02 +00:00
|
|
|
from wowlet_backend.utils import httpget
|
2021-05-02 22:36:44 +00:00
|
|
|
from wowlet_backend.tasks import WowletTask
|
2020-12-22 18:03:48 +00:00
|
|
|
|
|
|
|
|
2021-05-02 22:36:44 +00:00
|
|
|
class RedditTask(WowletTask):
|
2020-12-22 18:03:48 +00:00
|
|
|
def __init__(self, interval: int = 900):
|
2021-04-05 17:49:02 +00:00
|
|
|
from wowlet_backend.factory import app
|
2020-12-22 18:03:48 +00:00
|
|
|
super(RedditTask, self).__init__(interval)
|
|
|
|
|
|
|
|
self._cache_key = "reddit"
|
|
|
|
self._cache_expiry = self.interval * 10
|
|
|
|
|
|
|
|
self._websocket_cmd = "reddit"
|
|
|
|
|
|
|
|
self._http_endpoints = {
|
|
|
|
"xmr": "https://www.reddit.com/r/monero",
|
|
|
|
"wow": "https://www.reddit.com/r/wownero",
|
|
|
|
"aeon": "https://www.reddit.com/r/aeon",
|
|
|
|
"trtl": "https://www.reddit.com/r/TRTL",
|
|
|
|
"xhv": "https://www.reddit.com/r/havenprotocol",
|
|
|
|
"loki": "https://www.reddit.com/r/LokiProject"
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.COIN_SYMBOL not in self._http_endpoints:
|
|
|
|
app.logger.warning(f"Missing Reddit URL for {settings.COIN_SYMBOL.upper()}, ignoring update task")
|
|
|
|
self._active = False
|
|
|
|
|
|
|
|
self._http_endpoint = self._http_endpoints[settings.COIN_SYMBOL]
|
|
|
|
if self._http_endpoint.endswith("/"):
|
|
|
|
self._http_endpoint = self._http_endpoint[:-1]
|
|
|
|
|
|
|
|
async def task(self):
|
2021-04-05 17:49:02 +00:00
|
|
|
from wowlet_backend.factory import app
|
2020-12-22 18:03:48 +00:00
|
|
|
|
|
|
|
url = f"{self._http_endpoint}/new.json?limit=15"
|
|
|
|
try:
|
|
|
|
blob = await httpget(url, json=True, raise_for_status=True)
|
|
|
|
except Exception as ex:
|
|
|
|
app.logger.error(f"failed fetching '{url}' {ex}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
blob = [{
|
2020-12-30 07:25:47 +00:00
|
|
|
'title': html.unescape(z['data']['title']),
|
2020-12-22 18:03:48 +00:00
|
|
|
'author': z['data']['author'],
|
2020-12-29 19:25:04 +00:00
|
|
|
'url': "https://old.reddit.com" + z['data']['permalink'], # legacy
|
|
|
|
'permalink': z['data']['permalink'],
|
2020-12-22 18:03:48 +00:00
|
|
|
'comments': z['data']['num_comments']
|
|
|
|
} for z in blob['data']['children']]
|
|
|
|
if not blob:
|
|
|
|
raise Exception("no content")
|
|
|
|
|
|
|
|
return blob
|