2
\$\begingroup\$

https://github.com/floris-vos/vimeo_search_python

I made a small function that takes search terms as arguments and returns a dictionary with items, each holding an URL, title, duration, channel, and date published for a search result (the title is the key; a format that I have found extremely useful). The function uses the Requests module, so bypasses the need for API. It's kind of inspired by: https://pypi.org/project/youtube-search/

Just on a side note: I'm working on similar code for Dailymotion, and probably might in the end just make a tool that can search all big streaming websites. Still figuring out how to put it in PIP.

It works just fine, but any improvements would of course be interesting:

from requests import get as request
from urllib.parse import quote_plus
from json import loads

class vimeo_search:

    def search_vimeo(self, search_terms):
        encoded_search = quote_plus(search_terms)
        item = {}
        url = f"https://vimeo.com/search?q={encoded_search}"
        try:
            headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
            response = request(url, timeout=10, headers=headers).text
        except: 
            return
        index1 = response.index("vimeo.config = ")
        index1 = response.index("[", index1)
        index2 = response.index("}}}]", index1) + 4
        text = response[index1:index2]
        dicti = loads(text)
        results = dict()
        for song in dicti:
            url = song["clip"]["link"]
            title = song["clip"]["name"]
            play_time = song["clip"]["duration"]
            date = song["clip"]["created_time"]
            channel = song["clip"]["user"]["name"]
            results[title] = {
                "url": url,
                "play_time":play_time,
                "date":date[:9],
                "channel":channel,
                "title":title
            }
        return results

:::::::::EXAMPLE:::::

To search for Johnny Cash videos:

vimeo = vimeo_search()

print(vimeo.search_vimeo("johnny cash"))

Returns:

{'The Johnny Cash Project': {'url': 'https://vimeo.com/15416762', 'play_time': 347, 'date': '2010-09-3', 'channel': 'Chris Milk', 'title': 'The Johnny Cash Project'}, 'YELAWOLF "Johnny Cash"': {'url': 'https://vimeo.com/130398770', 'play_time': 266, 'date': '2015-06-1', 'channel': 'Martin Linss', 'title': 'YELAWOLF "Johnny Cash"'}, 'Johnny Cash - Hurt': {'url': 'https://vimeo.com/213515290', 'play_time': 237, 'date': '2017-04-1', 'channel': 'Racerboy Roy', 'title': 'Johnny Cash - Hurt'}, 'Johnny Cash - Solitary Man': {'url': 'https://vimeo.com/46188490', 'play_time': 147, 'date': '2012-07-2', 'channel': 'MetalMicha', 'title': 'Johnny Cash - Solitary Man'}, 'Johnny Cash': {'url': 'https://vimeo.com/38894934', 'play_time': 175, 'date': '2012-03-2', 'channel': 'Dan Cohoes', 'title': 'Johnny Cash'}, 'The Wanderer by Johnny Cash': {'url': 'https://vimeo.com/40623057', 'play_time': 284, 'date': '2012-04-1', 'channel': 'Matt Devir', 'title': 'The Wanderer by Johnny Cash'}, 'Johnny Cash - Folsom Prison Blues (Pete Rock Remix)': {'url': 'https://vimeo.com/2925003', 'play_time': 181, 'date': '2009-01-2', 'channel': 'John West', 'title': 'Johnny Cash - Folsom Prison Blues (Pete Rock Remix)'}, 'Johnny Cash - Hurt (Legendado)': {'url': 'https://vimeo.com/16554146', 'play_time': 232, 'date': '2010-11-0', 'channel': 'Rafael', 'title': 'Johnny Cash - Hurt (Legendado)'}, '"One", Johnny Cash': {'url': 'https://vimeo.com/145911240', 'play_time': 233, 'date': '2015-11-1', 'channel': 'Shangrila Club', 'title': '"One", Johnny Cash'}, 'Johnny Cash - Heart of gold': {'url': 'https://vimeo.com/49962875', 'play_time': 182, 'date': '2012-09-2', 'channel': 'Nelsony23', 'title': 'Johnny Cash - Heart of gold'}, 'Still Hurtin - Johnny Cash (RetroSpector Mix)': {'url': 'https://vimeo.com/4230107', 'play_time': 437, 'date': '2009-04-1', 'channel': 'Phil RetroSpector', 'title': 'Still Hurtin - Johnny Cash (RetroSpector Mix)'}, 'Johnny Cash "Farther Along" | Official Music Video': {'url': 'https://vimeo.com/gabrieltick/johnnycash-fartheralong', 'play_time': 236, 'date': '2021-01-0', 'channel': 'gabriel tick', 'title': 'Johnny Cash "Farther Along" | Official Music Video'}, 'The Johnny Cash Project Documentary by Radical Media': {'url': 'https://vimeo.com/18157343', 'play_time': 344, 'date': '2010-12-2', 'channel': 'Livio Rajh', 'title': 'The Johnny Cash Project Documentary by Radical Media'}, 'Johnny Cash - Wayfaring Stranger': {'url': 'https://vimeo.com/62079563', 'play_time': 200, 'date': '2013-03-1', 'channel': 'Peter', 'title': 'Johnny Cash - Wayfaring Stranger'}, 'Johnny Cash at Folsom Prison - Trailer': {'url': 'https://vimeo.com/12030988', 'play_time': 134, 'date': '2010-05-2', 'channel': 'Northern Light Productions', 'title': 'Johnny Cash at Folsom Prison - Trailer'}, 'Johnny Cash - Spiritual': {'url': 'https://vimeo.com/31212811', 'play_time': 308, 'date': '2011-10-2', 'channel': 'westend_zoo', 'title': 'Johnny Cash - Spiritual'}, 'Johnny Cash - Personal Jesus': {'url': 'https://vimeo.com/94636060', 'play_time': 200, 'date': '2014-05-0', 'channel': 'WidziszTo.com', 'title': 'Johnny Cash - Personal Jesus'}}

EDIT: Just in case the above is of use to anybody, here is a link to similar code for Dailymotion. Others to come. https://github.com/floris-vos/daily-motion-search-python/tree/main

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I would change results to be a list, because title is not unique. Using a non-unique string as a key could mean result returns with fewer results then are on the page. Also you define variables url, title, etc but you don't manipulate them, so just setting the object with the value directly might be cleaner

results = []
for song in dicti:
    results.append({
        "url": song["clip"]["link"],
        ...
    })

This would mean results has a different format but the title is a key/value inside results object items still.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the feedback! As to your first tip, I think that I will rather enumerate them (append (1), (2), etc to duplicates), because I found that making a dictionary is kind of handy because putting them in a list only requires list(results.keys()). But good tip anyways, they are indeed not unique! Also, yes, I can leave out those non-manipulated variables! Will change as suggested (and imply it in the Dailymotion searcher and other future searchers) \$\endgroup\$ Feb 6 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.