Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Nov 10, 2012
0 parents commit 9afc6ac
Show file tree
Hide file tree
Showing 9 changed files with 1,528 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .hgignore
@@ -0,0 +1,3 @@
syntax:glob
cache

41 changes: 41 additions & 0 deletions .htaccess
@@ -0,0 +1,41 @@
<Files .htaccess>
Order deny,allow
Deny from all
</Files>

<Files .hg>
Order deny,allow
Deny from all
</Files>

<Files .hgignore>
Order deny,allow
Deny from all
</Files>

<Files cache>
Order deny,allow
Deny from all
</Files>

<Files *.txt>
Order deny,allow
Deny from all
</Files>

<Files .twitteroauth>
Order deny,allow
Deny from all
</Files>

<Files *.php>
Order deny,allow
Deny from all
</Files>

<Files index.php>
Order allow,deny
Allow from all
</Files>


22 changes: 22 additions & 0 deletions TWITTEROAUTH-LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2009 Abraham Williams - http://abrah.am - abraham@poseurte.ch

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
189 changes: 189 additions & 0 deletions cachetweets.php
@@ -0,0 +1,189 @@
<?php
/* Load required lib files. */
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

$cachedir = './cache';
if (!file_exists($cachedir))
mkdir($cachedir);

$cachefnames = scandir($cachedir);
rsort($cachefnames, SORT_NUMERIC);

$reprocess_existing = true;
$getfromargv = false;

$connection = undef;
$statuses = undef;
$data = undef;

if ($reprocess_existing)
{
$data = array();
if ($cachefnames !== false)
{
foreach ($cachefnames as $fname)
{
$obj = unserialize(file_get_contents("$cachedir/$fname"));
if ($obj !== false)
$data[] = $obj;
}
}
}
else if ($getfromargv)
{
$data = array();

$skip = true;
foreach ($argv as $x)
{
if ($skip) { $skip = false; continue; }
$data[] = $x;
}

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);

/* If method is set change API call made. Test is called by default. */
$content = $connection->get('account/verify_credentials');
}
else
{
$maxtweet = '1';
if ($cachefnames !== false)
$maxtweet = $cachefnames[0];
unset($cachefnames);

/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);

/* If method is set change API call made. Test is called by default. */
$content = $connection->get('account/verify_credentials');

$data = $connection->get('statuses/user_timeline', array('screen_name' => 'icculus', 'count' => '2000', 'include_entities' => 'true', 'include_rts' => 'true', 'since_id' => $maxtweet));
}


