Skip to content

Instantly share code, notes, and snippets.

@tannerburson
Created August 11, 2011 18:49
Show Gist options
  • Save tannerburson/1140417 to your computer and use it in GitHub Desktop.
Save tannerburson/1140417 to your computer and use it in GitHub Desktop.
Stupid lowbrow API proxy
<?php
function cleanup($string) {
$string = strip_tags($string);
$string = preg_replace('/&(quot|apos|lt|gt);/','',$string);
$string = html_entity_decode(htmlspecialchars_decode($string));
return $string;
}
function generate($count){
$nums = array();
for($i=0;$i<$count;$i++)
$nums[] = rand(3,20473);
return $nums;
}
$mysql = mysql_connect();
mysql_select_db('brow_proxy');
$ids = array();
if($_GET['i']){
$ids = explode(',',$_GET['i']);
}
else if($_GET['c']){
if ($_GET['c'] > 10) $_GET['c'] = 10;
$ids = generate($_GET['c']);
}
else die(json_encode(false));
$res = mysql_query(sprintf("select id, id dbid,date,lbid,moment from moments where id in (%s)",mysql_real_escape_string(implode(',',$ids))));
if(!$res) die(mysql_error());
$rows = array();
$moments = array();
while($row = mysql_fetch_assoc($res)){
$rows[$row['dbid']] = $row;
}
foreach($ids as $id) {
if(!$rows[$id]) {
$moment = file_get_contents(sprintf('http://lowbrow.blackholed.org/api/key=336ebbb2179beaa7340a4f1620f3af40/qid=%s/api.json',$id));
$moment = json_decode($moment,true);
$moment['quote'] = cleanup($moment['quote']);
$moment['id'] = $moment['dbid'];
$moments[] = $moment;
$res = mysql_query(sprintf("insert into moments (id,date,lbid,moment) values (%d,'%s',%d,'%s')",
mysql_real_escape_string($moment['dbid']),
mysql_real_escape_string($moment['date']),
mysql_real_escape_string($moment['lbid']),
mysql_real_escape_string($moment['quote'])));
if(!$res) die(mysql_error());
}
else {
$moments[] = $rows[$id];
}
}
if(count($moments) == 1) $moments = $moments[0];
print json_encode($moments);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment