davidbauer / Instacurate

Turn your Twitter timeline into a personalised news site, in an instant. Fetches links from your timeline and displays them in a discovery friendly design.
http://www.instacurate.com
113 stars 29 forks source link

Added basic embed.ly api response caching. #79

Closed oliverwehn closed 11 years ago

oliverwehn commented 11 years ago

Regarding issue #46: Took some of the php code I wrote for your GoogleDocs2Twitter thing and built a rudimentary cache script, that stores the json requests as json files in a folder. You can call the embed-cache.php without any parameter using a cronjob to get rid of unused cache files after e.g. 30 days. Maybe that’s not the best solution here, but it at least should get down the number of embed.ly api requests for the moment. Anyway, I would give it a test run before implementing it on your production server.

davidbauer commented 11 years ago

Thanks a lot! So I put the two php-scripts on the server and I'm good to go (sorry for the stupid question, I'm not familiar with server-side scripts)? And how am I supposed to test it?

oliverwehn commented 11 years ago

I changed the getJSON request in the javascript file to address embed-cache.php instead of the embed.ly api. So you’d have to update this one, too. The php script will create a hash from the requested url which it will use as the cache files file name. If the cache file doesn’t exist, yet, the php script will send the api request to embed.ly, store the result in a cache file and return the json string.

The thing is, that your server is now responsible for executing all api requests. Before all this happened on client-side without stressing your own infrastructure. But let’s give it a try and see how it performs.

davidbauer commented 11 years ago

So all I have to do is update script.js and add the other two files to the directory? Right now, that results in the whole thing getting stuck while loading the embeds. Does it work on your local instance?

oliverwehn commented 11 years ago

It does. Maybe your directory rights aren’t set the way the script can create the cache directory. Try to create a directory “embed-cache” manually and make it writable. I’d suggest setting 644 for the php files and 755 for the cache dir. As soon as you have the directory created, check if there are cache files showing up after performing a Twitter search.

davidbauer commented 11 years ago

Changed it accordingly, still doesn't work (and no error messages in the console either). Where are cache files supposed to show up? Do I need to change something in the php files after moving the files to said directory?

oliverwehn commented 11 years ago

Did you create the embed-cache directory? Or was it already there? The cache files should show up inside of it. I’ll pull your setup to check.

oliverwehn commented 11 years ago

Oh, the php files should be in the project’s root dir. The embed-cache directory is just for the cache files the php script creates. Remember to change the getJSON call back to ./embed-cache.php

oliverwehn commented 11 years ago

Any progress on this, yet?

davidbauer commented 11 years ago

Didn't have the time to make the changes. Will do this evening.

davidbauer commented 11 years ago

Still can't get it to run. The two php files are now in the root directory and there's a 755 directory called embed-cache. The teasers won't load on the site and no files are added to /embed-cache. Any idea what I might be doing wrong?

oliverwehn commented 11 years ago

Try calling embed-cache.php directly like yourhost.ch/embed-cache.php?url=http%3A%2F%2Fbit.ly%2FWFcatS&maxwidth=268. It should acutally show a json string.

davidbauer commented 11 years ago

Looks like I get the script, not its output. Tested in Chrome and Firefox. <?php require_once('./embed-cache.class.php'); if($_GET['url']) { $url = ''; $params = array(); foreach($_GET as $k=>$v) { if($k == 'url') { $url = $v; } else { $params[$k] = $v; } } $embedcache = new embedcache(); header('Content-type: application/json'); if(($output = $embedcache->getEmbed($url, $params))) { echo $output; } else { echo '{ "error": "' . $embed->error .'" }'; } } else { $embedcache = new embedcache(); $embedcache->cleanUp(); }

?>

oliverwehn commented 11 years ago

Looks like your server doesn’t process the php scripts. Any idea why? Try adding “AddHandler x-mapp-php5 .php” to your .htaccess file. If you don’t have one, add one.

davidbauer commented 11 years ago

Still doesn't work. Will ask my hoster.

davidbauer commented 11 years ago

Strange things happen: The hoster removed the handler again (they said it was a wrong one) and suddenly the script runs. Well, consider the issue closed, thanks again!

oliverwehn commented 11 years ago

Ok, great. Now let’s see, if it serves its purpose appropriately. ;)