Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit 0eff8772690726d2a3b25081b32cbf62d3c25346 @jpf committed Dec 9, 2012
Showing with 11,137 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 Procfile
  3. +10,921 −0 animated-gifs
  4. +35 −0 app.py
  5. +8 −0 command.sh
  6. +67 −0 parse.py
  7. +6 −0 requirements.txt
  8. +97 −0 templates/index.html
2 .gitignore
@@ -0,0 +1,2 @@
+venv
+*.pyc
1 Procfile
@@ -0,0 +1 @@
+web: python app.py
10,921 animated-gifs
10,921 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
35 app.py
@@ -0,0 +1,35 @@
+import os
+import random
+import urllib
+import json
+from flask import Flask, request, Response, url_for, render_template
+app = Flask(__name__)
+data = []
+
+
+def load_data():
+ file = open('animated-gifs', 'ro')
+ for line in file.readlines():
+ data.append(line.strip())
+
+
+@app.route("/random")
+def random():
+ callback = request.args.get('callback')
+ choice = random.choice(data)
+ rv = json.dumps({'image': choice})
+ if callback:
+ rv = "%s(%s)" % (callback, rv)
@JamesDunne
JamesDunne added a note Dec 11, 2012

You probably want to avoid JSONP nowadays in favor of Access-Control-Allow-Origin: * header. Makes things easier by allowing you to use XmlHttpRequest instead of the <script src="/path/to/jsonp?callback=cb"></script> hacks.

@jpf
Owner
jpf added a note Dec 12, 2012

Hey, thanks! I honestly didn't even consider doing that - I just did what I needed to get it working with jQuery ... this is CORS right?

@JamesDunne
JamesDunne added a note Dec 12, 2012

