def test_get_filename_from_cd(): """Test the method get_filename_from_cd""" test_data_first = 'attachment; filename="image.png"' test_data_none = None expected_response_first = ('"image', '.png"') expected_response_none = ('', '') response_first = get_filename_from_cd(test_data_first) response_none = get_filename_from_cd(test_data_none) assert expected_response_first == response_first assert expected_response_none == response_none
def test_get_filename_from_cd(self): """Test the method get_filename_from_cd""" test_data_first = 'attachment; filename="image.png"' test_data_none = None expected_response_first = ('"image', '.png"') expected_response_none = ('', '') response_first = get_filename_from_cd(test_data_first) response_none = get_filename_from_cd(test_data_none) self.assertEqual(expected_response_first, response_first) self.assertEqual(expected_response_none, response_none)
def _download_media(data, srv, dir_path, settings): """ Downloads the media and saves it """ if srv not in DOWNLOAD_FIEDLS: return for i in DOWNLOAD_FIEDLS[srv]: if not data[i]: continue if not settings[DOWNLOAD_FIEDLS[srv][i][0]]: continue path = DOWNLOAD_FIEDLS[srv][i][1] if srv == 'speakers': path %= make_filename(data['name']), data['id'] elif srv == 'sponsors': path %= make_filename(data['name']), data['id'] elif srv != 'event': path = path % (data['id']) if data[i].find('.') > -1: # add extension ext = data[i].rsplit('.', 1)[1] if ext.find('/') == -1: path += '.' + ext full_path = dir_path + path # make dir cdir = full_path.rsplit('/', 1)[0] if not os.path.isdir(cdir): os.makedirs(cdir) # download and set url = data[i] if not is_downloadable(url): continue try: r = requests.get(url, allow_redirects=True) ext = get_filename_from_cd(r.headers.get('content-disposition'))[1] full_path += ext path += ext open(full_path, 'wb').write(r.content) data[i] = path except Exception: pass