Skip to content

Commit

Permalink
feat: support more reddit sorting
Browse files Browse the repository at this point in the history
Added "new", "top" and "rising"
  • Loading branch information
Massolari committed Jan 25, 2024
1 parent b010037 commit 7e6db54
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Reddit to Telegram

This is a script that will send hot posts from a subreddit to a Telegram channel.
This is a script that will send hot/new/top/rising posts from a subreddit to a Telegram channel.

## Usage

Expand Down Expand Up @@ -63,13 +63,10 @@ TELEGRAM_TOKEN=
```json
[
{
"subreddit": "golang",
"channel": "@golang"
"subreddit": "gleam",
"reddit_sort": "hot", // new, top, rising
"telegram_channel": "@r_gleam"
},
{
"subreddit": "rust",
"channel": "@rustlang"
}
]
```

1 change: 1 addition & 0 deletions bridges.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"subreddit": "neovim",
"reddit_sort": "hot",
"telegram_channel": "@reddit_test_channel"
}
]
27 changes: 25 additions & 2 deletions src/bridge.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import gleam/dynamic
import simplifile
import gleam/json
import gleam/result
import gleam/option
import reddit

pub type Bridge {
Bridge(subreddit: String, telegram_channel: String)
Bridge(subreddit: String, reddit_sort: reddit.Sort, telegram_channel: String)
}

pub fn get() -> Result(List(Bridge), String) {
Expand All @@ -19,9 +21,30 @@ fn bridges_decoder(json: String) -> Result(List(Bridge), String) {
}

fn bridge_decoder() -> dynamic.Decoder(Bridge) {
dynamic.decode2(
dynamic.decode3(
Bridge,
dynamic.field("subreddit", dynamic.string),
reddit_sort_decoder(),
dynamic.field("telegram_channel", dynamic.string),
)
}

fn reddit_sort_decoder() -> dynamic.Decoder(reddit.Sort) {
fn(json) {
json
|> dynamic.optional_field("reddit_sort", fn(dynamic_sort) {
dynamic_sort
|> dynamic.string
|> result.try(fn(sort) {
case sort {
"hot" -> Ok(reddit.Hot)
"new" -> Ok(reddit.New)
"top" -> Ok(reddit.Top)
"rising" -> Ok(reddit.Rising)
_ -> Error([])
}
})
})
|> result.map(option.unwrap(_, reddit.Hot))
}
}
29 changes: 25 additions & 4 deletions src/reddit.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ pub type Post {
)
}

pub type Sort {
Hot
New
Top
Rising
}

pub type Media {
Media(url: String, type_: MediaType)
}
Expand All @@ -35,10 +42,14 @@ pub fn short_link(post: Post) -> String {
"https://redd.it/" <> post.id
}

pub fn get_posts(data: AppData, subreddit: String) -> Result(List(Post), Nil) {
pub fn get_posts(
data: AppData,
subreddit: String,
sort: Sort,
) -> Result(List(Post), Nil) {
use token <- result.try(get_token(data))

get_hot(token, data, subreddit)
get_threads(token, data, subreddit, sort)
}

fn get_token(data: AppData) -> Result(String, Nil) {
Expand Down Expand Up @@ -86,15 +97,25 @@ fn set_user_agent(request: Request(a), data: AppData) -> Request(a) {
)
}

fn get_hot(
fn get_threads(
token: String,
data: AppData,
subreddit: String,
sort: Sort,
) -> Result(List(Post), Nil) {
let sort_string = case sort {
Hot -> "hot"
New -> "new"
Top -> "top"
Rising -> "rising"
}

use request <- result.try(request.to(
"https://oauth.reddit.com/r/"
<> subreddit
<> "/hot?limit=10",
<> "/"
<> sort_string
<> "?limit=10",
))

request
Expand Down
3 changes: 2 additions & 1 deletion src/reddit_to_telegram.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fn start(
use bridge <- list.each(bridges)

io.println("Getting posts from subreddit " <> bridge.subreddit <> "...")
let result_posts = reddit.get_posts(data, bridge.subreddit)
let result_posts =
reddit.get_posts(data, bridge.subreddit, bridge.reddit_sort)

case result_posts {
Ok(posts) -> {
Expand Down

0 comments on commit 7e6db54

Please sign in to comment.