Advertisement
Jexal

2214b2e5-fc19-4d11-96aa-54c58a864f18

Apr 11th, 2025
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. """This script allows you to save URLs from a specified text file to Archive.ph."""
  2.  
  3. import logging
  4. import re
  5. import time
  6. import webbrowser
  7. import subprocess
  8. from os import system
  9. from colorama import Fore, Style, init
  10. from rich.traceback import install
  11.  
  12. # ----------------------- Initialization -----------------------
  13. system("title " + "Archive.ph Link Saver")
  14. init(autoreset=True)
  15. install()
  16.  
  17. # ----------------------- Config -----------------------
  18. DELAY_BETWEEN_URLS = 5  # Delay between URL submissions
  19. URL_INPUT_PATH = r"C:\Scripts\Archive.ph Link Saver\URLs.txt"
  20. MODIFIED_URLS_PATH = r"C:\Scripts\Archive.ph Link Saver\Logs\Newly Modified URLs.txt"
  21. LOG_FILE_PATH = r"C:\Scripts\Archive.ph Link Saver\Logs\Archive.ph.log"
  22.  
  23. # ----------------------- Logging Setup -----------------------
  24. logging.basicConfig(filename=LOG_FILE_PATH,
  25.                     level=logging.DEBUG,
  26.                     format='%(asctime)s - %(levelname)s - %(message)s')
  27.  
  28. logging.info("Script started")
  29.  
  30.  
  31. # ----------------------- Regex Pattern -----------------------
  32. general_url_pattern = re.compile(
  33.     r'^(https?://)?'  # optional http or https
  34.     r'(www\.)?'       # optional www.
  35.     r'([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+'  # domain name
  36.     r'[a-zA-Z]{2,}'   # TLD
  37.     r'(/[a-zA-Z0-9-._~%!$&\'()*+,;=]*)*'  # optional path
  38.     r'(\?[a-zA-Z0-9-._~%!$&\'()*+,;=]*)?'  # optional query
  39.     r'(#.*)?$'  # optional fragment
  40. )
  41.  
  42.  
  43. # ----------------------- Utility -----------------------
  44. def log_and_print(message, level="info", color=None):
  45.     getattr(logging, level)(message)
  46.     print((color or "") + message)
  47.  
  48.  
  49. # ----------------------- Main Function -----------------------
  50. def main():
  51.     invalid_urls = False
  52.     archive_base = "https://archive.ph/?run=1&url="
  53.  
  54.     try:
  55.         with open(URL_INPUT_PATH, "r") as input_file, open(MODIFIED_URLS_PATH, "w") as output_file:
  56.             count = 1
  57.             for line in input_file:
  58.                 url = line.strip()
  59.                 if not url:
  60.                     log_and_print("Skipped empty line", "info")
  61.                     continue
  62.  
  63.                 if general_url_pattern.match(url):
  64.                     new_url = f"{archive_base}{url}"
  65.                     output_file.write(new_url + "\n")
  66.                     log_and_print(f"Saved URL #{count}: {new_url}", "info")
  67.                     webbrowser.open(new_url)
  68.                     log_and_print(f"> Saving URL #{count}", color=Fore.YELLOW)
  69.                     time.sleep(DELAY_BETWEEN_URLS)
  70.                     count += 1
  71.                 else:
  72.                     log_and_print(f"> Invalid URL: {url}", level="warning", color=Fore.RED + Fore.LIGHTCYAN_EX)
  73.                     invalid_urls = True
  74.  
  75.     except FileNotFoundError as e:
  76.         log_and_print(f"Error: {e}", level="error", color=Fore.RED)
  77.         return
  78.  
  79.     if invalid_urls:
  80.         log_and_print("> Some URLs were invalid and not saved.", level="warning", color=Fore.YELLOW)
  81.         input(f"> Press {Fore.YELLOW}Enter{Style.RESET_ALL} to exit and open the {Fore.YELLOW}URLs{Style.RESET_ALL} text file...")
  82.         subprocess.Popen(["notepad.exe", URL_INPUT_PATH])
  83.     else:
  84.         log_and_print("> All URLs are being saved.", color=Fore.GREEN)
  85.  
  86.     time.sleep(1)
  87.     clean_trailing_newline(MODIFIED_URLS_PATH)
  88.     logging.info("Script completed")
  89.  
  90.  
  91. # ----------------------- Trailing Newline Cleanup -----------------------
  92. def clean_trailing_newline(path):
  93.     try:
  94.         with open(path, "r+") as file:
  95.             data = file.read().rstrip('\n')
  96.             file.seek(0)
  97.             file.write(data)
  98.             file.truncate()
  99.         logging.info("Removed empty line at the end of the output file")
  100.     except Exception as e:
  101.         log_and_print(f"Error cleaning file: {e}", level="error", color=Fore.RED)
  102.  
  103.  
  104. # ----------------------- Entrypoint -----------------------
  105. if __name__ == "__main__":
  106.     main()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement