Created
June 26, 2022 21:11
-
-
Save tnorthcutt/ea0dfdd4acd7576a38b499a306fea140 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Threader | |
{ | |
private array $tweets, $roots; | |
private string $user; | |
public function handle(): void | |
{ | |
echo "What is your twitter username? "; | |
$handle = fopen ("php://stdin","r"); | |
$line = fgets($handle); | |
$this->user = trim($line); | |
echo "\n"; | |
echo "Thank you, continuing...\n"; | |
$json = file_get_contents('./tweet.js'); | |
$this->tweets = json_decode($json); | |
echo "Analyzing...\n"; | |
foreach ($this->tweets as $tweet) { | |
$tweet = $tweet->tweet; | |
echo "."; | |
if ($this->is_reply_to_self($tweet)) { | |
$this->get_root($tweet); | |
} | |
} | |
print_r($this->roots); | |
} | |
private function in_reply_to_id($tweet): bool|string | |
{ | |
return property_exists($tweet, "in_reply_to_status_id") ? $tweet->in_reply_to_status_id : false; | |
} | |
private function get_root($tweet): void | |
{ | |
$parent_id = $this->in_reply_to_id($tweet); | |
$parent = $this->get_tweet_object_by_id($parent_id); | |
if ($parent) { | |
$this->get_root($parent); | |
} else if ($this->is_not_reply($tweet)) { | |
$this->roots[] = "https://twitter.com/" . $this->user . "/status/" . $tweet->id; | |
} | |
} | |
private function get_tweet_object_by_id($id): bool|object | |
{ | |
foreach ($this->tweets as $tweet) { | |
if ($tweet->tweet->id === $id) { | |
return $tweet->tweet; | |
} | |
} | |
return false; | |
} | |
private function is_reply_to_self($tweet): bool | |
{ | |
return property_exists($tweet, "in_reply_to_screen_name") && $tweet->in_reply_to_screen_name === $this->user; | |
} | |
private function is_not_reply($tweet): bool | |
{ | |
return !property_exists($tweet, "in_reply_to_screen_name") && !property_exists($tweet, "in_reply_to_status_id") && !str_starts_with($tweet->full_text, "@"); | |
} | |
} | |
$t = new Threader(); | |
$t->handle(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is super hacky, use at your own risk, nothing guaranteed, etc. etc.
On your local machine, copy this to a file in a new directory, called e.g.
threads.php
. Puttweets.js
in the same directory. Then, in that directory, runphp ./threads.php
on the command line. You should get an array of thread root URLs as output.@tnorthcutt on twitter if you have questions