import urllib import re from .. import helpers def search(request): searchQuery = helpers.getFromRequest(request, "query") if not searchQuery: searchQuery = "rickroll" try: html = urllib.request.urlopen("https://www.youtube.com/results?search_query={}".format(urllib.parse.quote_plus(searchQuery))) videoList = re.findall(r"watch\?v=(\S{11})", html.read().decode()) videoIds = dict() for i in range(len(videoList)): videoIds.update({i: videoList[i]}) return {"status": "pass", "query": searchQuery, "videoIds": videoIds} except Exception as e: print(e) return {"status": "error", "details": { "error_code": 666, "error_details": "Something went wrong..." }}, 500