A super simple Django site to see if Taylor Swift is on Tumblr right now. You can see it at istayontumblr.com
It polls the Tumblr API to see if she's liked or posted anything recently, and makes a guess as to whether or not she's currently online. It's pretty simple so far.
The UI needs a refactor pretty badly. I know a lot more about JS, CSS, Sass, Django templates, etc. than I did when I wrote this. Currently I've hacked in some stuff to celebrate the anniversary of the Fearless release.
##Setup## ###Install Prereqs### To run this locally you'll need a few things. (These instructions are for OS X. They're also from memory. I'll clean them up later, but some of you are playing with it now.)
- First you need the Xcode command line tools. If you already have Xcode installed, install them with
- Install openssl with
brew install openssl
- You need to have Python, pip, and setuptools installed. Do it with the instructions here.
- Then use pip to install virtualenv and virtualenvwrapper with commands like
pip install <package name>. Details about those can be found here. Basically, virtualenvs keep your python packages local to your projects.
- Installing the Heroku Toolbet) is the easiest way to install the heroku cli and the other tools you'll need.
- You'll also need to have a local install of PostgreSQL. I used Postgres.app because I'm lazy.
- You need to install memcached for the caching service, which you can do with
brew install memcachedif you installed Homebrew. (If you haven't, it's in the python installation instructions above.) Details on how to setup memcached to be easy to launch are here.
- Go to the Tumblr API documentation and request and API key.
- Also, add
alias fucking="sudo"to your
.bash_profile. It's not necessary, I just find it cathartic.
###Getting it running###
- Okay, you've cloned this this repo somewhere. If not, do that.
- Now you need to create a virtualenv. Do that with
mkvirtualenv istayonassuming you got virtualenvwrapper installed.
- Then activate the virtualenv with
workon istayon. Now your python should point to the python in the virtualenv. You can confirm that with
- Install the required pip packages. From the same directory as the
requirements.txtfile, do 'pip install -r requirements.txt --allow-all-external'.
- create a
.envfile in the root of the project. Heroku local sets the entries in the file as your environment variables, and
settings.pychecks environment variables for things like the API keys and database connection strings. This file is .gitignored so this sensitive stuff stays out of source control. If you deploy to Heroku, then you'll have to add these environment variable in the admin interface. We use the following entries for the local site (Make sure to add your Tumblr API key):
PYTHONUNBUFFERED=true DATABASE_URL=postgres://localhost/istayon SECRET_KEY=<makup a long string for this with random characters> DEBUG=Anything_because_strings_are_truthy TUMBLR_API_KEY= TUMBLR_API_SECRET_KEY= NEW_RELIC_APP_NAME=default1 MEMCACHEDCLOUD_SERVERS=127.0.0.1:11211 MEMCACHEDCLOUD_USERNAME=<this can be blank for local> MEMCACHEDCLOUD_PASSWORD=<you don't need this either> LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib"
- Open the postgres console (should just be able to hit the elephant in your menu bar) and create a database called
CREATE DATABASE istayon. You might have to make a user first? Try it, and check the PostgreSQL documentation if it doesn't work. (I'm writing this from memory.)
- From the root of the project, run
foreman run ./manage.py syncdbto setup the django databases. This might ask you to create a superuser. That's for the admin interface, which isn't currently used, but go ahead and make one if you want.
- I think you should just be able to do
heroku local webat this point. If it's working, you chould be able to go to
localhost:5000and see the site.
If I forgot something or it doesn't work, open an issue or let me know. If you want to deploy to Heroku, I'm using the Heroku Postgres, MemcachedCloud, Papertrail, and New Relic addons, so you'll need to provision those. They all have free tiers.
Django provides the web app framework, and I use the dj-static package to serve static files from Django. (There’s not enough traffic yet to warrant setting up S3 for static files.)
I don’t want to hit the Tumblr API on every request, since that would lock up the app for a long time, but I also didn’t want to pay for a second dyno to run a background process that queries the API. My solution was to query the API on the first request, and then use Memcached to store the information from the API for a minute. Since it’s user requests that prompt a cache refresh, I use a Stale-While-Revalidate strategy to avoid the thundering herd problem. You should see the difference in the New Relic stats before and after I got caching working properly.
I use Numpy to bin the results from the “blog/likes” API call into five minute intervals, so I end up with a histogram of likes/interval. I use flot.js to plot this histogram and provide a visual representation of recent activity.
It also scales responsively between desktop browsers and mobile size browsers. It’s based solely on the browser window width.
I recently added a check to see if the site is being loaded on an iPhone/iPad/iPod, and if that’s the case any links to Tumblr use the x-callback-url spec to open the Tumblr app. It’s unlikely anyone using this doesn’t have Tumblr installed on their phone, so I don’t handle that case yet. Based on Google Analytics Android devices are a small minority as well, so on Android links still go the the Tumblr website.