import time
from waybackpy import WaybackMachineSaveAPI # upload to archive.org
import logging
logger = logging.getLogger(__name__)

def upload_to_archive(article_object):
    """uploads to archive.org and returns the archived url"""
    user_agent = "Mozilla/5.0 (Windows NT 5.1; rv:40.0) Gecko/20100101 Firefox/40.0" # needed?
    url = article_object.article_url
    try:
        wayback = WaybackMachineSaveAPI(url, user_agent)
        archive_url = wayback.save()
        # logger.info(f"{url} uploaded to archive successfully")
        article_object.archive_url = archive_url

    except Exception as e:
        article_object.archive_url = "Error while uploading: {}".format(e)
        logger.error(f"Error while generating archive url: {e}")

    return article_object