Skip to content

Commit

Permalink
Add Reddit link replacement support
Browse files Browse the repository at this point in the history
Closes #306
Closes #353
  • Loading branch information
zedeus committed Dec 27, 2021
1 parent 1a08d00 commit 3c45630
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions nitter.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tokenCount = 10
theme = "Nitter"
replaceTwitter = "nitter.net"
replaceYouTube = "piped.kavin.rocks"
replaceReddit = "teddit.net"
replaceInstagram = ""
proxyVideos = true
hlsPlayback = false
Expand Down
16 changes: 15 additions & 1 deletion src/formatters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import types, utils, query

const
ytRegex = re"([A-z.]+\.)?youtu(be\.com|\.be)"
twRegex = re"(?<![^\/> ])(?<![^\/]\/)(www\.|mobile\.)?twitter\.com"
igRegex = re"(www\.)?instagram\.com"

rdRegex = re"(?<![.b])((www|np|new|amp|old)\.)?reddit.com"
rdShortRegex = re"(?<![.b])redd\.it\/"
# Videos cannot be supported uniformly between Teddit and Libreddit,
# so v.redd.it links will not be replaced.
# Images aren't supported due to errors from Teddit when the image
# wasn't first displayed via a post on the Teddit instance.

twRegex = re"(?<![^\/> ])(?<![^\/]\/)(www\.|mobile\.)?twitter\.com"
cards = "cards.twitter.com/cards"
tco = "https://t.co"

Expand Down Expand Up @@ -53,6 +61,12 @@ proc replaceUrl*(url: string; prefs: Prefs; absolute=""): string =
result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter)

if prefs.replaceReddit.len > 0 and (rdRegex in result or "redd.it" in result):
result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/")
result = result.replace(rdRegex, prefs.replaceReddit)
if prefs.replaceReddit in result and "/gallery/" in result:
result = result.replace("/gallery/", "/comments/")

if prefs.replaceInstagram.len > 0 and igRegex in result:
result = result.replace(igRegex, prefs.replaceInstagram)

Expand Down
4 changes: 4 additions & 0 deletions src/prefs_impl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ genPrefs:
"Replace YouTube links with Piped/Invidious (blank to disable)"
placeholder: "Piped hostname"

replaceReddit(input, "teddit.net"):
"Replace Reddit links with Teddit/Libreddit (blank to disable)"
placeholder: "Teddit hostname"

replaceInstagram(input, ""):
"Replace Instagram links with Bibliogram (blank to disable)"
placeholder: "Bibliogram hostname"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
['nim_lang/status/1110499584852353024', [
'nim-lang.org/araq/ownedrefs.…',
'news.ycombinator.com/item?id…',
'old.reddit.com/r/programming…'
'teddit.net/r/programming…'
]],
['nim_lang/status/1125887775151140864', [
'en.wikipedia.org/wiki/Nim_(p…'
Expand Down

0 comments on commit 3c45630

Please sign in to comment.