import urllib import re from .. import helpers def search(request): try: if request.method == 'POST': searchQuery = request.form['query'] else: searchQuery = request.args['query'] if searchQuery.strip() in ['', None]: raise Exception() except: searchQuery = "rickroll" try: if request.method == 'POST': signature = request.form['signature'] else: signature = request.args['signature'] except: return {"status": "error", "details": { "error_code": 103, "error_details": "No signature" }} if not helpers.checkSignature(signature): return {"status": "error", "details": { "error_code": 105, "error_details": " signature" }} 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}