def upload_to_what(request, book_upload): book_upload.full_clean() if not book_upload.what_torrent_file: raise Exception('what_torrent is no') if not book_upload.cover_url: raise Exception('cover_url is no') print 'Sending request for upload to what.cd' what = get_what_client(request) payload_files = dict() payload_files['file_input'] = ('torrent.torrent', book_upload.what_torrent_file) payload = dict() payload['submit'] = 'true' payload['auth'] = what.authkey payload['type'] = '2' payload['title'] = book_upload.author + ' - ' + book_upload.title payload['tags'] = get_what_tags(book_upload) payload['image'] = book_upload.cover_url payload['desc'] = get_what_desc(book_upload) old_content_type = what.session.headers['Content-type'] upload_exception = None try: del what.session.headers['Content-type'] response = what.session.post(WHAT_UPLOAD_URL, data=payload, files=payload_files) if response.url == WHAT_UPLOAD_URL: try: errors = extract_upload_errors(response.text) except Exception: errors = '' exception = Exception( 'Error uploading data to what.cd. Errors: {0}'.format( '; '.join(errors))) exception.response_text = response.text raise exception except Exception as ex: upload_exception = ex finally: what.session.headers['Content-type'] = old_content_type try: new_torrent = safe_retrieve_new_torrent( what, get_info_hash_from_data(book_upload.what_torrent_file)) book_upload.what_torrent = WhatTorrent.get_or_create( request, what_id=new_torrent['torrent']['id']) book_upload.save() except Exception as ex: if upload_exception: raise upload_exception raise ex move_to_dest_add(request, book_upload) return book_upload.what_torrent
def get_or_create(bibliotik_client, torrent_id): try: return BibliotikTorrent.objects.get(id=torrent_id) except BibliotikTorrent.DoesNotExist: if not bibliotik_client: raise Exception("We do not have the BibliotikTorrent, but no client was provided.") torrent = BibliotikTorrent(id=torrent_id, retrieved=timezone.now()) torrent.import_bibliotik_data(bibliotik_client) torrent.download_torrent_file(bibliotik_client) torrent.info_hash = get_info_hash_from_data(torrent.torrent_file) torrent.save() return torrent
def get_or_create(bibliotik_client, torrent_id): try: return BibliotikTorrent.objects.get(id=torrent_id) except BibliotikTorrent.DoesNotExist: if not bibliotik_client: raise Exception('We do not have the BibliotikTorrent, but no client was provided.') torrent = BibliotikTorrent( id=torrent_id, retrieved=timezone.now(), ) torrent.import_bibliotik_data(bibliotik_client) torrent.download_torrent_file(bibliotik_client) torrent.info_hash = get_info_hash_from_data(torrent.torrent_file) torrent.save() return torrent
def mktorrent(self): print 'Creating torrent file...' torrent_temp_filename = 'temp.torrent' try: os.remove(torrent_temp_filename) except OSError: pass call_mktorrent(self.torrent_dir_path, torrent_temp_filename, settings.WHAT_ANNOUNCE, self.torrent_new_name) with open(torrent_temp_filename, 'rb') as torrent_file: self.torrent_file_new_data = pthify_torrent(torrent_file.read()) self.torrent_new_infohash = get_info_hash_from_data( self.torrent_file_new_data) print 'New info hash is: ', self.torrent_new_infohash print 'Torrent file created'
def mktorrent(self): print 'Creating torrent file...' torrent_temp_filename = 'temp.torrent' try: os.remove(torrent_temp_filename) except OSError: pass call_mktorrent(self.torrent_dir_path, torrent_temp_filename, settings.WHAT_ANNOUNCE, self.torrent_new_name) with open(torrent_temp_filename, 'rb') as torrent_file: self.torrent_file_new_data = pthify_torrent(torrent_file.read()) self.torrent_new_infohash = get_info_hash_from_data(self.torrent_file_new_data) print 'New info hash is: ', self.torrent_new_infohash print 'Torrent file created'
def get_or_create(mam_client, torrent_id): try: return MAMTorrent.objects.get(id=torrent_id) except MAMTorrent.DoesNotExist: if not mam_client: raise Exception('We do not have the MAMTorrent, but no client was provided.') torrent = MAMTorrent( id=torrent_id, retrieved=timezone.now(), ) torrent.import_mam_data(mam_client) if torrent.torrent_url is not None: torrent.download_torrent_file(mam_client) assert torrent.info_hash == get_info_hash_from_data(torrent.torrent_file) torrent.save() return torrent
def get_or_create(mam_client, torrent_id): try: return MAMTorrent.objects.get(id=torrent_id) except MAMTorrent.DoesNotExist: if not mam_client: raise Exception( 'We do not have the MAMTorrent, but no client was provided.' ) torrent = MAMTorrent( id=torrent_id, retrieved=timezone.now(), ) torrent.import_mam_data(mam_client) if torrent.torrent_url is not None: torrent.download_torrent_file(mam_client) assert torrent.info_hash == get_info_hash_from_data( torrent.torrent_file) torrent.save() return torrent