Created
December 7, 2014 04:30
-
-
Save komasaru/0ccca36036920eecb555 to your computer and use it in GitHub Desktop.
Ruby script to check existance of twitter accounts.
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
#! /usr/local/bin/ruby | |
# coding: utf-8 | |
#= Twitter アカウント登録済みチェック | |
# | |
# date name version | |
# 2014.12.07 mk-mode.com 1.00 新規作成 | |
# | |
# Copyright(C) 2014 mk-mode.com All Rights Reserved. | |
#--------------------------------------------------------------------------------- | |
# 引数 : なし | |
#--------------------------------------------------------------------------------- | |
#++ | |
require 'net/http' | |
class CheckTwitterAccount | |
URL = "https://twitter.com/" | |
ACCOUNTS = [ | |
"hogehoge", | |
"fugafuga" | |
] | |
# Main process | |
def exec | |
begin | |
ACCOUNTS.each do |ac| | |
res = url_request("#{URL}#{ac}") | |
puts res ? " * #{ac}" : " - #{ac}" | |
end | |
rescue => e | |
STDERR.puts "[#{e.class}] #{e.message}" | |
e.backtrace.each{|trace| STDERR.puts "\t#{trace}"} | |
exit 1 | |
end | |
end | |
private | |
# Check URL request recursively | |
# @param url[String] | |
# @param limit[Integer] | |
# @return [Boolean] | |
def url_request(url, limit = 5) | |
return false if limit == 0 | |
begin | |
res = Net::HTTP.get_response(URI.parse(url)) | |
rescue | |
return false | |
else | |
case res | |
when Net::HTTPSuccess | |
return true | |
when Net::HTTPRedirection | |
url_request(res['location'], limit - 1) | |
else | |
return false | |
end | |
end | |
end | |
end | |
CheckTwitterAccount.new.exec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment