SoundCloud download initial commit
This commit is contained in:
parent
d9117721b9
commit
04333aaa29
5 changed files with 82 additions and 2 deletions
|
@ -77,6 +77,19 @@ abstract class ThirdPartyController extends Zend_Controller_Action {
|
|||
$this->_service->upload($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download the file with the given id from a third-party service
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws Zend_Controller_Response_Exception thrown if download fails for any reason
|
||||
*/
|
||||
public function downloadAction() {
|
||||
$request = $this->getRequest();
|
||||
$id = $request->getParam('id');
|
||||
$this->_service->download($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the file with the given id from a third-party service
|
||||
*
|
||||
|
|
|
@ -29,6 +29,11 @@ class SoundcloudService extends ThirdPartyCeleryService implements OAuth2 {
|
|||
*/
|
||||
protected static $_CELERY_UPLOAD_TASK_NAME = 'soundcloud-upload';
|
||||
|
||||
/**
|
||||
* @var string celery task name for third party uploads
|
||||
*/
|
||||
protected static $_CELERY_DOWNLOAD_TASK_NAME = 'soundcloud-download';
|
||||
|
||||
/**
|
||||
* @var string celery task name for third party deletions
|
||||
*/
|
||||
|
|
|
@ -14,6 +14,11 @@ abstract class ThirdPartyCeleryService extends ThirdPartyService {
|
|||
*/
|
||||
protected static $_CELERY_UPLOAD_TASK_NAME;
|
||||
|
||||
/**
|
||||
* @var string celery task name for third-party uploads
|
||||
*/
|
||||
protected static $_CELERY_DOWNLOAD_TASK_NAME;
|
||||
|
||||
/**
|
||||
* @var string celery task name for third-party deletion
|
||||
*/
|
||||
|
@ -41,6 +46,31 @@ abstract class ThirdPartyCeleryService extends ThirdPartyService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a SoundCloud track identifier, download a track from SoundCloud.
|
||||
*
|
||||
* If no track identifier is given, download all tracks for the currently
|
||||
* authenticated SoundCloud user.
|
||||
*
|
||||
* @param int|null $trackId a SoundCloud track identifier
|
||||
*/
|
||||
public function download($trackId = null) {
|
||||
$namespace = new Zend_Session_Namespace('csrf_namespace');
|
||||
$csrfToken = $namespace->authtoken;
|
||||
$data = array(
|
||||
'callback_url' => 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['HTTP_HOST'] . '/media/post>csrf_token=' . $csrfToken,
|
||||
'token' => $this->_accessToken,
|
||||
'track_id' => $trackId
|
||||
);
|
||||
try {
|
||||
CeleryService::sendCeleryMessage(static::$_CELERY_DOWNLOAD_TASK_NAME,
|
||||
static::$_CELERY_EXCHANGE_NAME,
|
||||
$data);
|
||||
} catch (Exception $e) {
|
||||
Logging::info("Invalid request: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the file with the given identifier from a third-party service
|
||||
*
|
||||
|
@ -52,7 +82,7 @@ abstract class ThirdPartyCeleryService extends ThirdPartyService {
|
|||
public function delete($fileId) {
|
||||
$serviceId = $this->getServiceId($fileId);
|
||||
if ($serviceId == 0) {
|
||||
throw new ServiceNotFoundException("No service found for file with ID $fileId");
|
||||
throw new ServiceNotFoundException("No service ID found for file with ID $fileId");
|
||||
}
|
||||
$data = array(
|
||||
'token' => $this->_accessToken,
|
||||
|
|
|
@ -124,6 +124,13 @@ abstract class ThirdPartyService {
|
|||
*/
|
||||
abstract function upload($fileId);
|
||||
|
||||
/**
|
||||
* Download the file with the given identifier from a third-party service
|
||||
*
|
||||
* @param int $trackId the third-party service track identifier
|
||||
*/
|
||||
abstract function download($trackId);
|
||||
|
||||
/**
|
||||
* Delete the file with the given identifier from a third-party service
|
||||
*
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import json
|
||||
import urllib2
|
||||
import requests
|
||||
import soundcloud
|
||||
from celery import Celery
|
||||
from celery.utils.log import get_task_logger
|
||||
|
@ -33,12 +34,36 @@ def soundcloud_upload(data, token, file_path):
|
|||
data['asset_data'].close()
|
||||
return json.dumps(track.fields())
|
||||
|
||||
|
||||
@celery.task(name='soundcloud-download', acks_late=True)
|
||||
def soundcloud_download(token, callback_url, track_id=None):
|
||||
"""
|
||||
|
||||
:param token: OAuth2 client access token
|
||||
:param track_id: SoundCloud track identifier
|
||||
:rtype: None
|
||||
"""
|
||||
client = soundcloud.Client(access_token=token)
|
||||
try:
|
||||
tracks = client.get('/me/tracks') if track_id is None else {client.get('/tracks/%s' % track_id)}
|
||||
for track in tracks:
|
||||
if track.downloadable:
|
||||
track_file = client.get(track.download_url)
|
||||
with track_file as f:
|
||||
requests.post(callback_url, data=f)
|
||||
except Exception as e:
|
||||
logger.info('Error during file download: {0}'.format(e.message))
|
||||
logger.info(str(e))
|
||||
raise e
|
||||
|
||||
|
||||
@celery.task(name='soundcloud-delete', acks_late=True)
|
||||
def soundcloud_delete(token, track_id):
|
||||
"""
|
||||
Delete a file from SoundCloud
|
||||
|
||||
:param token: OAuth2 client access token
|
||||
:param token: OAuth2 client access token
|
||||
:param track_id: SoundCloud track identifier
|
||||
|
||||
:return: the SoundCloud response object
|
||||
:rtype: dict
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue