Skip to content

Instantly share code, notes, and snippets.

@zeimusu
Last active December 21, 2015 06:18
Show Gist options
  • Save zeimusu/6262824 to your computer and use it in GitHub Desktop.
Save zeimusu/6262824 to your computer and use it in GitHub Desktop.
This gist provides a web page (time.php) that contains an image that is updated hourly. It is intended to be used with a directory of images with hashed filenames, generated by renameseq.py and shafile.py, and the event server timesse.php to make a "slow movie", rather like xkcd.com/time.
<html>
<head>
<title>Old frames</title>
<body>
<h1>Old frames</h1>
<table>
<thead>
<tr>
<th>Frame</th>
<th>Time</th>
<th>Image Filename</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<?php
$year=2013;
$month=8;
$day=17;
$hour=14;
$now = time();
$salt="";
$interval = 3600;
$frame = 1;
$time = mktime($hour,0,0,$month,$day,$year);
while ($time < $now) {
$name = strftime("%Y-%m-%d-%H-00",$time).$salt;
$shaname = sha1($name);
echo "<tr><th>$frame</th>";
echo "<td>$name</td>";
echo "<td>$shaname</td>";
echo "<td><a href=\"image/$shaname.jpg\">Link</a></td>";
echo "</tr>";
$time += $interval;
$frame += 1;
}
?>
</tbody>
</table>
</html>
#!/usr/bin/env python3
#probably works without change in python2
#Usage renameseq.py 2013-08-17-14 file1.jpg [file2.jpg ...]
#Rename a list of files with a time that they are to be shown, prior to hashing
#with shafiles.py. The first argument is the filename to rename the first file to.
#This utility names files to be shown 1 per hour.
import os,sys,datetime
year,month,day,hour = sys.argv[1].split("-")
date=datetime.datetime(int(year),int(month),int(day),hour=int(hour))
args= sys.argv[1:]
for path in args:
if not os.path.exists(path):
continue
filename,extension = os.path.splitext(os.path.basename(path))
datename = date.strftime("%Y-%m-%d-%H-00")+extension
os.rename(path,datename)
date = date + datetime.timedelta(hours=1)
#!/usr/bin/env python3
#Probably works in python2 also.
#Usage: shafiles.py file1.jpg [file2.jpg ...]
#Renames the files with the sha1 hash of their filenames (excluding the extension)
#Normal use would be to rename files to the YYYY-mm-dd-HH-00.jpg form using renameseq.py
#Then use this utility to rename in hash form
#Salt may be added, if desired
import hashlib, os, sys
arguments = sys.argv[1:]
salt = ""
for files in arguments:
if not os.path.exists(files):
print("Warning: File", files, "not found.")
continue
filename, extension = os.path.splitext(os.path.basename(files))
salted_name= filename+salt
bsalted_name = bytes(salted_name,"utf_8")
hashedname = hashlib.sha1(bsalted_name).hexdigest()+extension
print(filename,"->",hashedname)
os.rename(files, hashedname)
This gist provides a web page (time.php) that contains an image that is updated hourly.
It is intended to be used with a directory of images with hashed filenames, generated by renameseq.py and shafile.py, and the event server timesse.php to make a "slow movie", rather like xkcd.com/time.
The php file are put on the web server, and the python files can be put somewhere that python will find them. They should work ok in python2.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="twocol.css" rel="stylesheet" type="text/css" />
<title>Stolen!</title>
</head>
<body>
<script>
var source = new EventSource('timesse.php');
source.onmessage = function(e) {
document.getElementById("time").src="image/"+e.data;
};
</script>
<div id="header">
<h1> ~~ Stolen! ~~ </h1>
<p>An hourly webcomic or a &lsquo;timed&rsquo; movie</p>
</div>
<div class="imageholder">
<?php
$salt = "";
$rawmsg = date("Y-m-d-H-00",time()) . $salt;
$shamsg = sha1($rawmsg).".jpg";
echo "<img id=\"time\" width=\"480\" src = \"/image/$shamsg\" title=\"Wait for it\">";
?>
</div>
</body>
</html>
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
/**
* Constructs the SSE data format and flushes that data to the client.
*
* The message is the hashed name of a the file the client should next request.
*/
function sendMsg($id , $msg) {
echo "id: $id" . PHP_EOL;
echo "data: $msg.jpg\n";
echo PHP_EOL;
ob_flush();
flush();
}
//Interval is the update rate
//Salt may be added to the hashing function in the form of .
//It should also be added everywhere else that hashing is done.
$interval = 3600;
$startedAt = time();
$salt = "";
//The unhashed filenames should be in form 2013-08-17-23-00.jpg
//There are python utilities for renaming and hashing multiple files.
do {
$rawmsg = date("Y-m-d-H-00",time()) . $salt;
$shamsg = sha1($rawmsg);
$sleeptime = $interval - (time() % $interval); //calculate coma time until next newpix
sendMsg($startedAt , $shamsg);
sleep($sleeptime);
// If we didn't use a while loop, the browser would essentially do polling
// every ~3seconds. Using the while, we keep the connection open and only make
// one request.
} while(true);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment