Script to add titles to WWDC videos

I like to store my WWDC videos in EagleFiler so that I can easily search them, add notes, and keep track of which ones I’ve already watched. I download them using the unofficial WWDC app, but it saves filenames like wwdc2020_10687_hd.mp4, which makes it hard to see what each video is. Here’s a quick and dirty script combo to fetch the session names and put them into the record’s title in EagleFiler:

wwdc-title.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, urllib, re
# wwdc2021-10015_hd.mp4
filename = sys.argv[1]
match = re.search("(wwdc\d{4})[-_](.+)_", filename)
# https://developer.apple.com/videos/play/wwdc2021/102/
# https://developer.apple.com/videos/play/wwdc2021/10085/
url = "https://developer.apple.com/videos/play/%s/%s/" % (match.group(1), match.group(2))
data = urllib.urlopen(url).read()
string = unicode(data, "utf-8")
# <title>Apple’s privacy pillars in focus - WWDC 2021 - Videos - Apple Developer</title>
title = re.search(u"<title>(.*?) -", string).group(1)
print title.encode("utf-8")

WWDC Title.applescript

tell application "EagleFiler"
    set _records to selected records of browser window 1
    repeat with _record in _records
        set _record's title to my titleFromFilename(_record's filename)
    end repeat
end tell

on titleFromFilename(_filename)
    return do shell script "/path/to/wwdc-title.py " & _filename's quoted form
end titleFromFilename
2 Likes

Python has been removed from macOS, so here’s an updated version that’s pure AppleScript:

use AppleScript version "2.4"
use framework "Foundation"

tell application "EagleFiler"
    set _records to selected records of browser window 1
    repeat with _record in _records
        set _record's title to my titleFromFilename(_record's filename)
    end repeat
end tell

on titleFromFilename(_filename)
    set _url to my urlFromFilename(_filename)
    set _pageString to my (NSString's stringWithContentsOfURL:_url encoding:(my NSUTF8StringEncoding) |error|:(missing value))
    return my titleFromPageSource(_pageString)
end titleFromFilename

on urlFromFilename(_filename)
    set _regex to my (NSRegularExpression's regularExpressionWithPattern:"(wwdc\\d{4})[-_](.+)_" options:0 |error|:(missing value))
    set _nsString to my (NSString's stringWithString:_filename)
    set _textCheckingResult to _regex's firstMatchInString:_nsString options:0 range:{0, _nsString's |length|()}
    set _year to my captureAt(1, _nsString, _textCheckingResult)
    set _session to my captureAt(2, _nsString, _textCheckingResult)
    set _urlString to "https://developer.apple.com/videos/play/" & _year & "/" & _session & "/"
    return my (NSURL's URLWithString:_urlString)
end urlFromFilename

on titleFromPageSource(_source)
    set _regex to my (NSRegularExpression's regularExpressionWithPattern:"<title>(.*?) -" options:0 |error|:(missing value))
    set _nsString to my (NSString's stringWithString:_source)
    set _textCheckingResult to _regex's firstMatchInString:_nsString options:0 range:{0, _nsString's |length|()}
    return my captureAt(1, _nsString, _textCheckingResult)
end titleFromPageSource

on captureAt(_index, _nsString, _textCheckingResult)
    set _range to _textCheckingResult's rangeAtIndex:_index
    set _group to _nsString's substringWithRange:_range
    return _group as Unicode text
end captureAt