Guest User

Untitled

a guest
Nov 18th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. # gatgher_link.py
  2. import requests as r
  3. from bs4 import BeautifulSoup
  4.  
  5. def preprocess_article(article : str):
  6. lines = article.splitlines()
  7. return [lines[1], lines[5]]
  8.  
  9. def get_html(link):
  10. """
  11. return links to websites without img, potentially mp4
  12. """
  13. html=r.get(link).text
  14. soup = BeautifulSoup(html, 'html.parser')
  15. photos = soup.find_all('div', class_ = 'card-list__items')
  16. soup = BeautifulSoup(str(photos), 'html.parser')
  17. articles = soup.find_all('article')
  18. articles = [preprocess_article(str(article)) for article in articles]
  19. articles = [
  20. f"https://coomer.su{article[0][9:47]}"
  21. for article in articles
  22. if article[1].find("img") == -1
  23. ]
  24.  
  25. return articles
  26.  
  27. def main():
  28. link = "https://{hehe}.com?o="
  29.  
  30. links = [
  31. link + str(50*i)
  32. for i in range(0, 32)
  33. ]
  34. sublinks = []
  35. for link in links:
  36. sublinks = sublinks + get_html(link)
  37. with open("links.txt", "w+", encoding="utf-8") as f:
  38. f.writelines(sublinks)
  39.  
  40. if __name__ == "__main__":
  41. main()
  42.  
  43. #preprocess links
  44. import requests as r
  45. from bs4 import BeautifulSoup
  46. import threading
  47.  
  48.  
  49. def preprocess_article(article: str):
  50. lines = article.splitlines()
  51. return [lines[1], lines[5]]
  52.  
  53.  
  54. sublinks = []
  55. faulty = []
  56.  
  57.  
  58. def get_download_link(link):
  59. """
  60. return links to websites without img, potentially mp4
  61. """
  62. html = r.get(link).text
  63. soup = BeautifulSoup(html, "html.parser")
  64. photos = soup.find("a", class_="post__attachment-link")
  65. if photos == -1:
  66. faulty.append(link)
  67. return
  68. photos = str(photos)
  69. begin = photos.find("href")
  70. photos = photos[begin + 6 :]
  71. end = photos.find('"')
  72. photos = photos[:end]
  73. link = photos
  74. sublinks.append(link)
  75. print(len(sublinks))
  76.  
  77.  
  78. def download_links(batch):
  79. for link in batch:
  80. try:
  81. get_download_link(link)
  82. except Exception:
  83. print(link)
  84.  
  85.  
  86. def write_lines(filename, lines):
  87. with open(filename, "w+") as f:
  88. for line in lines:
  89. print(line)
  90. f.write(line)
  91. f.write("\n")
  92.  
  93. def main():
  94. with open("links.txt", "r", encoding="utf-8") as f:
  95. links = [link.strip() for link in f.readlines()]
  96.  
  97. batch_size = 50
  98. threads = []
  99. for i in range(0, int(len(links) / batch_size) + 1):
  100. begin = i * batch_size
  101. end = (i + 1) * batch_size - 1
  102. if end >= len(links):
  103. end = len(links) - 1
  104. batch = links[begin:end]
  105. threads.append(threading.Thread(target=download_links, args=(batch,)))
  106. break
  107. batch = links[begin:end]
  108. threads.append(threading.Thread(target=download_links, args=(batch,)))
  109. for t in threads:
  110. t.start()
  111. for t in threads:
  112. t.join()
  113.  
  114. write_lines("video_links.txt", sublinks)
  115. write_lines("fault_video_links.txt", faulty)
  116.  
  117.  
  118. if __name__ == "__main__":
  119. main()
  120.  
  121. #download links
  122. import threading
  123. import os
  124. import uuid
  125. import requests
  126.  
  127. with open("video_links.txt", "r") as f:
  128. links = [
  129. line.strip()
  130. for line in f.readlines()
  131. ]
  132.  
  133. t = []
  134. def download_video(url, directory="videos"):
  135. # Ensure the directory exists
  136. os.makedirs(directory, exist_ok=True)
  137.  
  138. # Generate a unique file name
  139. unique_name = f"{uuid.uuid4()}.mp4"
  140. file_path = os.path.join(directory, unique_name)
  141.  
  142. try:
  143. # Download the video
  144. response = requests.get(url, stream=True)
  145. response.raise_for_status() # Raise an error for HTTP issues
  146.  
  147. # Write to the file
  148. with open(file_path, "wb") as f:
  149. for chunk in response.iter_content(chunk_size=1024):
  150. if chunk: # Filter out keep-alive new chunks
  151. f.write(chunk)
  152.  
  153. print(f"Video downloaded successfully: {file_path}")
  154. return file_path
  155. except requests.RequestException as e:
  156. print(f"Failed to download the video: {e}")
  157. return None
  158.  
  159. def download_links(links):
  160. for link in links:
  161. download_video(link)
  162.  
  163. threads = []
  164. batch_size = 10
  165. for i in range(0, int(len(links) / batch_size) + 1):
  166. begin = i * batch_size
  167. end = (i + 1) * batch_size - 1
  168. if end >= len(links):
  169. end = len(links) - 1
  170. batch = links[begin:end]
  171. threads.append(threading.Thread(target=download_links, args=(batch,)))
  172. break
  173. batch = links[begin:end]
  174. threads.append(threading.Thread(target=download_links, args=(batch,)))
  175. for t in threads:
  176. t.start()
  177. for t in threads:
  178. t.join()
  179.  
Advertisement
Add Comment
Please, Sign In to add comment