diff --git a/cmd/resolver/Makefile b/cmd/resolver/Makefile index 34beab7..789f3c9 100644 --- a/cmd/resolver/Makefile +++ b/cmd/resolver/Makefile @@ -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 \ diff --git a/cmd/resolver/function.py b/cmd/resolver/function.py index 29dc7b2..e89e7af 100644 --- a/cmd/resolver/function.py +++ b/cmd/resolver/function.py @@ -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') diff --git a/cmd/resolver/function_test.py b/cmd/resolver/function_test.py index ee3e916..5d5d607 100644 --- a/cmd/resolver/function_test.py +++ b/cmd/resolver/function_test.py @@ -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']) diff --git a/cmd/resolver/requirements.txt b/cmd/resolver/requirements.txt index 4eb4bad..2b3f89d 100644 --- a/cmd/resolver/requirements.txt +++ b/cmd/resolver/requirements.txt @@ -1,2 +1 @@ -requests==2.19.1 youtube_dl==2019.03.09 \ No newline at end of file