Ikewise

ariados_1.0.py

Nov 27th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. ######################################################
  2. #          ARIADOS v1.0  Yuku Forum Backup           #
  3. # Because they ruined our forum and we want it back. #
  4. ######################################################
  5.  
  6. import os
  7. import time
  8. import lxml
  9. import requests
  10. from bs4 import BeautifulSoup
  11.  
  12. # Forum URL
  13. boardURL = "http://trsrockin.fr.yuku.com"
  14.  
  15. # Backup save location
  16. #   Backup will be saved in the following folder structure:
  17. #   Main Forum Name > Subforum Name > Thread Name > Page.htm
  18. saveDir = "C:\\"
  19.  
  20. # ARIADOS will attempt to back up all threads within the following range
  21. threadFirst = 1
  22. threadLast = 6543
  23.  
  24. # Specify time delay between server requests
  25. timeDelay = 2
  26.  
  27.  
  28.  
  29. # MAIN PROGRAM
  30. print("--------------------------------")
  31. print("ARIADOS v1.0 - Yuku Forum Backup")
  32. print("--------------------------------")
  33. print(" ")
  34. print("ARIADOS will attempt to back up threads " + str(threadFirst) + " through " + str(threadLast))
  35. print(" ")
  36. print("--------------------------------")
  37. print(" ")
  38.  
  39. iT = threadFirst # Thread iterator
  40.  
  41. while iT <= threadLast:
  42.     iP = 1 # Page iterator
  43.  
  44.     time.sleep(timeDelay)
  45.  
  46.     # Get first page of thread
  47.     soup = BeautifulSoup(requests.get(boardURL + "/topic/" + str(iT)).text, "lxml")
  48.  
  49.     # Only process the page if it isn't a 404
  50.     if soup.title.string != "We are going to be back soon":
  51.         boardIndex = soup.select(".breadcrumb > li:nth-of-type(1) > a")[0]["title"]
  52.         currentForum = soup.select(".breadcrumb > li:nth-of-type(2) > a")[0]["title"]
  53.         currentThread = soup.select(".breadcrumb > li:nth-of-type(3) > a")[0]["title"]
  54.  
  55.         savePath = saveDir + boardIndex + "\\" + currentForum + "\\" + currentThread
  56.        
  57.         # Check for folders and make them if missing
  58.         if os.path.isdir(saveDir + boardIndex) != 1:
  59.             os.mkdir(saveDir + boardIndex)
  60.         if os.path.isdir(saveDir + boardIndex + "\\" + currentForum) != 1:
  61.             os.mkdir(saveDir + boardIndex + "\\" + currentForum)
  62.         if os.path.isdir(saveDir + boardIndex + "\\" + currentForum + "\\" + currentThread) != 1:
  63.             os.mkdir(saveDir + boardIndex + "\\" + currentForum + "\\" + currentThread)
  64.  
  65.         # Check whether page exists and if not, save it
  66.         if os.path.isfile(savePath + "\\" + str(iP) + ".htm") != 1:
  67.             print("Accessing thread: " + currentThread)
  68.             with open(savePath + "\\" + str(iP) + ".htm", "x", encoding='utf8') as file:
  69.                 file.write(soup.prettify())
  70.        
  71.         # Check number of pages in thread
  72.         if len(soup.select(".pagination")) > 0:
  73.             threadEnd = int(soup.select("div.pagination ul li a")[len(soup.select("div.pagination ul li a"))-2].string)
  74.  
  75.             # Loop through thread pages
  76.             while iP < threadEnd:
  77.                 iP += 1
  78.  
  79.                 time.sleep(timeDelay)
  80.  
  81.                 # Get next page of thread
  82.                 soup = BeautifulSoup(requests.get(boardURL + "/topic/" + str(iT) + "/?page=" + str(iP)).text, "html.parser")
  83.  
  84.                 # Check whether page exists and if not, save it
  85.                 if os.path.isfile(savePath + "\\" + str(iP) + ".htm") != 1:
  86.                     print("     Saving page " + str(iP) + " of " + str(threadEnd))
  87.                     with open(savePath + "\\" + str(iP) + ".htm", "x", encoding='utf8') as file:
  88.                         file.write(soup.prettify())
  89.  
  90.         print("Thread saved at " + savePath)
  91.         print(" ")
  92.         with open(saveDir + boardIndex + "\\" + "progress.txt", "a") as file:
  93.                 file.write(str(iT) + "\r\n")
  94.  
  95.     iT += 1
  96.  
  97. print("Backup operation completed.")
Advertisement
Add Comment
Please, Sign In to add comment