import requests from flask import g from files.helpers.config.const import * from files.classes.comment import Comment from files.helpers.sanitize import * from files.helpers.alerts import push_notif from files.classes.notifications import Notification def notify(text, created_utc): text = sanitize(text, blackjack="offsite mention", golden=False) existing_comment = g.db.query(Comment.id).filter_by( author_id=AUTOJANNY_ID, parent_post=None, body_html=text).one_or_none() if existing_comment: return 1 new_comment = Comment( author_id=AUTOJANNY_ID, parent_post=None, body_html=text, distinguished=True, created_utc=created_utc, ) g.db.add(new_comment) g.db.flush() new_comment.top_comment_id = new_comment.id def reddit_mentions_task(): for kind in ("submission", "comment"): q = " or ".join(OFFSITE_NOTIF_QUERIES) url = f'https://api.pullpush.io/reddit/search/{kind}?q={q}' try: data = requests.get(url, headers=HEADERS, timeout=20).json()['data'] except Exception as e: print(e, flush=True) return for thing in data: if not thing.get('permalink'): continue if thing.get('subreddit_subscribers') and thing['subreddit_subscribers'] < 2:continue if thing.get('subreddit_type') == 'user': continue if thing['subreddit'] in {'IAmA', 'PokemonGoRaids', 'SubSimulatorGPT2', 'SubSimGPT2Interactive'}: continue if 'bot' in thing['author'].lower(): continue if thing['author'] == 'AutoModerator': continue if kind == 'comment': body = thing["body"].replace('>', '> ') if SITE_NAME == 'rDrama' and 'teenager' in body: continue text = f'

{body}

' else: title = thing["title"].replace('>', '> ') if 'Kathrine Mclaurin' in title: continue text = f'

{title}

' if thing["selftext"]: selftext = thing["selftext"].replace('>', '> ')[:5000] text += f'

{selftext}

' author_string = f"/u/{thing['author']}" permalink = 'https://old.reddit.com' + thing['permalink'] text = f'New site mention by {author_string}\n\n{permalink}\n\n{text}' created_utc = thing['created_utc'] if notify(text, created_utc) == 1: break def lemmy_mentions_task(): for q in OFFSITE_NOTIF_QUERIES: url = f'https://lemm.ee/api/v3/search?q={q}' try: data = requests.get(url, headers=HEADERS, timeout=20, proxies=proxies).json() except Exception as e: print(e, flush=True) return for kind in ("post", "comment"): for thing in data[f'{kind}s']: creator = thing['creator'] author_string = f"[/u/{creator['name']}]({creator['actor_id']})" thing = thing[kind] if kind == 'comment': body = thing["content"] text = f'

{body}

' else: title = thing["name"] text = f'

{title}

' if thing.get("body"): selftext = thing["body"][:5000] text += f'

{selftext}

' if 'erdrama' in text.lower(): continue permalink = thing['ap_id'] text = f'New site mention by {author_string}\n\n{permalink}\n\n{text}' try: created_utc = int(time.mktime(time.strptime(thing['published'].split('.')[0], "%Y-%m-%dT%H:%M:%S"))) except: created_utc = int(time.mktime(time.strptime(thing['published'].split('.')[0], "%Y-%m-%dT%H:%M:%SZ"))) if notify(text, created_utc) == 1: break def fourchan_mentions_task(): queries = OFFSITE_NOTIF_QUERIES - {'r/drama'} for q in queries: url = f'https://archived.moe/_/api/chan/search?text={q}' try: data = requests.get(url, headers=HEADERS, timeout=20, proxies=proxies).json()['0']['posts'] except Exception as e: print(e, flush=True) return for thing in data: board = thing['board']['shortname'] author_string = thing['name'] num = thing["num"] thread_num = thing["thread_num"] if num != thread_num: text = f'

{thing["comment"]}

' permalink = f'https://archived.moe/{board}/thread/{thread_num}/#{num}' else: text = f'

{thing["title"]}


{thing["comment"]}

' permalink = f'https://archived.moe/{board}/thread/{thread_num}' text = f'New site mention by {author_string}\n\n{permalink}\n\n{text}' created_utc = thing["timestamp"] if notify(text, created_utc) == 1: break def soyjak_mentions_task(): for q in OFFSITE_NOTIF_QUERIES: url = f'https://api.marge.moe/search?q={q}' try: data = requests.get(url, headers={"User-Agent": "MarseyFromWPD"}, timeout=20, proxies=proxies).json()['results'] except Exception as e: print(e, flush=True) return for thing in data: text = f'

{thing["comment"]}

' if 'erdrama' in text.lower(): continue if 'rdrama won' in text.lower(): continue permalink = thing['url'] text = f'New site mention\n\n{permalink}\n\n{text}' created_utc = int(time.mktime(time.strptime(thing['date'].split('.')[0], "%Y-%m-%dT%H:%M:%S"))) if notify(text, created_utc) == 1: break