Monday, June 17, 2013

Twitch.tv without Adobe Flash

How to watch streams in your favourite video player.

Current problems with Flash under Linux:

  • new versions of Adobe plugins will support only Chrome Pepper - for other browsers only older plugins will be available
  • closed source ™
  • sometimes you cannot exit full screen mode
  • sometimes just hangs with 100% CPU usage
  • sometimes it just crash

There is some alternative - shumway but currently it doesn't support YouTube so my guess is that Twitch isn't either. Additionally when page is using SWFObject we will see only "install Adobe Flash" text.

Downloading

Listing flv files for given archive link (past broadcast), archive.py:

#!/usr/bin/python3

from urllib.request import urlopen
import json, sys, re

decoder = json.decoder.JSONDecoder()

for arg in sys.argv[1:]:
 m = re.match(r"^.*/([^/]+)/b/([0-9]+)$", arg)
 video_id = int(m.group(2))
 with urlopen("http://api.justin.tv/api/broadcast/by_archive/%d.json" % video_id) as f:
  resp = decoder.decode(f.read().decode())
  for i in resp:
   print(i["video_file_url"])

Usage:

$ ./archive.py http://www.twitch.tv/some_channel/b/4173086
http://store30.media30.justin.tv/archives/2013-6-15/live_user_some_channel_13713088.flv
http://store38.media38.justin.tv/archives/2013-6-15/live_user_some_channel_13713106.flv
http://store6.media6.justin.tv/archives/2013-6-15/live_user_some_channel_13713124.flv

Only remaining thing is to download those files.

Watching

We will be needing livestreamer and rtmpdump. To not litter in system i've used python virtualenv. Steps below are assuming that rtmpdump is not installed in system.

So:

cd /path/to/project
git clone git://git.ffmpeg.org/rtmpdump
virtualenv env
make -C rtmpdump/
source env/bin/activate
pip install livestreamer

Next step could be writing script for starting player:

#!/bin/bash

d="$(dirname $0)"

source "${d}"/l/bin/activate
LD_LIBRARY_PATH="${d}"/rtmpdump/librtmp/ livestreamer -p /path/to/player -r "${d}"/rtmpdump/rtmpdump "${@}"

... and we can start watching:

./run.sh http://www.twitch.tv/dreamhacksc2 720p

you can also save stream to file:

./run.sh http://www.twitch.tv/dreamhacksc2 720p -o file.flv

Friday, June 14, 2013

Eclipse: The Dark IDE - Linux edition

Not so long ago I've stumbled upon http://ethanschoonover.com/solarized. It is kind of magical set of dark and light themes for range of IDE/editors/widgets etc. and it looks kind of good. I was thinking for a while about background and foreground color contrast - it could be illegible - but it is written that colors comply to CIELAB spec and this solution was tested on variety of devices. So it has to be true! ;)

Since I am spending so much time working with Eclipse, at one time I've decided to try and take Google's advice - darker colors could may reduce eye fatigue (especially when you're coding at night). I'm presenting you steps how to fully theme Eclipse with dark theme (Linux edition).

end result as incentive

Eclipse

At the beginning we have:

To apply above themes you have to first install Eclipse Color Themes and Chrome Theme.

After applying new theme (for Aptana and Eclipse) I've come to see that Aptana's background is a little darker than Eclipse's so I've changed some colors for appropriate - from base scheme (download changed theme).
Our next step is to change color of tabs and other Eclipse widgets, we will use Chrome Theme for it. There you can download already prepared configuration file for ui - you need to place it in workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings. In case of manual fun you will need to remember about adding CSS line from answer on stackoverflow.com:

#org-eclipse-e4-ui-compatibility-editor * { background-color: #002b36; }

GTK

Happily GTK+ 2 theme is easy to find: https://github.com/heichblatt/gtk2-theme-solarizeddark.
You don't need it enabled for whole system, just Eclipse will suffice.

GTK2_RC_FILES=~/.themes/solarized.gtkrc /mnt/sandbox/eclipse/eclipse

Desktop file

If you want to run styled Eclipse from your Desktop Menus, you'll need change Exec part of its .desktop file. Below is whole file as used by me:

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse IDE
Comment=Eclipse Programming IDE
Exec=env GTK2_RC_FILES=~/.themes/solarized.gtkrc /mnt/sandbox/eclipse/eclipse
Icon=/mnt/sandbox/eclipse/icon.xpm
Terminal=false
Type=Application
Categories=Application;Development;

Sadly, event after everything is properly styled you can still find editors where colors will be wrong. For example - Twig editor from Symfony2 package. (fixed Twig configuration)

Thursday, January 31, 2013

Bugzilla + nginx

Single question started everything - Which bug tracking app is best? One that just fits? I've chosen Bugzilla. Solution - I would think - ideal for sole developer.

Installation process was quite painless. Bugzilla was written in Perl so to install it I had to first install many dependencies - cause I've never really used Perl. After installation i was glad I could setup SQLite as data storage, but then I discovered dreadful problem - only mode which Bugzilla could be run with was CGI. I'm trying to use uWSGI wherever I can so in this case I was trying to run it with PSGI. Sadly the only supported modes are cgi and Apache's mod_perl. Happily both Nginx and uWSGI supports runing scripts as CGI. Then again - after configuration was done Bugzilla's main page was loading for about 3 seconds (server isn't that slow).

The Struggle began :)

Google quickly answered with issue for Bugzilla: https://bugzilla.mozilla.org/show_bug.cgi?id=316665 (note that it was created in 2005 ;) ). There is patch which adds support for FastCGI to Bugzilla (and possibly PSGI).

You can find same patch on GitHub: https://github.com/Stackato-Apps/bugzilla/blob/master/psgi.patch . From header one can deduce that it was written for version 4.0.3 (newest is 4.2.4).

Patch didn't apply successfully for newest version so, based on it, I've created updated one. The next step - configure uwsgi and start application. Application started without errors - even looked like was working properly - until I've wanted to log in. I've spotted strange error in logs - "no data". Additionally, page which was supposed to render was cut in half (and there was info about incorrect password/login).

Following new trail - no data on POST request at server side, I've luckily found another issue, this time it was for CGI-Emulate-PSGI.

Turns out the problem was just that - application doesn't receive POST data when uWSGI is used, fixing it looked easy... but I really don't know Perl :) And there is that bug with cutting page in half which can or cannot be related to first.

PSGI emulation from patch uses Plack so I possibly could use it to run app without uWSGI - on FastCGI which is supported by Nginx.

In short

Bugzilla as FastCGI application - the fast way.

  • Download Bugzilla package
  • Apply fastcgi patch
  • Configure BugzillÄ™ with checksetup.pl
  • Configure Nginx'a and start Plack service (files and commands below)

If you're using Gentoo Linux you can just add glorpen-overlay with patched ebuild for Bugzilla.

Final configuration

For Nginx:

#cgi leftovers - but why would anyone remove it?
location ~ ^(.*\.(jpe?g|gif|css|js|png|ico))$ {
        alias /srv/bugzilla/app/$1;
}

location  /  {
        include        fastcgi_params;
        fastcgi_param  SCRIPT_NAME      "";
        fastcgi_pass   unix:///srv/bugzilla/fastcgi.socket;
}

Starting Plack:

#!/bin/bash

d="/srv/bugzilla"
plackup -s FCGI -S "${d}"/fastcgi.socket "${d}"/app/app.psgi --pid "${d}"/plackup.pid -D

and lastly - stopping Plack:

#!/bin/bash

kill `cat /srv/bugzilla/plackup.pid`