Skip to content

Commit f088f5f

Browse files
committed
Add download method
1 parent 399a48e commit f088f5f

2 files changed

Lines changed: 37 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
# Grab the filename from the passed dictionary and store it.
12+
original_filename = self.asset['name']
13+
final_destination = os.path.join(self.download_folder, original_filename)
14+
15+
# Grab the origial_url from the passed dictionary, then download the file
16+
url = self.asset['original']
17+
r = requests.get(url, allow_redirects=True)
18+
open(final_destination, 'wb').write(r.content)
19+

0 commit comments

Comments
 (0)