-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarkdown_updater.rb
62 lines (49 loc) · 2.08 KB
/
markdown_updater.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require "redd"
require "dotenv"
Dotenv.load(File.join(File.dirname(__FILE__), '.env'))
template = File.join(File.dirname(__FILE__), "template.md")
@current_template = File.join(File.dirname(__FILE__), "current.md")
@new_markdown = open(template, &:read)
@current_markdown = open(@current_template, &:read)
@redd = Redd::Client::Authenticated.new_from_credentials(ENV["USERNAME"], ENV["PASSWORD"])
settings = @redd.about_edit(ENV["SUBREDDIT"])
@attrs = settings.attributes.clone
# SUBREDDIT SETTINGS
# API requires all of these fields
@attrs[:allow_top] = false
@attrs[:css_on_cname] = true
@attrs[:lang] = @attrs[:language]
@attrs[:link_type] = @attrs[:content_options]
@attrs[:name] = ENV["SUBREDDIT"]
@attrs[:show_cname_sidebar] = true
@attrs[:sr] = @attrs[:subreddit_id]
@attrs[:submit_link_label] = ''
@attrs[:submit_text_label] = ''
@attrs[:type] = @attrs[:subreddit_type]
@attrs[:description] = ''
def updateStreamers(current_streamers, all_streamers)
current_streamers.each { |user|
online_text = "
[#{user[:name]}](http://www.twitch.tv/#{user[:name]} 'twitch-online') \n
*→ playing #{user[:game]}*"
@new_markdown.sub! "twitchit(#{user[:name]})", online_text
}
all_streamers.each { |user|
offline_text = "[#{user}](http://www.twitch.tv/#{user} 'twitch-offline')"
@new_markdown.sub! "twitchit(#{user})", offline_text
}
# Convert to proper markdown because Reddit API doesn't return these as markdown
@new_markdown.gsub! '>', '>'
@new_markdown.gsub! '<', '<'
# If current markdown is already accurate then exit
if @current_markdown == @new_markdown
puts Time.now.to_s + ' Current markdown is up-to-date.'
exit
end
# Update local current to avoid hitting Reddit API to check if markdown needs updating
File.open(@current_template, 'w') { |file| file.write(@new_markdown) }
result = @redd.edit_stylesheet(ENV["SUBREDDIT"], @new_markdown)
# Update the live markdown
@attrs[:description] = @new_markdown
@redd.site_admin(@attrs)
end