You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.9KB

  1. #!/usr/bin/env ruby
  2. require 'json'
  3. require 'pp'
  4. require 'time'
  5. ME = "67204542"
  6. class Tweet
  7. def initialize(hash)
  8. @data = {}
  9. %w(favorite_count id_str retweet_count created_at full_text
  10. in_reply_to_status_id_str in_reply_to_user_id_str
  11. in_reply_to_screen_name).each do |key|
  12. @data[key] = hash.delete(key)
  13. end
  14. if hash.length > 0
  15. raise "unhandled data remains: #{hash.inspect}"
  16. end
  17. end
  18. def favs
  19. @data["favorite_count"].to_i
  20. end
  21. def rts
  22. @data["retweet_count"].to_i
  23. end
  24. def id
  25. @data["id_str"]
  26. end
  27. def created_at
  28. Time.parse(@data["created_at"])
  29. end
  30. def text
  31. @data["full_text"]
  32. end
  33. def in_reply_to
  34. status_id = @data["in_reply_to_status_id_str"]
  35. user_id = @data["in_reply_to_user_id_str"]
  36. screen_name = @data["in_reply_to_screen_name"]
  37. screen_name && {
  38. status_id: status_id,
  39. user_id: user_id,
  40. screen_name: screen_name,
  41. }
  42. end
  43. def inspect
  44. "<Tweet at #{created_at} (#{favs}❤️ #{rts}🔄) #{text.inspect}>"
  45. end
  46. def root?
  47. !in_reply_to
  48. end
  49. end
  50. data = File.read(ENV["POPUTWEET_JS"] || "tweet.js")
  51. tweets = JSON.parse(data[data.index("[")..-1])
  52. tweets.map! do |tweet|
  53. tweet = tweet["tweet"]
  54. %w(possibly_sensitive entities source display_text_range lang
  55. extended_entities retweeted favorited truncated
  56. id in_reply_to_user_id in_reply_to_status_id).each do |key|
  57. tweet.delete(key)
  58. end
  59. Tweet.new(tweet)
  60. end
  61. tweets.reject! do |tweet|
  62. tweet.text.start_with?("RT @") ||
  63. begin
  64. in_reply_to = tweet.in_reply_to
  65. in_reply_to && in_reply_to[:user_id] != ME
  66. end
  67. end
  68. tweets_by_id = {}
  69. tweets.each do |tweet|
  70. tweets_by_id[tweet.id] = tweet
  71. end
  72. pp tweets_by_id
  73. pp tweets_by_id.first
  74. pp tweets_by_id.first[1].root?