File size: 1,070 Bytes
66ca64a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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}