Last active
July 30, 2023 11:18
-
-
Save xoebus/770334 to your computer and use it in GitHub Desktop.
Some code to grab Minecraft codes off reddit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nibbler' | |
require 'open-uri' | |
CODE_RE = /[A-Za-z1-9]{4}-[A-Za-z1-9]{4}-[A-Za-z1-9]{4}/ | |
class String | |
[:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white].each_with_index do |color, i| | |
define_method color do "\033[1;#{30+i}m#{self}\033[0m" end | |
define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end | |
end | |
end | |
class Link < Nibbler | |
element 'a.title' => :title | |
element './/div/p/a/@href' => :url | |
element './/div/ul/li/a/@href' => :comments | |
def minecraft_link? | |
!!title[/minecraft.*codes?/i] | |
end | |
end | |
class SubReddit < Nibbler | |
elements '.link' => :links, :with => Link | |
end | |
gaming = SubReddit.parse open('http://www.reddit.com/r/gaming') | |
gaming.links.each do |link| | |
if link.minecraft_link? | |
puts "Found link: #{link.title.purple} - #{link.url.yellow}" | |
link_codes = open(link.url).read.scan(CODE_RE) | |
comment_codes = open(link.comments).read.scan(CODE_RE) | |
# Some random stuff in the HTML matches the code regex. If it | |
# doesn't have a number in it then it probably isn't a link. | |
all_codes = (link_codes | comment_codes).select { |s| s =~ /\d/} | |
all_codes.uniq.each do |code| | |
puts " #{code.green}" | |
end | |
end | |
end |
have you figured it out??
I think you run it in Rudy IDE, you can try it out by downloading it and then do tell me if it worked.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you execute this?