-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def video_mapper(item): | ||
snippet = item['snippet'] | ||
return { | ||
'thumbnail_url': snippet['thumbnails']['default']['url'], | ||
'title': snippet['title'], | ||
'video_url': item['id']['videoId'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from .mappers import video_mapper | ||
|
||
from celery.result import AsyncResult | ||
from web.tasks import extract_info | ||
|
||
def pass_query(fn): | ||
def wraps(*args): | ||
request, *rest = args | ||
return fn(request.query, *rest) | ||
return wraps | ||
|
||
@pass_query | ||
def search_video(query, api_key): | ||
params = { | ||
'part': 'snippet', | ||
'maxResults': 25, | ||
'q': query, | ||
'key': api_key, | ||
'type': 'video', | ||
} | ||
response = requests.get(SEARCH_ENDPOINT, params=params) | ||
json = response.json() | ||
|
||
results = list( | ||
map(video_mapper, json['items']) | ||
) | ||
return results | ||
|
||
@pass_query | ||
def get_media_types(query): | ||
async_result = extract_info.delay(query) | ||
return { | ||
'task_id': async_result.task_id | ||
} | ||
|
||
@pass_query | ||
def get_task_status(query): | ||
async_result = AsyncResult(query) | ||
return { | ||
'state': async_result.state, | ||
'result': async_result.result, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,34 @@ | ||
from flask import jsonify, request, current_app | ||
import requests | ||
from flask import jsonify, request, current_app | ||
|
||
from .utils import search_video, get_media_types, get_task_status | ||
|
||
|
||
SEARCH_ENDPOINT = 'https://www.googleapis.com/youtube/v3/search/' | ||
|
||
def search_video(query): | ||
api_key = current_app.config.get('YOUTUBE_API_KEY', '') | ||
params = { | ||
'part': 'snippet', | ||
'maxResults': 25, | ||
'q': query, | ||
'key': api_key, | ||
'type': 'video', | ||
} | ||
response = requests.get(SEARCH_ENDPOINT, params=params) | ||
json = response.json() | ||
|
||
def video_dct(item): | ||
snippet = item['snippet'] | ||
return { | ||
'thumbnail_url': snippet['thumbnails']['default']['url'], | ||
'title': snippet['title'], | ||
'video_url': item['id']['videoId'], | ||
} | ||
|
||
results = list( | ||
map(video_dct, json['items']) | ||
) | ||
return results | ||
|
||
class GetRequest(): | ||
def __init__(self, req): | ||
self.query = req.args.get('q', '') | ||
|
||
|
||
def create_views(blueprint): | ||
|
||
@blueprint.route('/search') | ||
def search(): | ||
query = request.args.get('q', '') | ||
results = search_video(query) | ||
return jsonify(results) | ||
api_key = current_app.config.get('YOUTUBE_API_KEY', '') | ||
result = search_video(GetRequest(request), api_key) | ||
return jsonify(result) | ||
|
||
@blueprint.route('/media_types') | ||
def media_types(): | ||
result = get_media_types(GetRequest(request)) | ||
return jsonify(result) | ||
|
||
@blueprint.route('/task_status') | ||
def task_status(): | ||
''' Poll task status. | ||
TODO: deprecate to favor SSEs. | ||
''' | ||
result = get_task_status(GetRequest(request)) | ||
return jsonify(result) |