Created
September 2, 2014 00:26
-
-
Save hilotter/a08c87b3da3aeba195d1 to your computer and use it in GitHub Desktop.
twitter screen_nameからユーザidを取得するサンプル
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 'sinatra' | |
require 'erb' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'pry' | |
get '/' do | |
@screen_name = "xxxx" | |
erb :index | |
end | |
post '/' do | |
uid = @params[:uid] | |
redirect back if uid.nil? || uid.empty? | |
begin | |
doc = Nokogiri::HTML(open("https://twitter.com/intent/user?screen_name=#{uid}")) | |
native_url = doc.css('meta[name="native-url"]').first.attribute('content').value | |
res = native_url.match(%r{^twitter://user\?id=(\d+)$}) | |
@screen_name = res[1] | |
rescue OpenURI::HTTPError => e | |
if e.message == '404 Not Found' | |
@screen_name = "404 Not Found" | |
else | |
raise e | |
end | |
end | |
erb :index | |
end | |
__END__ | |
@@index | |
<html> | |
<body> | |
<p>twitter screen_name converter</p> | |
<form action="/" method="post"> | |
<input type="text" maxlength="30" name="uid" placeholder="input screen_name"> | |
<input type="submit"> | |
</form> | |
uid is <%= @screen_name %> | |
</body> | |
</html> |
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
source 'https://rubygems.org' | |
gem 'sinatra' | |
gem 'nokogiri' | |
gem 'pry' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment