parameters: (link_guid)
docstring: Call synchronously from the Django shell with the invocation:
      >>> upload_to_internet_archive.apply(kwargs={"link_guid": 'AAAA-AAAA'})
docstring_summary: Call synchronously from the Django shell with the invocation:
      >>> upload_to_internet_archive.apply(kwargs={"link_guid": 'AAAA-AAAA'})
docstring_tokens: Call synchronously from the Django shell with the invocation : >>> upload_to_internet_archive . apply ( kwargs = { link_guid : AAAA - AAAA } )
function: def upload_to_internet_archive(link_guid):
      """
      Call synchronously from the Django shell with the invocation:
      >>> upload_to_internet_archive.apply(kwargs={"link_guid": 'AAAA-AAAA'})
      """
  
      if not settings.UPLOAD_TO_INTERNET_ARCHIVE:
          return
  
      link = Link.objects.get(guid=link_guid)
      if not link.can_upload_to_internet_archive():
          logger.info(f"Queued Link {link_guid} no longer eligible for upload.")
          return
  
      url = remove_control_characters(link.submitted_url)
      metadata = {
          "collection": settings.INTERNET_ARCHIVE_COLLECTION,
          "title": f"{link_guid}: {truncatechars(link.submitted_title, 50)}",
          "mediatype": "web",
          "description": f"Perma.cc archive of {url} created on {link.creation_timestamp}.",
          "contributor": "Perma.cc",
          "submitted_url": url,
          "perma_url": protocol() + settings.HOST + reverse('single_permalink', args=[link.guid]),
          "external-identifier": f"urn:X-perma:{link_guid}",
      }
  
      temp_warc_file = tempfile.TemporaryFile()
      try:
          item = internetarchive.get_item(link.ia_identifier)
          if item.exists:
              if not item.metadata.get('title') or item.metadata['title'] == 'Removed':
                  # if item already exists (but has been removed),
                  # ia won't update its metadata when we attempt to re-upload:
                  # we have to explicitly modify the metadata, then upload.
                  logger.info(f"Link {link_guid} previously removed from IA: updating metadata")
                  item.modify_metadata(
                      metadata,
                      access_key=settings.INTERNET_ARCHIVE_ACCESS_KEY,
                      secret_key=settings.INTERNET_ARCHIVE_SECRET_KEY,
                  )
              else:
                  logger.info(f"Link {link_guid} was already uploaded to IA: skipping.")
                  return
  
          # copy warc to local disk storage for upload
          with default_storage.open(link.warc_storage_file()) as warc_file:
              copy_file_data(warc_file, temp_warc_file)
              temp_warc_file.seek(0)
  
          logger.info(f"Uploading Link {link_guid} to IA.")
          warc_name = os.path.basename(link.warc_storage_file())
          response_list = internetarchive.upload(
              link.ia_identifier,
              {warc_name: temp_warc_file},
              metadata=metadata,
              access_key=settings.INTERNET_ARCHIVE_ACCESS_KEY,
              secret_key=settings.INTERNET_ARCHIVE_SECRET_KEY,
              retries=2,
              retries_sleep=5,
              verbose=True,
          )
          response_list[0].raise_for_status()
          link.internet_archive_upload_status = 'completed'
      except Exception:
          logger.exception(f"Exception while uploading Link {link.guid} to IA:")
          link.internet_archive_upload_status = 'failed'
      finally:
          temp_warc_file.close()
          link.save(update_fields=['internet_archive_upload_status'])
function_tokens: def upload_to_internet_archive ( link_guid ) : if not settings . UPLOAD_TO_INTERNET_ARCHIVE : return link = Link . objects . get ( guid = link_guid ) if not link . can_upload_to_internet_archive ( ) : logger . info ( f"Queued Link {link_guid} no longer eligible for upload." ) return url = remove_control_characters ( link . submitted_url ) metadata = { "collection" : settings . INTERNET_ARCHIVE_COLLECTION , "title" : f"{link_guid}: {truncatechars(link.submitted_title, 50)}" , "mediatype" : "web" , "description" : f"Perma.cc archive of {url} created on {link.creation_timestamp}." , "contributor" : "Perma.cc" , "submitted_url" : url , "perma_url" : protocol ( ) + settings . HOST + reverse ( 'single_permalink' , args = [ link . guid ] ) , "external-identifier" : f"urn:X-perma:{link_guid}" , } temp_warc_file = tempfile . TemporaryFile ( ) try : item = internetarchive . get_item ( link . ia_identifier ) if item . exists : if not item . metadata . get ( 'title' ) or item . metadata [ 'title' ] == 'Removed' : # if item already exists (but has been removed), # ia won't update its metadata when we attempt to re-upload: # we have to explicitly modify the metadata, then upload. logger . info ( f"Link {link_guid} previously removed from IA: updating metadata" ) item . modify_metadata ( metadata , access_key = settings . INTERNET_ARCHIVE_ACCESS_KEY , secret_key = settings . INTERNET_ARCHIVE_SECRET_KEY , ) else : logger . info ( f"Link {link_guid} was already uploaded to IA: skipping." ) return # copy warc to local disk storage for upload with default_storage . open ( link . warc_storage_file ( ) ) as warc_file : copy_file_data ( warc_file , temp_warc_file ) temp_warc_file . seek ( 0 ) logger . info ( f"Uploading Link {link_guid} to IA." ) warc_name = os . path . basename ( link . warc_storage_file ( ) ) response_list = internetarchive . upload ( link . ia_identifier , { warc_name : temp_warc_file } , metadata = metadata , access_key = settings . INTERNET_ARCHIVE_ACCESS_KEY , secret_key = settings . INTERNET_ARCHIVE_SECRET_KEY , retries = 2 , retries_sleep = 5 , verbose = True , ) response_list [ 0 ] . raise_for_status ( ) link . internet_archive_upload_status = 'completed' except Exception : logger . exception ( f"Exception while uploading Link {link.guid} to IA:" ) link . internet_archive_upload_status = 'failed' finally : temp_warc_file . close ( ) link . save ( update_fields = [ 'internet_archive_upload_status' ] )