I need this every so often, and I keep on having to rediscover it. So here it is: a bash function that prints a random line from a given file:
randline () { tail -n +$(expr $RANDOM % $(wc -l $1 | cut -d\ -f1) + 1) $1 | head -n1; }
Just pushed the video sources (for the Unity video lens) for TED Talks (thanks to Roberto Alsina for putting together an initial screenscraper) and Canal Encuentro (thanks to Facundo Batista’s project Encuentro for making this one super easy in the end); this morning I added ABC iView (thanks to Marius Mather for suggesting that one), and last night, Vimeo (John Lea pointed out it was an obvious omission from the original lineup) and Sci-Fi London (kudos to the Sci-Fi London team).
No geocoding being done yet (this server is still in the labs) so you’ll all get results from all the sources listed below. In parentheses, the country codes where that source will be available (i.e. right now you get results, but no amount of clicking will let you see the video; when this goes out of labs, it’ll not appear amongst your sources if you’re not going to be able to watch it). So, in alphabetical order,
- ABC iView (AU)
- Amazon (US)
- BBC iPlayer (UK)
- Encuentro
- Sci-Fi London
- TED Talks
- VODO
- Vimeo
- YouTube Education (varies)
- YouTube Movies (varies)
- YouTube Shows (varies)
hopefully there’ll be more to come soon.
"Vulvas are much like snowflakes, no two are identical and when it gets really cold they flutter down from the sky, getting caught in your eyelashes."
— Erika Moen, explaining girl-on-girl action. Not suitable for some workplaces. And if you’ve been doing your homework, nothing really new. Go read.
sqlite has fairly decent full-text search, if you have it compiled in (and most reasonable places you can get your sqlites, they will have it). Only problem is that in order to rank the results—a rather common task when you’re doing full-text search in the first place—you need a bit of rocketsciencing. All very nicely documented mind you, but writing C for a web search app is not really my thing. Fortunately creating user-defined functions is also very easy to do from the python sqlite db-api interface, and also well documented. However, it’s not immediately obvious, and after having to re-learn how to do it, I thought I’d share.
So, here’s how I do it:
def make_rank_func(weights):
def rank(matchinfo):
# matchinfo is defined as returning 32-bit unsigned integers
# in machine byte order
# http://www.sqlite.org/fts3.html#matchinfo
# and struct defaults to machine byte order
matchinfo = struct.unpack("I"*(len(matchinfo)/4), matchinfo)
it = iter(matchinfo[2:])
return sum(x[0]*w/x[1]
for x, w in zip(zip(it, it, it), weights)
if x[1])
return rank
def get_results(query):
db = sqlite3.connect('tv.db')
db.row_factory = sqlite3.Row
db.create_function("rank", 1, make_rank_func((1., .1, 0, 0)))
c = db.cursor()
c.execute("select title, img, url from"
" (select rank(matchinfo(tv)) as r, title, img, url"
" from tv where tv match ?)"
" where r > .03 order by r desc limit 10", (query,))
return c.fetchall()
the nested select there is because I don’t want to call the rank function multiple times per row.
I seem to have lost my habit of posting crap, or anything at all. Ah well, it happens. Let’s see if we can get it back.
"You thought yesterday was bad? Well, now you have a enormous metal chicken to deal with. Perspective. Now you have it."
— And that’s why you should learn to pick your battles.