Browse Source

delicious

master
Asherah Connor 1 month ago
parent
commit
d080f47886
1 changed files with 133 additions and 58 deletions
  1. 133
    58
      poputweets

+ 133
- 58
poputweets View File

@@ -3,89 +3,164 @@
require 'json'
require 'pp'
require 'time'
require 'pry'

ME = "67204542"

class Tweet
def initialize(hash)
@data = {}
%w(favorite_count id_str retweet_count created_at full_text
in_reply_to_status_id_str in_reply_to_user_id_str
in_reply_to_screen_name).each do |key|
@data[key] = hash.delete(key)
end

if hash.length > 0
raise "unhandled data remains: #{hash.inspect}"
end
attr_reader :children
attr_accessor :parent

def initialize(hash)
@data = {}
%w(favorite_count id_str retweet_count created_at full_text
in_reply_to_status_id_str in_reply_to_user_id_str
in_reply_to_screen_name).each do |key|
@data[key] = hash.delete(key)
end

def favs
@data["favorite_count"].to_i
if hash.length > 0
raise "unhandled data remains: #{hash.inspect}"
end

def rts
@data["retweet_count"].to_i
@children = []
@parent = nil
end

def favs
@data["favorite_count"].to_i
end

def rts
@data["retweet_count"].to_i
end

def id
@data["id_str"]
end

def created_at
Time.parse(@data["created_at"])
end

def text
@data["full_text"]
end

def in_reply_to
status_id = @data["in_reply_to_status_id_str"]
user_id = @data["in_reply_to_user_id_str"]
screen_name = @data["in_reply_to_screen_name"]
screen_name && {
status_id: status_id,
user_id: user_id,
screen_name: screen_name,
}
end

def in_reply_to_invalid?
@data["in_reply_to_user_id_str"] && !@data["in_reply_to_screen_name"]
end

def all_children
all = []
stack = children.dup

while node = stack.pop
all << node
stack += node.children
end

def id
@data["id_str"]
end

def created_at
Time.parse(@data["created_at"])
end
all
end

def text
@data["full_text"]
def inspect
all = all_children
if all.any?
chn = "(#{all.length} children) "
else
chn = ""
end

def in_reply_to
status_id = @data["in_reply_to_status_id_str"]
user_id = @data["in_reply_to_user_id_str"]
screen_name = @data["in_reply_to_screen_name"]
screen_name && {
status_id: status_id,
user_id: user_id,
screen_name: screen_name,
}
end

def inspect
"<Tweet at #{created_at} (#{favs}❤️ #{rts}🔄) #{text.inspect}>"
end

def root?
!in_reply_to
"<Tweet: #{created_at} #{chn}(#{favs}/#{rts})>"
end

def root
if parent.nil?
self
else
parent.root
end
end
end

data = File.read(ENV["POPUTWEET_JS"] || "tweet.js")
tweets = JSON.parse(data[data.index("[")..-1])
tweets.map! do |tweet|
tweet = tweet["tweet"]
%w(possibly_sensitive entities source display_text_range lang
extended_entities retweeted favorited truncated
id in_reply_to_user_id in_reply_to_status_id).each do |key|
tweet.delete(key)
end
Tweet.new(tweet)
tweet = tweet["tweet"]
%w(possibly_sensitive entities source display_text_range lang
extended_entities retweeted favorited truncated
id in_reply_to_user_id in_reply_to_status_id
withheld_in_countries).each do |key|
tweet.delete(key)
end
Tweet.new(tweet)
end

tweets.reject! do |tweet|
tweet.text.start_with?("RT @") ||
begin
in_reply_to = tweet.in_reply_to
in_reply_to && in_reply_to[:user_id] != ME
end
tweet.text.start_with?("RT @") ||
tweet.in_reply_to_invalid? ||
begin
in_reply_to = tweet.in_reply_to
in_reply_to && in_reply_to[:user_id] != ME
end
end

tweets_by_id = {}
tweets.each do |tweet|
tweets_by_id[tweet.id] = tweet
tweets_by_id[tweet.id] = tweet
end

pp tweets_by_id
tweets.reject! do |tweet|
if in_reply_to = tweet.in_reply_to
status_id = in_reply_to[:status_id]
if t = tweets_by_id[status_id]
t.children << tweet
tweet.parent = t
else
tweets_by_id.delete tweet.id
next true
end
end

pp tweets_by_id.first
pp tweets_by_id.first[1].root?
false
end

roots = {}

tweets.each do |tweet|
root = tweet.root
next if roots[root.id]

roots[root.id] = root
end

by_score = {}

roots.values.each do |root|
favs = root.favs + root.all_children.map(&:favs).sum
rts = root.rts + root.all_children.map(&:rts).sum

score = (root.all_children.length + 1)*(favs*4 + rts*8)
by_score[score] ||= []
by_score[score] << root
end

by_score.keys.sort.reverse[0...100].each do |score|
puts "Score: #{score}"

by_score[score].each do |t|
puts " #{t.inspect}"
puts " https://twitter.com/kivikakk/status/#{t.id}"
puts t.text.chars.each_slice(90).map {|l| " #{l.join}"}.join("\n")
end
end

Loading…
Cancel
Save