Advertisement
Guest User

Untitled

a guest
Sep 28th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import argparse
  4. import bs4
  5. import os
  6. import re
  7. import requests
  8. import urllib.parse
  9.  
  10. parser = argparse.ArgumentParser()
  11. parser.add_argument("-s", "--search", help="Search terms")
  12. parser.add_argument("-d", "--download", help="Link to emuparadise download")
  13. args = parser.parse_args()
  14.  
  15.  
  16. def search_game():
  17.  
  18.     relative_path = os.path.join(os.path.dirname(__file__), "emu-db")
  19.     xdg_path = os.path.join(os.path.expanduser("~"), ".local", "share", "emu-db")
  20.  
  21.     if not os.path.exists(xdg_path):
  22.         database = relative_path
  23.     else:
  24.         database = xdg_path
  25.  
  26.     with open(database, encoding="utf-8") as db:
  27.         game_links = db.readlines()
  28.     db.close()
  29.  
  30.     keywords = args.search.lower().split(" ")
  31.     for game in game_links:
  32.         if all(key in game.lower() for key in keywords):
  33.             print("\"%s\"" % game.strip("\n"))
  34.  
  35.  
  36. def download_game(link):
  37.  
  38.     game = requests.get(link, headers={"referer": args.download}, stream=True)
  39.     decoded_url = urllib.parse.unquote(game.url)
  40.     filename = decoded_url.split("/")[-1]
  41.  
  42.     if os.path.exists(filename):
  43.         print("File already exists. Not downloading.")
  44.         return False
  45.  
  46.     current_size = 0
  47.     total_size = int(game.headers.get("content-length"))
  48.  
  49.     print(filename)
  50.     with open(filename, "wb") as game_file:
  51.         for block in game.iter_content(1048576):
  52.             game_file.write(block)
  53.             current_size += len(block)
  54.             percent = int((current_size / total_size * 100))
  55.             print("\r%dMiB / %dMiB %d%%" % ((current_size >> 20), (total_size >> 20), percent), end="")
  56.     game_file.close()
  57.     print("")
  58.  
  59.  
  60. def get_dc_links():
  61.  
  62.     filenames = []
  63.  
  64.     html = requests.get(args.download).text
  65.     soup = bs4.BeautifulSoup(html, "html.parser")
  66.  
  67.     dl_div = soup.find("div", class_="download-link")
  68.  
  69.     for dl_p in dl_div.find_all("p"):
  70.  
  71.         game_title = dl_p.a.get("title")
  72.         filename = re.search("Download (.+?) ISO", game_title)
  73.         filename = filename.group(1)
  74.         filenames.append(filename)
  75.  
  76.     return filenames
  77.  
  78.  
  79. def format_link():
  80.  
  81.     if "Sega_Dreamcast_ISOs" in args.download:
  82.         filenames = get_dc_links()
  83.         for filename in filenames:
  84.             link = "http://50.7.92.186/happyxhJ1ACmlTrxJQpol71nBc/Dreamcast/" + filename
  85.             download_game(link)
  86.     else:
  87.         gid = args.download.split("/")[-1]
  88.         link = "https://www.emuparadise.me/roms/get-download.php?gid=" + gid + "&test=true"
  89.         download_game(link)
  90.  
  91.  
  92. if __name__ == "__main__":
  93.  
  94.     if args.search:
  95.         search_game()
  96.  
  97.     elif args.download:
  98.         format_link()
  99.  
  100.     else:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement