-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit.rb
executable file
·43 lines (36 loc) · 1.08 KB
/
reddit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'redditkit'
class RedditModule
# URL of Reddit website.
REDDIT_URL = 'https://www.reddit.com'
#Initialize the client object for Reddit account
def initialize
@client = RedditKit::Client.new
@client.api_endpoint = REDDIT_URL
@client.sign_in(ENV["REDDIT_USERNAME"], ENV["REDDIT_PASSWORD"])
end
# Get the Today I Learned post from Reddit
def get_til
# Get top 100 posts from TIL subreddit.
posts = @client.links 'todayILearned', :category => :top, :time => :today, :limit => 100
title = get_title(posts)
title = replace_prefix(title)
puts title
title
end
# Fetch random post from posts and get the title from the post
def get_title(posts)
posts = posts.to_a
random_post = posts[rand(100)].title # Get random post from TIL.
title = random_post.downcase
puts title
title
end
# Remove the prefix like TIL, Til that, etc., form the title
def replace_prefix(title)
title =title.gsub("til that","")
title =title.gsub("til of", "")
title =title.gsub("til","")
title =title.gsub("til,","")
title = title.strip.capitalize
end
end