Skip to content

Commit 825d497

Browse files
authored
Merge pull request #33 from Frameio/jh/add-download
Add Download function
2 parents 399a48e + 58d0bc4 commit 825d497

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

frameioclient/client.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .upload import FrameioUploader
33
from requests.adapters import HTTPAdapter
44
from requests.packages.urllib3.util.retry import Retry
5+
from .download import FrameioDownloader
56

67
class PaginatedResponse(object):
78
def __init__(self, results=[], page=0, page_size=0, total=0, total_pages=0):
@@ -232,11 +233,26 @@ def upload(self, asset, file):
232233
233234
Example::
234235
235-
client.upload(asset, open('example.mp4'))
236+
client.upload(asset, open('example.mp4')) // TODO fix this example (bad way of opening file)
236237
"""
237238
uploader = FrameioUploader(asset, file)
238239
uploader.upload()
239-
240+
241+
def download(self, asset, download_folder):
242+
"""
243+
Download an asset. The method will exit once the file is downloaded.
244+
245+
:Args:
246+
asset (object): The asset object.
247+
download_folder (path): The location to download the file to.
248+
249+
Example::
250+
251+
client.download(asset, "~./Downloads")
252+
"""
253+
downloader = FrameioDownloader(asset, download_folder)
254+
downloader.download()
255+
240256
def get_comment(self, comment_id, **kwargs):
241257
"""
242258
Get a comment.

frameioclient/download.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import requests
2+
import math
3+
import os
4+
5+
class FrameioDownloader(object):
6+
def __init__(self, asset, download_folder):
7+
self.asset = asset
8+
self.download_folder = download_folder
9+
10+
def download(self):
11+
# TODO add test to chere here to make sure the URL we download is coming from our S3 buckets
12+
# Grab the filename from the passed dictionary and store it.
13+
original_filename = self.asset['name']
14+
final_destination = os.path.join(self.download_folder, original_filename)
15+
16+
# Grab the origial_url from the passed dictionary, then download the file
17+
url = self.asset['original']
18+
r = requests.get(url)
19+
open(final_destination, 'wb').write(r.content)
20+

0 commit comments

Comments
 (0)