foreach ($data as $tweet)
{
if ($getfromargv)
{
$id = $tweet;
$tweet = $connection->get('statuses/show', array('id' => $id, 'include_entities' => 'true'));
if (isset($tweet->errors))
{
print("get tweet $id failed:\n");
foreach ($tweet->errors as $err)
print(" - {$err->message}\n");
continue;
}
}

//if ($tweet->user->screen_name != "icculus") { print ("tweet $id is probably a retweet: '{$tweet->text}'\n"); continue; }

$id = $tweet->id_str;
$cachefname = "$cachedir/$id";

if (!$reprocess_existing && file_exists($cachefname))
{
print("tweet $id already cached!\n");
continue;
}

$origtweet = $tweet;
if (isset($tweet->retweeted_status))
$tweet = $tweet->retweeted_status;


unset($in_reply_to_name);
$mediahtml = '';
$text = $tweet->origtext;
$html = $text;
if (isset($tweet->entities))
{
if (isset($tweet->entities->user_mentions))
{
foreach($tweet->entities->user_mentions as $item)
{
if (strcasecmp($tweet->in_reply_to_screen_name, $item->screen_name) == 0)
$in_reply_to_name = $item->name;
$html = str_ireplace("@{$item->screen_name}", "<a href='https://twitter.com/{$item->screen_name}/'>@{$item->screen_name}</a>", $html, $temp = 1);
}
}
if (isset($tweet->entities->urls))
{
foreach($tweet->entities->urls as $item)
{
$text = str_replace($item->url, $item->display_url, $text, $temp = 1);
$html = str_replace($item->url, "<a href='{$item->expanded_url}'>{$item->display_url}</a>", $html, $temp = 1);
// !!! FIXME: embed youtube
}
}
if (isset($tweet->entities->media))
{
foreach($tweet->entities->media as $item)
{
$mediaurl = isset($item->media_url_https) ? $item->media_url_https : $item->media_url;
if ($item->type == "photo")
{
$mediahtml = $mediahtml . "<img src='$mediaurl' style='max-width:375px;'/><br/>";
$text = str_replace($item->url, $item->display_url, $text, $temp = 1);
$html = str_replace($item->url, "<a href='{$item->expanded_url}'>{$item->display_url}</a>", $html, $temp = 1);
}
}
}
if (isset($tweet->entities->hashtags))
{
foreach($tweet->entities->hashtags as $item)
$html = str_replace("#{$item->text}", "<a href='https://twitter.com/search?q=%23{$item->text}'>#{$item->text}</a>", $html, $temp = 1);
}
}

$in_reply_to = '';
if ($tweet->in_reply_to_status_id_str)
{
if (!$in_reply_to_name)
$in_reply_to_name = $tweet->in_reply_to_screen_name;
$in_reply_to = "in reply to <a href='https://twitter.com/{$tweet->in_reply_to_screen_name}/status/{$tweet->in_reply_to_status_id_str}'>$in_reply_to_name</a>";
}

$datetime = new DateTime($tweet->created_at); //('D M d H:i:s T Y', $tweet->created_at);
$created_at = $datetime->format('j M y');
$tweet->rss_created_at = $datetime->format(DateTime::RSS);

if (!isset($tweet->origtext))
$tweet->origtext = $tweet->text;
$tweet->text = $text;

$tweet->html = <<<EOS
<div style="padding-bottom:25px;padding-left:50px;padding-right:50px;padding-top:5px;">
<table><tr>
<td><img src='{$tweet->user->profile_image_url}'/></td>
<td>
<span style="font-family:'Helvetica Neue', Arial, sans-serif;font-size:18px;font-weight:bold;color:#333;text-decoration:none;display:block;">
{$tweet->user->name}
</span>
<span style="font-family:'Helvetica Neue', Arial, sans-serif;font-size:14px;font-weight:normal;color:#999;text-decoration:none;display:block;">
<a href="https://twitter.com/{$tweet->user->screen_name}">@{$tweet->user->screen_name}</a>
</span>
</td>
</tr></table>
<span style="padding-top:5px;padding-bottom:5px;width:438px;line-height: 28px;font-family:Georgia, 'Times New Roman', serif;font-size:22px;color:#333;display:block;">$html</span>
$mediahtml
<span style="font-family:'Helvetica Neue', Arial, sans-serif;font-size:12px;color:#999;display:block;">
Tweeted <a href="https://twitter.com/{$tweet->user->screen_name}/status/{$tweet->id_str}">$created_at</a> $in_reply_to via {$tweet->source}
</span>
</div>
EOS;

if (file_put_contents($cachefname, serialize($origtweet)) === false)
{
print("couldn't write $cachefname!\n");
unlink($cachefname);
}

//print_r($tweet);
print("cached tweet $id\n");
}

25 changes: 25 additions & 0 deletions dump.php
@@ -0,0 +1,25 @@
<?php

/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

$data = array();
$cachedir = './cache';
$cachefnames = scandir($cachedir);
rsort($cachefnames, SORT_NUMERIC);

if ($cachefnames !== false)
{
foreach ($cachefnames as $fname)
{
$obj = unserialize(file_get_contents("$cachedir/$fname"));
if ($obj !== false)
$data[] = $obj;
}
}

header('Content-Type: text/plain; charset=UTF-8');
print_r($data);

42 changes: 42 additions & 0 deletions index.php
@@ -0,0 +1,42 @@
<?php

/* Load required lib files. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

$data = array();
$cachedir = './cache';
$cachefnames = scandir($cachedir);
rsort($cachefnames, SORT_NUMERIC);

if ($cachefnames !== false)
{
foreach ($cachefnames as $fname)
{
$obj = unserialize(file_get_contents("$cachedir/$fname"));
if ($obj !== false)
$data[] = $obj;
}
}

header('Content-Type: text/html; charset=UTF-8');
print("<html><head>");
print("<link rel='alternate' type='application/rss+xml' title='Twitter / icculus' href='http://icculus.org/~icculus/tweets/rss.php' />");
print("<title>Twitter / icculus</title></head><body>\n");

foreach ($data as $tweet)
{
if (isset($tweet->retweeted_status))
$tweet = $tweet->retweeted_status;
print($tweet->html);
}

if (0)
{
print("<pre>\n\n\n\n");
print_r($data);
}

print("\n</body></html>\n\n");

0 comments on commit 9afc6ac

Please sign in to comment.