Skip to content

Instantly share code, notes, and snippets.

@bryanveloso
Created January 12, 2012 02:55
Show Gist options
  • Save bryanveloso/1598270 to your computer and use it in GitHub Desktop.
Save bryanveloso/1598270 to your computer and use it in GitHub Desktop.
A horrible, simply horrible hack to getting TweetNest to read JSON. Copy this to the /maintenance/ folder of your TweetNest installation.
<?php
// TWEET NEST
// Load tweets (from JSON)
error_reporting(E_ALL ^ E_NOTICE); ini_set("display_errors", true); // For easy debugging, this is not a production page
@set_time_limit(0);
require_once "mpreheader.php";
$p = "";
// The below is not important, so errors surpressed
$f = @fopen("loadlog.txt", "a"); @fwrite($f, "Attempted load " . date("r") . "\n"); @fclose($f);
$pageTitle = "Load tweets from JSON";
require "mheader.php";
// Identifying user
if(!empty($_GET['userid']) && is_numeric($_GET['userid'])){
$q = $db->query("SELECT * FROM `".DTP."tweetusers` WHERE `userid` = '" . $db->s($_GET['userid']) . "' LIMIT 1");
if($db->numRows($q) > 0){
$p = "user_id=" . preg_replace("/[^0-9]+/", "", $_GET['userid']);
} else {
dieout(l(bad("Please load the user first.")));
}
} else {
if(!empty($_GET['screenname'])){
$q = $db->query("SELECT * FROM `".DTP."tweetusers` WHERE `screenname` = '" . $db->s($_GET['screenname']) . "' LIMIT 1");
if($db->numRows($q) > 0){
$p = "screen_name=" . preg_replace("/[^0-9a-zA-Z_-]+/", "", $_GET['screenname']);
} else {
dieout(l(bad("Please load the user first.")));
}
}
}
// Define import routines
function importTweets($data){
global $twitterApi, $db, $config, $access, $search;
$maxCount = 200;
$tweets = array();
$sinceID = 0;
$maxID = 0;
echo l("Importing:\n");
// Retrieve tweets
if(is_array($data) && $data[0] === false){ dieout(l(bad("Error: " . $data[1] . "/" . $data[2]))); }
echo l("<strong>" . ($data ? count($data) : 0) . "</strong> new tweets\n");
echo l("<ul>");
foreach($data as $tweet){
echo l("<li>" . $tweet->id_str . " " . $tweet->created_at . "</li>\n");
$tweets[] = $twitterApi->transformTweet($tweet);
$maxID = sprintf("%.0F", (float)((float)$tweet->id - 1));
}
echo l("</ul>");
if(count($tweets) > 0){
// Ascending sort, oldest first
$tweets = array_reverse($tweets);
echo l("<strong>All tweets collected. Reconnecting to DB...</strong>\n");
$db->reconnect(); // Sometimes, DB connection times out during tweet loading. This is our counter-action
echo l("Inserting into DB...\n");
$error = false;
foreach($tweets as $tweet){
$q = $db->query($twitterApi->insertQuery($tweet));
if(!$q){
dieout(l(bad("DATABASE ERROR: " . $db->error())));
}
$text = $tweet['text'];
$te = $tweet['extra'];
if(is_string($te)){ $te = @unserialize($tweet['extra']); }
if(is_array($te)){
// Because retweets might get cut off otherwise
$text = (array_key_exists("rt", $te) && !empty($te['rt']) && !empty($te['rt']['screenname']) && !empty($te['rt']['text']))
? "RT @" . $te['rt']['screenname'] . ": " . $te['rt']['text']
: $tweet['text'];
}
$search->index($db->insertID(), $text);
}
echo !$error ? l(good("Done!\n")) : "";
} else {
echo l(bad("Nothing to insert.\n"));
}
}
if((!empty($_FILES["uploaded_file"])) && ($_FILES["uploaded_file"]["error"] == 0)){
$filename = basename($_FILES["uploaded_file"]["name"]);
$ext = substr($filename, strrpos($filename, '.') + 1);
if($ext == "json"){
$json = file_get_contents($_FILES["uploaded_file"]["tmp_name"]);
$data = json_decode($json);
if($data){
importTweets($data);
} else {
dieout(l(bad("Whoops! That's not valid JSON.")));
}
} else {
dieout(l(bad("Whoops! That's not a JSON file." . $ext)));
}
} else {
// Our form
echo l("
<form enctype=\"multipart/form-data\" action=\"loadtweetsfromjson.php\" method=\"post\">
Choose a file to upload: <input name=\"uploaded_file\" type=\"file\" /> <input type=\"submit\" value=\"Upload\" />
</form>");
}
require "mfooter.php";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment