Skip to content

Instantly share code, notes, and snippets.

@xoebus
Last active July 30, 2023 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoebus/770334 to your computer and use it in GitHub Desktop.
Save xoebus/770334 to your computer and use it in GitHub Desktop.
Some code to grab Minecraft codes off reddit.
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
@2010Programmer
Copy link

How do you execute this?

@NoobGamerZaid
Copy link

have you figured it out??

@RaahimFahd
Copy link

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