Yes I think that's another name for it. It was only until recently that I thought JSONP was the latest way to do this but CORS is much much better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ return Response(rv, mimetype='application/json')
+
+
+@app.route("/")
+def index():
+ return render_template('index.html', url=url_for('random', _external=True))
+
+
+if __name__ == "__main__":
+ port = int(os.environ.get('PORT', 5000))
+ load_data()
+ app.debug = True
+ app.run(host='0.0.0.0', port=port)
8 command.sh
@@ -0,0 +1,8 @@
+# Run these commands by hand:
+#
+# This is a huge file. Over 7 GiB in size.
+# $ curl -O http://dumps.wikimedia.org/commonswiki/latest/commonswiki-latest-image.sql.gz
+# This takes about 10 minutes
+# $ zcat commonswiki-latest-image.sql.gz | sed -e 's/),/),\n/g' | grep frameCount | python process.py > found-images
+# $ cat found-images | grep -v '.png' | grep -v ' ' | grep -vi masturba | grep -vi scrotum > animated-gifs
+cat $0
67 parse.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+# http://pypi.python.org/pypi/phpserialize
+import sys
+import csv
+import StringIO
+
+class LineReader:
+ def __init__(self):
+ self.line = ''
+ self.seperator = "\n"
+ self.window = []
+
+ def add(self, character):
+ self.window.append(character)
+ self.line += character
+ ###print "'%s'" % self.line
+ if len(self.window) > len(self.seperator):
+ self.window.pop(0)
+
+ def found_line(self):
+ if ''.join(self.window) == self.seperator:
+ rv = self.line
+ self.line = ''
+ return rv
+ else:
+ return False
+
+def process(row):
+ if len(row) < 8:
+ return False
+ if not row[7] == "'image'":
+ return False
+ if not row[8] == "'gif'":
+ return False
+ # print [row[0],row[7],row[8]]
+ # print row
+ return True
+
+
+
+
+reader = LineReader()
+inside_insert = False
+search = 'INSERT INTO `image` VALUES'
+while True:
+ c = sys.stdin.read(1)
+ if not len(c) > 0:
+ break
+ reader.add(c)
+ line = reader.found_line()
+ if len(reader.line) > len(search) and reader.line.startswith(search):
+ inside_insert = True
+ reader.line = ''
+ reader.seperator = '),'
+ if not inside_insert:
+ continue
+ if not line:
+ continue
+
+ # strip out '(' at beginning and '),' at end
+ string = line[1:-2]
+ # print line[1:-2]
+ string_array = string.split(',')
+ rv = process(string_array)
+ if rv == True:
+ print line
+
6 requirements.txt
@@ -0,0 +1,6 @@
+Flask==0.9
+Jinja2==2.6
+Werkzeug==0.8.3
+distribute==0.6.24
+phpserialize==1.3
+wsgiref==0.1.2
97 templates/index.html
@@ -0,0 +1,97 @@
+<html>
+<head>
+ <title>Every animated GIF on Wikipedia</title>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
+<style type="text/css">
+table { height: 100%; width: 100%; }
+#center { align: center; }
+#gif { text-align: center; vertical-align: middle; }
+#title { text-align: center; }
+#title a:link { color: black; text-decoration: none; }
+#title a:hover { color: blue; text-decoration: underline; }
+#title, #footer, #credits { height: 1px; }
+#footer { text-align: center; margin: 0px;}
+#footer ul { margin: 0px; }
+#footer ul li { display: inline; margin: 5px; }
+#title a { font-size: 2em; }
+#gif img { max-width: 800px; max-height: 400px; }
+</style>
+</head>
+<body>
+<table><!-- forgive me Father, for I have sinned -->
+ <tr><td id="title"></td></tr>
+ <tr><td id="center"><div id="gif" onclick="load_random_image();"></div></td></tr>
+ <tr><td id="footer"><ul id="usage"></ul></td>
+ <tr><td id="credits">Created by <a href="http://joel.franusic.com">Joel Franusic</a>.</td>
+</table>
+ <script>
+ window.addEventListener("hashchange", on_hash_change, false);
+ var current_filename = "";
+
+ function filename_only(url) {
+ var rv = url.split(":")[2];
+ rv = rv.replace(/_/g, " ");
+ rv = rv.replace(".gif","");
+ rv = rv.replace(/-/g, " ");
+ rv = decodeURIComponent(rv);
+ return rv;
+ }
+
+ function load_image_by_filename(filename) {
+ current_filename = filename;
+ location.hash = filename;
+
+ var url = 'http://en.wikipedia.org/w/api.php?' +
+ 'action=query&prop=imageinfo' +
+ '&format=json&iiprop=url' +
+ '&list=imageusage&iutitle=File:' +
+ filename +
+ '&titles=File:' +
+ filename +
+ '&callback=?';
+ $.getJSON(url, function(data) {
+ var img_url = data.query.pages['-1'].imageinfo[0].url;
+ var img_description_url = data.query.pages['-1'].imageinfo[0].descriptionurl;
+ var img_name = filename_only(img_description_url);
+ $("#gif").empty().append($("<img/>").attr("src", img_url));
+ $("#title").empty().append($("<a/>").attr("href", img_description_url).append(img_name));
+ $("#usage").empty();
+ if(data.query.imageusage) {
+ $.each(data.query.imageusage, function(index, value) {
+ $("#usage").append('<li><a href="http://en.wikipedia.org/wiki/' + value.title + '">' + value.title + '</a></li>');
+ });
+ }
+ });
+ }
+
+ function load_random_image() {
+ $.getJSON("http://bob.fihn.net:5000/pick?callback=?", function(data) {
+ load_image_by_filename(data.image)
+ });
+ }
+
+ function on_hash_change() {
+ var hash_value = location.hash;
+ hash_value = hash_value.replace('#', '');
+
+ if(hash_value !== current_filename) {
+ load_image_by_filename(hash_value);
+ }
+ }
+
+ $(function() {
+ $(document).keyup(function(evt) {
+ if (evt.keyCode == 32) {
+ load_random_image();
+ }
+ })
+ });
+
+ if(location.hash !== "") {
+ on_hash_change();
+ } else {
+ load_random_image();
+ }
+</script>
+</body>
+</html>

0 comments on commit 0eff877

Please sign in to comment.
Something went wrong with that request. Please try again.