alx / twitticious
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
83e7f67
twitticious / twitticious.rb
| ae6328c6 » | alx | 2008-12-21 | 1 | # myapp.rb | |
| 2 | require 'rubygems' | ||||
| 3 | require 'sinatra' | ||||
| 4 | require 'rdelicious' | ||||
| 5 | |||||
| 6 | get '/' do | ||||
| 7 | erb :index | ||||
| 8 | end | ||||
| 9 | |||||
| 10 | post '/' do | ||||
| 11 | process_request | ||||
| 12 | erb :index | ||||
| 13 | end | ||||
| 14 | |||||
| 15 | helpers do | ||||
| 16 | |||||
| 17 | def process_request | ||||
| 18 | |||||
| 19 | # Verify twitter id is correct | ||||
| 20 | if is_twitter_id?(params[:twitter_id]) or params[:twitter_public] == "yes" | ||||
| 21 | @delicious = Rdelicious.new(params[:delicious_id], params[:delicious_password]) | ||||
| 22 | |||||
| 23 | # Verify we can access delicious account | ||||
| 24 | if @delicious.is_connected? | ||||
| 25 | |||||
| 26 | # Travelling around twitter timeline | ||||
| 27 | if params[:twitter_public] == "yes" | ||||
| 28 | # Read public timeline | ||||
| 29 | twitter = read_public_twitter() | ||||
| 30 | else | ||||
| 31 | # Read user timeline | ||||
| 32 | if params[:twitter_friends] == "include" | ||||
| 33 | # Include friends | ||||
| 34 | twitter = read_twitter(params[:twitter_id], true) | ||||
| 35 | else | ||||
| 36 | # Without friend network | ||||
| 37 | twitter = read_twitter(params[:twitter_id]) | ||||
| 38 | end | ||||
| 39 | end | ||||
| 40 | |||||
| 41 | twitter.each_element('//text') do |element| | ||||
| 42 | description = element.text | ||||
| 43 | # Scan for url in twitter status item | ||||
| 44 | url = description.scan(/(http:\/\/.[^<\s$]*)/)[0].to_s | ||||
| 45 | tags = description.scan(/\+.[^\s]*/).join(" ").delete('+') | ||||
| 46 | # Verify url is not null and doesnt exists in delicious account | ||||
| 47 | if !url.nil? and !url.empty? and !@delicious.url_exists?(url) | ||||
| 48 | description.slice!(url) # Remove url from description | ||||
| 49 | description.slice!(/\+.[^\s]*/) # Remove tags | ||||
| 50 | description.slice!(/:\s*$/) # Remove trailing ":" used to send url in twitter | ||||
| 51 | @delicious.add(url, description, tags) | ||||
| 52 | end | ||||
| 53 | end | ||||
| 54 | else | ||||
| 55 | error = "Please verify your Delicious information" | ||||
| 56 | end | ||||
| 57 | else | ||||
| 58 | error = "Please verify your Twitter information" | ||||
| 59 | end | ||||
| 60 | |||||
| 61 | return [@delicious, error] | ||||
| 62 | end | ||||
| 63 | |||||
| 64 | ### | ||||
| 65 | # Twitter methods | ||||
| 66 | ### | ||||
| 67 | |||||
| 68 | # Return true if twitter_id exists | ||||
| 69 | def is_twitter_id?(twitter_id) | ||||
| 70 | resp = twitter_request(twitter_id) | ||||
| 71 | # Response is not empty and doesn't contain 404 or "not found" | ||||
| 72 | return (!resp.empty? and ((resp =~ /404/).nil? or (resp =~ /not found/).nil?)) | ||||
| 73 | end | ||||
| 74 | |||||
| 75 | # Return an array of last element of twitter_id timeline | ||||
| 76 | def read_twitter(twitter_id, include_friends = false) | ||||
| 77 | resp = twitter_request(twitter_id, include_friends) | ||||
| 78 | begin | ||||
| 79 | # XML Document | ||||
| 80 | return REXML::Document.new(resp) | ||||
| 81 | rescue REXML::ParseException => e | ||||
| 82 | return false | ||||
| 83 | end | ||||
| 84 | end | ||||
| 85 | |||||
| 86 | # Return an array of last element of twitter public timeline | ||||
| 87 | def read_public_twitter() | ||||
| 88 | return read_twitter("public_timeline") | ||||
| 89 | end | ||||
| 90 | |||||
| 91 | def twitter_request(twitter_id, include_friends = false) | ||||
| 92 | if include_friends | ||||
| 93 | request = "/statuses/friends_timeline/#{twitter_id}.xml" | ||||
| 94 | else | ||||
| 95 | request = "/statuses/user_timeline/#{twitter_id}.xml" | ||||
| 96 | end | ||||
| 97 | response = "" | ||||
| 98 | begin | ||||
| 99 | http = Net::HTTP.new("twitter.com") | ||||
| 100 | http.start do |http| | ||||
| 101 | req = Net::HTTP::Get.new(request, {"User-Agent" => "Twitticious"}) | ||||
| 102 | response = http.request(req).body | ||||
| 103 | end | ||||
| 104 | rescue SocketError | ||||
| 105 | return false | ||||
| 106 | end | ||||
| 107 | return response | ||||
| 108 | end | ||||
| 109 | |||||
| 110 | end | ||||
