youtube-dl does something similar (it works on a lot more than just youtube). AFAIK each site needs a slightly altered code. You could have a look at the source on their github.
It might be as easy as forking the project and creating your own extractor.py.