From e1f1807f5a1eb5fea65be6738613a9ed85ca3b6a Mon Sep 17 00:00:00 2001 From: drigato Date: Tue, 2 Dec 2014 18:46:17 -0500 Subject: [PATCH] SAAS-503: PYPO -> Use the REST API to download files Removed Amazon S3 specific code --- airtime_mvc/application/models/Schedule.php | 16 +++---- .../application/models/airtime/CloudFile.php | 2 +- python_apps/pypo/pypofile.py | 45 ++++++++++++++----- 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/airtime_mvc/application/models/Schedule.php b/airtime_mvc/application/models/Schedule.php index 2483a8b54..339464906 100644 --- a/airtime_mvc/application/models/Schedule.php +++ b/airtime_mvc/application/models/Schedule.php @@ -771,9 +771,8 @@ SQL; * @param Array $item schedule info about one track * @param Integer $media_id scheduled item's cc_files id * @param String $uri path to the scheduled item's physical location - * @param String $amazonS3ResourceId scheduled item's Amazon S3 resource id, if applicable */ - private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $amazonS3ResourceId) + private static function createFileScheduleEvent(&$data, $item, $media_id, $uri, $downloadURL, $filesize) { $start = self::AirtimeTimeToPypoTime($item["start"]); $end = self::AirtimeTimeToPypoTime($item["end"]); @@ -807,11 +806,10 @@ SQL; 'end' => $end, 'show_name' => $item["show_name"], 'replay_gain' => $replay_gain, - 'independent_event' => $independent_event + 'independent_event' => $independent_event, + 'download_url' => $downloadURL, + 'filesize' => $filesize, ); - if (!is_null($amazonS3ResourceId)) { - $schedule_item["amazonS3_resource_id"] = $amazonS3ResourceId; - } if ($schedule_item['cue_in'] > $schedule_item['cue_out']) { $schedule_item['cue_in'] = $schedule_item['cue_out']; @@ -945,9 +943,11 @@ SQL; $storedFile = Application_Model_StoredFile::RecallById($media_id); $file = $storedFile->getPropelOrm(); $uri = $file->getAbsoluteFilePath(); + // TODO: fix this URL + $downloadURL = "http://localhost/rest/media/$media_id/download"; + $filesize = $file->getFileSize(); - $amazonS3ResourceId = $file->getResourceId(); - self::createFileScheduleEvent($data, $item, $media_id, $uri, $amazonS3ResourceId); + self::createFileScheduleEvent($data, $item, $media_id, $uri, $downloadURL, $filesize); } elseif (!is_null($item['stream_id'])) { //row is type "webstream" diff --git a/airtime_mvc/application/models/airtime/CloudFile.php b/airtime_mvc/application/models/airtime/CloudFile.php index 099f7c4fa..cd2d22657 100644 --- a/airtime_mvc/application/models/airtime/CloudFile.php +++ b/airtime_mvc/application/models/airtime/CloudFile.php @@ -85,7 +85,7 @@ class CloudFile extends BaseCloudFile /** * - * Deletes the file from Amazon S3 + * Deletes the file from cloud storage */ public function deletePhysicalFile() { diff --git a/python_apps/pypo/pypofile.py b/python_apps/pypo/pypofile.py index 2c14bbbf5..c6caca342 100644 --- a/python_apps/pypo/pypofile.py +++ b/python_apps/pypo/pypofile.py @@ -9,10 +9,14 @@ import shutil import os import sys import stat - +import urllib2 +import base64 +import ConfigParser from std_err_override import LogWriter +CONFIG_PATH = '/etc/airtime/airtime.conf' + # configure logging logging.config.fileConfig("logging.cfg") logger = logging.getLogger() @@ -38,11 +42,14 @@ class PypoFile(Thread): src = media_item['uri'] dst = media_item['dst'] + """ try: src_size = os.path.getsize(src) except Exception, e: self.logger.error("Could not get size of source file: %s", src) return + """ + src_size = media_item['filesize'] dst_exists = True try: @@ -68,7 +75,18 @@ class PypoFile(Thread): """ copy will overwrite dst if it already exists """ - shutil.copy(src, dst) + #shutil.copy(src, dst) + config = self.read_config_file(CONFIG_PATH) + CONFIG_SECTION = "general" + username = config.get(CONFIG_SECTION, 'api_key') + url = media_item['download_url'] + + """ + Make HTTP request here + """ + + with open(dst, "wb") as code: + code.write(file.read()) #make file world readable os.chmod(dst, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) @@ -108,6 +126,19 @@ class PypoFile(Thread): return media_item + def read_config_file(self, config_path): + """Parse the application's config file located at config_path.""" + config = ConfigParser.SafeConfigParser() + try: + config.readfp(open(config_path)) + except IOError as e: + logging.debug("Failed to open config file at %s: %s" % (config_path, e.strerror)) + sys.exit() + except Exception: + logging.debug(e.strerror) + sys.exit() + + return config def main(self): while True: @@ -133,15 +164,7 @@ class PypoFile(Thread): media_item = self.get_highest_priority_media_item(self.media) if media_item is not None: - """ - If an object_name exists the file is stored on Amazon S3 - """ - if 'amazonS3_resource_id' in media_item: - csd = CloudStorageDownloader() - csd.download_obj(media_item['dst'], media_item['amazonS3_resource_id']) - media_item['file_ready'] = True - else: - self.copy_file(media_item) + self.copy_file(media_item) except Exception, e: import traceback top = traceback.format_exc()