1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

Query metadata directly from DynamoDB

This commit is contained in:
Maksym Pavlenko
2019-03-18 20:50:35 -07:00
parent b45f7225aa
commit 272da90480
4 changed files with 36 additions and 12 deletions

View File

@ -7,7 +7,7 @@ build:
deploy: build
aws --profile Podsync lambda create-function \
--function-name Resolver \
--role $(shell aws --profile Podsync iam get-role --role-name AWSLambdaBasicExecutionRole --query 'Role.Arn' --output text) \
--role $(shell aws --profile Podsync iam get-role --role-name PodsyncResolverLambdaRole --query 'Role.Arn' --output text) \
--runtime python3.7 \
--handler function.handler \
--zip-file fileb://function.zip \

View File

@ -1,15 +1,16 @@
import os
import requests
import youtube_dl
METADATA_URL = os.getenv('METADATA_URL', 'http://podsync.net/api/metadata/{feed_id}')
print('Using metadata URL template: ' + METADATA_URL)
import boto3
class InvalidUsage(Exception):
pass
dynamodb = boto3.resource('dynamodb')
feeds_table = dynamodb.Table(os.getenv('RESOLVER_DYNAMO_FEEDS_TABLE', 'Feeds'))
opts = {
'quiet': True,
'no_warnings': True,
@ -46,22 +47,37 @@ def download(feed_id, video_id):
if not video_id:
raise InvalidUsage('Invalid video id')
# Pull metadata from API server
metadata_url = METADATA_URL.format(feed_id=feed_id, video_id=video_id)
r = requests.get(url=metadata_url)
json = r.json()
# Query feed metadata info from DynamoDB
item = _get_metadata(feed_id)
# Build URL
provider = json['provider']
provider = item['provider']
tpl = url_formats[provider]
if not tpl:
raise InvalidUsage('Invalid feed')
url = tpl.format(video_id)
redirect_url = _resolve(url, json)
redirect_url = _resolve(url, item)
return redirect_url
def _get_metadata(feed_id):
response = feeds_table.get_item(
Key={'HashID': feed_id},
ProjectionExpression='#P,#F,#Q',
ExpressionAttributeNames={
'#P': 'Provider',
'#F': 'Format',
'#Q': 'Quality',
},
)
item = response['Item']
# Make dict keys lowercase
return dict((k.lower(), v) for k, v in item.items())
def _resolve(url, metadata):
if not url:
raise InvalidUsage('Invalid URL')

View File

@ -25,3 +25,12 @@ class TestYtdl(unittest.TestCase):
self.assertIsNotNone(
ytdl._resolve('https://vimeo.com/275211960', {'format': 'video', 'quality': 'high', 'provider': 'vimeo'})
)
class TestDynamo(unittest.TestCase):
def test_metadata(self):
item = ytdl._get_metadata('86qZ')
self.assertIsNotNone(item)
self.assertIsNotNone(item['format'])
self.assertIsNotNone(item['quality'])
self.assertIsNotNone(item['provider'])

View File

@ -1,2 +1 @@
requests==2.19.1
youtube_dl==2019.03.09