Skip to content

Commit

Permalink
FUCK YOU TWITTER!!!!
Browse files Browse the repository at this point in the history
After the twitter gem and API have proven to be flawed and unreliable
I've decided to remove gut it.

I've resorted to screenscraping for tweets. I'm not proud of it,
It works, however, and I am losing zero functionality. Though, responce
times will be slower since instead of fetching a tiny JSON object the
entire web page is being fetched and then parsed with nokogiri. I'm
doing some magic with XPATH since, unfortunately, nokogiri did not
like me having two nots in my css selectors.

Note the relevant ticket: sparklemotion/nokogiri#451
  • Loading branch information
LindseyB committed Mar 16, 2013
1 parent 3427ea6 commit f40d9d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source :rubygems
gem "cinch", "~> 1.1.3"
gem "mechanize"
gem "twitter"
gem "nokogiri"
gem "htmlentities"
gem "json"
11 changes: 1 addition & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ GEM
cinch (1.1.3)
domain_name (0.5.6)
unf (~> 0.0.3)
faraday (0.8.4)
multipart-post (~> 1.1)
htmlentities (4.3.1)
json (1.7.6)
mechanize (2.5.1)
Expand All @@ -17,17 +15,10 @@ GEM
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (~> 0.0, >= 0.0.9)
mime-types (1.19)
multi_json (1.5.0)
multipart-post (1.1.5)
net-http-digest_auth (1.2.1)
net-http-persistent (2.8)
nokogiri (1.5.6)
ntlm-http (0.1.1)
simple_oauth (0.2.0)
twitter (4.4.3)
faraday (~> 0.8, < 0.10)
multi_json (~> 1.3)
simple_oauth (~> 0.2)
unf (0.0.5)
unf_ext
unf_ext (0.0.5)
Expand All @@ -41,4 +32,4 @@ DEPENDENCIES
htmlentities
json
mechanize
twitter
nokogiri
15 changes: 10 additions & 5 deletions lib/xombot/plugins/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'open-uri'

class Tweet < XOmBot::Plugin
match /tweet (\d+)/, :method => :tweet_by_id
help "Displays the tweet with the given id"
Expand All @@ -17,16 +19,19 @@ def setup
end

def tweet_by_id(m, id)
status = Twitter.status(id)
m.reply "@#{status.user.screen_name}: #{HTMLEntities.new.decode status.text}"
tweet_by_url m, "https://twitter.com/a/status/#{id}"
end

def tweet_by_username(m, username)
m.reply "@#{username}: #{HTMLEntities.new.decode Twitter.user_timeline(username).first.text}"
doc = Nokogiri::HTML(open("https://twitter.com/#{username}"))
# terrible selector to grab the first tweet that isn't a reply or retweeted
tweet = doc.xpath("//*[contains(concat(' ', @class, ' '), ' original-tweet ') and not(@data-is-reply-to = \"true\") and not(@data-retweet-id)]")
.first
tweet ? tweet_by_id(m, tweet.attr('data-tweet-id')) : m.reply("#{username} doesn't have any recent tweets")
end

def tweet_by_url(m, url)
id = url[/^https?:\/\/.*\/status\/(\d+)/,1]
tweet_by_id m, id
doc = Nokogiri::HTML(open(url))
m.reply "#{doc.css('.permalink-tweet-container .username.js-action-profile-name').first.text}: #{doc.css('.tweet-text').first.text}"
end
end

0 comments on commit f40d9d4

Please sign in to comment.