Class: TwitterParser
- Inherits:
-
Object
- Object
- TwitterParser
- Defined in:
- lib/twitter_parser.rb
Instance Method Summary collapse
-
#get_favorite_count ⇒ Object
Get the # of favorites.
-
#get_fullname ⇒ Object
Get the fullname.
-
#get_mentions ⇒ Object
Get the mentioned accounts (if any).
-
#get_reply_count ⇒ Object
Get the # of replies.
-
#get_reply_to_user ⇒ Object
Get the user tweet is replying to (if any).
-
#get_retweet_count ⇒ Object
Get the # of retweets.
-
#get_tweet_link ⇒ Object
Get the link to the tweet.
-
#get_tweet_text ⇒ Object
Get the tweet text.
-
#get_tweet_time ⇒ Object
Get the time for the tweet.
-
#get_user_id ⇒ Object
Get user ID number.
-
#get_username ⇒ Object
Get the username.
-
#initialize(tweet) ⇒ TwitterParser
constructor
A new instance of TwitterParser.
-
#parse_tweet ⇒ Object
Parse the individual tweet.
Constructor Details
#initialize(tweet) ⇒ TwitterParser
Returns a new instance of TwitterParser.
5 6 7 |
# File 'lib/twitter_parser.rb', line 5 def initialize(tweet) @tweet = Nokogiri::HTML.parse(tweet) end |
Instance Method Details
#get_favorite_count ⇒ Object
Get the # of favorites
66 67 68 |
# File 'lib/twitter_parser.rb', line 66 def get_favorite_count @tweet.css(".ProfileTweet-action--favorite")[0].css("span")[0]['data-tweet-stat-count'] end |
#get_fullname ⇒ Object
Get the fullname
36 37 38 |
# File 'lib/twitter_parser.rb', line 36 def get_fullname @tweet.css(".fullname").text end |
#get_mentions ⇒ Object
Get the mentioned accounts (if any)
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twitter_parser.rb', line 88 def get_mentions mentions = @tweet.css(".twitter-atreply") if !mentions.empty? mention_names = mentions.map{|t| t.text} mention_uids = mentions.map{|t| t['data-mentioned-user-id']} return mention_names, mention_uids else return nil, nil end end |
#get_reply_count ⇒ Object
Get the # of replies
71 72 73 |
# File 'lib/twitter_parser.rb', line 71 def get_reply_count @tweet.css(".ProfileTweet-action--reply")[0].css("span")[0]['data-tweet-stat-count'] end |
#get_reply_to_user ⇒ Object
Get the user tweet is replying to (if any)
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/twitter_parser.rb', line 76 def get_reply_to_user reply_to = @tweet.css("span").select{|s| s.text.include?("In reply")}[0] if reply_to reply_to_user = reply_to.css("a")[0]['href'].gsub("/", "@") reply_to_uid = reply_to.css("a")[0]['data-user-id'] return reply_to_user, reply_to_uid else return nil, nil end end |
#get_retweet_count ⇒ Object
Get the # of retweets
61 62 63 |
# File 'lib/twitter_parser.rb', line 61 def get_retweet_count @tweet.css(".ProfileTweet-action--retweet")[0].css("span")[0]['data-tweet-stat-count'] end |
#get_tweet_link ⇒ Object
Get the link to the tweet
56 57 58 |
# File 'lib/twitter_parser.rb', line 56 def get_tweet_link "https://twitter.com"+@tweet.css(".tweet-timestamp")[0]['href'] end |
#get_tweet_text ⇒ Object
Get the tweet text
46 47 48 |
# File 'lib/twitter_parser.rb', line 46 def get_tweet_text @tweet.css(".js-tweet-text-container").text.lstrip.strip end |
#get_tweet_time ⇒ Object
Get the time for the tweet
51 52 53 |
# File 'lib/twitter_parser.rb', line 51 def get_tweet_time DateTime.parse(@tweet.css(".tweet-timestamp")[0]["title"]).strftime('%d %b %Y %H:%M:%S') end |
#get_user_id ⇒ Object
Get user ID number
41 42 43 |
# File 'lib/twitter_parser.rb', line 41 def get_user_id @tweet.css(".js-user-profile-link").css(".account-group")[0]["data-user-id"] end |
#get_username ⇒ Object
Get the username
31 32 33 |
# File 'lib/twitter_parser.rb', line 31 def get_username @tweet.css(".username").text end |
#parse_tweet ⇒ Object
Parse the individual tweet
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/twitter_parser.rb', line 10 def parse_tweet if !@tweet.text.empty? return { tweet_text: get_tweet_text, username: get_username, fullname: get_fullname, user_id: get_user_id, reply_to_user: get_reply_to_user[0], reply_to_uid: get_reply_to_user[1], tweet_time: get_tweet_time, tweet_link: get_tweet_link, retweet_count: get_retweet_count, favorite_count: get_favorite_count, reply_count: get_reply_count, mention_names: get_mentions[0], mention_uids: get_mentions[1] } end end |