Skip to content

Commit

Permalink
Merge pull request #5 from TomCasavant/old-reddit
Browse files Browse the repository at this point in the history
Added ability to change reddit base URL
  • Loading branch information
TomCasavant authored Nov 14, 2022
2 parents cde10b5 + e6f1bd7 commit 8012587
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Tom Casavant
Copyright (c) 2022 Tom Casavant

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 2 additions & 0 deletions base-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Reddit URL, set to old.reddit.com, etc.
reddit_base: reddit.com # https://{reddit_base}/r/...
6 changes: 3 additions & 3 deletions maubot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ maubot: 0.1.0
id: casavant.tom.reddit

# A PEP 440 compliant version string.
version: 1.4.0
version: 1.5.0

# The SPDX license identifier for the plugin. https://spdx.org/licenses/
# Optional, assumes all rights reserved if omitted.
Expand All @@ -32,8 +32,8 @@ main_class: RedditPlugin
database: false

# Extra files that the upcoming build tool should include in the mbp file.
#extra_files:
#- base-config.yaml
extra_files:
- base-config.yaml
#- LICENSE

# List of dependencies
Expand Down
18 changes: 16 additions & 2 deletions reddit.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
from random import choice
from typing import List, Tuple
from typing import List, Tuple, Type
import urllib.parse
from maubot import Plugin, MessageEvent
from maubot.handlers import command
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper

# Setup config file
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("reddit_base")

class RedditPlugin(Plugin):
async def start(self) -> None:
await super().start()
self.config.load_and_update()

@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config

@command.passive("((^| )r\/)([^\s^,^.]+)", multiple=True)
async def handler(self, evt: MessageEvent, subs: List[Tuple[str, str]]) -> None:
await evt.mark_read()
subreddits = [] # List of all subreddits given by user
for _, r_slash, __, sub_str in subs:
link = "https://reddit.com/r/{}".format(urllib.parse.quote(sub_str.lower()))
base_url = self.config["reddit_base"]
link = "https://{}/r/{}".format(base_url, urllib.parse.quote(sub_str.lower()))

async with self.http.head(
link, headers={"User-agent": "redditmaubot"}, allow_redirects=True
Expand Down

0 comments on commit 8012587

Please sign in to comment.