Moriarty - Tool to check social networks for available username
GIF preview v-0.2.0 - without latest changes'
Gem is still in alpha, many things will be added (like scrapping info from social networks)
Breaking changes are highly possible, so I do not suggest use of this gem except for development and test
That means, do not use it in production services, otherwise feel free to play with it.
Moriarty
Tool to check social networks for available username.
Idea from python tool - sherlock
What it do
Search multiple social networks for free username.
It's reverse from Sherlock, so not-found username is success.
Argument --hunt will run search like sherlock would, looking for valid users.
How to install
Clone repo and install dependencies:
# Moriarty use rest-client, nokogiri and colorize gems
git clone https://github.com/decentralizuj/moriarty.git &&cd moriarty && bundle install
Or install from rubygems:
DISCLAIMER
GitHub repository is updated before Rubygems.
Before v-1.0.0 is released, recommended way is to clone repo,…
First code is never good enough, especially in Ruby, where everything could be done in many different ways. I'll start with initializer:
# Set username and site for search request# exclude 'https', #make_url will add it for you# To use different protocol, set it as third parameter## @jim = Moriarty.new( :moriarty, 'example.com', :http )# => @jim.user == 'moriarty'# => @jim.url == 'http://example.com/'definitialize(name='',site='github.com',prot=:https)@user=name.to_s@url=make_urlsite,protend
Here we added 'make_url(site_name, protocol)'. This method purpose is to construct URL from string, so we can use dev.to instead of https://dev.to.
# create URL from site name, add 'https' if needed# @jim.make_url 'github.com'# => 'https://github.com/'defmake_url(link,prot=:https)prot=niliflink.to_s.start_with?prot.to_surl=prot.nil??link.to_s:prot.to_s+'://'+link.to_surl+='/'unlessurl.end_with?('/')returnurlend
Here we can add parameter site: github.com in Moriarty#go, without https://. In first part I said that we'll create setters manually. Now we can use #make_url in setter to create link from string.
# Set URL from site name and protocol(optional)# @jim.url = 'github.com'# => @jim.url == 'https://github.com'defurl=(link,prot=:https)@url=make_urllink,protend# Set username from string or :symbol# @jim.user = :moriarty# => @jim.user == 'moriarty'defuser=(name)@user=name.to_send
It's time to edit Moriarty#go, method in charge to send request.
# execute request (args are optional)# @jim.go site: 'github.com', user: 'mynickname'# => true/false# -> @jim.response (.code, .headers, .body)# -> @jim.html (page HTML if request success)defgo(opt={})# define username to search, or use defaultopt[:user]||=@user# if option 'site:' is 'nil', use default# otherwise construct url with '#make_url'url=opt[:site].nil??@url:make_url(opt[:site])# create url for request, and senduri=url+opt[:user]@response=RestClient.geturi# try to scrap data from page@html=Nokogiri::HTML@response# return true or falsereturn@success=truerescuereturn@success=falseend# add alias search, just because of conventionaliassearchgo
There's one more thing that we should add here, to check for response success:
# Check does request succeeded or not# @jim.success?# => true/falsedefsuccess?@success==trueend
Create method to execute search request, and output data in terminal. Default method is search for free username, in that case fail requests mean username is free. If we define :hunt as type parameter, we will search for registered user.
# Find method is responsible to handle all other methods# and to tell use final resultsdeffind!(user=@user,web=@url,type=:search)@jim.gouser: user,site: sitename=print_name(user).to_ssite=print_url(web).to_scasewhentype.to_sym==:hunt&&@jim.success?puts"#{name} found on #{site}"whentype.to_sym==:hunt&&!@jim.success?puts"#{name} not found #{site}"when@jim.success?puts"#{name} is taken on #{site}"elseputs"#{name} is free on #{site}"endend
Methods #print_name and #print_url prepare strings for printing in cli. Names are cleaned from signs like @, #, /u/, and site is stripped of extensions and capitalized.
# Remove extensions from domain name# Moriarty.print_url('https://www.github.com')# => 'github'defprint_url(site)# define site from string# make name empty stringsite,name=site.to_s,''# if site start with 'http', try to remove 'https'# if not, then remove 'http'ifsite.start_with?('http')site.gsub!("https://",'')orsite.gsub!("http://",'')end# remove 'www.' from sitename, if includedsite.gsub!("www.",'')ifsite.start_with?('www.')# split domain on '.' to get extension, and remove itext=site.to_s.split('.').lastname=site.gsub(".#{ext}",'')# if ext is longer then 5, routes contain more data# example: 'rubygems.org/profiles/*'name=name.split('/').firstifext.size<5# first letter uppercasereturnname.capitalizeend# Remove extensions from username# @jim.print_name('@moriarty') # => 'moriarty'defprint_name(name)name.gsub!('@','')ifname.to_s.start_with?('@')name.gsub!('#','')ifname.to_s.start_with?('#')name.gsub!('/u/','')ifname.to_s.start_with?('/u/')returnnameend
Now we have class with all objects needed to do what we want it to do. For sites that need to check html for success (like twitter), there's variable @html ( getter Moriarty.html ).
Let's run search from terminal. How? We need to define sites to search, and usernames. We'll define sites as array, and accept usernames as arguments. If we add --hunt, it will run in hunt mode, otherwise in default (search for free name).
# define sites to searchSITES=['github.com','dev.to'].freeze# run hunt if arg --hunt included, otherwise searchhunt=ARGV.include?('--hunt')?:hunt::searchunlessARGV.empty?@jim=Moriarty.new:jimARGV.eachdo|user|SITES.eachdo|site|nextifuser=='--hunt'@jim.find!user,site,huntendendend
Thanks all for reading this post(s), don't forget to share content...somewhere :) In next article I'll pack and compile this into a gem.