Advertisement
Luksor54

vintage-story-mod-downloader_v1.0.2

Apr 8th, 2022 (edited)
120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None
  1. import requests
  2. import re
  3. import string
  4. import os
  5. import wget_ as wget
  6. from bs4 import BeautifulSoup as bs
  7.  
  8. home_url = "https://mods.vintagestory.at"
  9. modlist_url = "https://mods.vintagestory.at/list/mod"
  10.  
  11. req_modname=requests.get(modlist_url,'html.parser')
  12. search_active = False
  13. curr_path=os.path.dirname(os.path.abspath(__file__))
  14.  
  15. if not os.path.isfile(".\\modsdir.txt"):
  16.     f=open(".\\modsdir.txt", "w+")    
  17.     mods_dir=input('What is your mod directory?')
  18.     f.write(mods_dir)
  19.     f.close
  20.  
  21. f=open(".\\modsdir.txt", "r")
  22.  
  23. if f.read()=='':
  24.     f.close
  25.     f=open(".\\modsdir.txt", "w")
  26.     mods_dir=input('What is your mod directory?')
  27.     f.write(mods_dir)
  28.     f.close
  29.  
  30. f.close
  31.    
  32. f=open(".\\modsdir.txt", "r")
  33. modpath=f.read()
  34. f.close
  35.  
  36. path_out='.\\output'
  37. if not os.path.isdir(path_out):
  38.     os.mkdir(path_out)
  39.     print("Creating output directory")
  40.  
  41.  
  42. modlist = os.listdir(modpath)
  43. mods=[]
  44.  
  45. search=input("\nDo you wanna search mods by hand? (y or n): ")
  46.  
  47. if search.lower() not in ['y','yes']:
  48.     for mod in modlist:
  49.         if mod[-4:-1]+mod[-1] == '.zip':
  50.             upper=[]
  51.             for j,mm in enumerate(mod):
  52.                 if mm.isupper():
  53.                     upper.append(j)
  54.             modx=mod.split(' ')[0]
  55.             if 5 in upper or 6 in upper:
  56.                 m=mod[0:-4]
  57.                 m=m[0:9]
  58.                 mods.append([m,mod[0:-4]])
  59.                 modx=''
  60.                 mody=''
  61.             else:
  62.                 m=mod[0:-4]
  63.                 m=m[0:6]
  64.                 if 'xlib' in m:
  65.                     m='xlib'  
  66.                 mods.append([m,mod[0:-4]])
  67. else:
  68.     search_active = True
  69.  
  70. if search_active:
  71.     for i in range(0,1000):
  72.         mods.append(['',''])
  73.  
  74. for m,full_mod in mods:
  75.     break_ = False
  76.     mod_name=m
  77.     if search_active:
  78.         mod_name=input('\n\nSearch for mod name (0 for quit): ')
  79.         full_mod=''
  80.         if mod_name=='0':
  81.             quit()
  82.    
  83.     mod_name=mod_name.strip(" _-.'!0")
  84.     regex=r''
  85.     for m in mod_name:
  86.         regex=regex+'[\_\-\.\'\!\s]*'+m
  87.     regex=regex+'[\_\-\.\'\!\s]*'
  88.     #print(regex)
  89.    
  90.     html_modname=req_modname.text
  91.     html_modname=html_modname.strip(" _-.'!")
  92.     p_htmlmodname=bs(html_modname)
  93.     #print(p_htmlmodname)
  94.  
  95.     mod_urls=p_htmlmodname.find_all('strong',text=re.compile(".*({}).*".format(regex),re.IGNORECASE))
  96.     if len(mod_urls) == 0:
  97.         break_ = True
  98.         print("\n\nNo results for {0}\n".format(mod_name))
  99.     elif len(mod_urls)==1:
  100.         mod_urls.append('x')
  101.  
  102.     #os.system('cls')
  103.        
  104.     if not break_:
  105.  
  106.         if mod_urls[-1]!='x':
  107.             print("\n\nFound more than one mod for search {0}".format(mod_name))
  108.             if full_mod:
  109.                 print("Full mod name: {0}\n".format(full_mod))
  110.             for i,n in enumerate(mod_urls):
  111.                 print(str(i+1)+'. '+str(n)[8:-9])
  112.             x=int(input("\nWhich do you want to download? (0 for pass): "))-1
  113.             full_mod=str(mod_urls[x])[8:-9]
  114.             if x==-1:
  115.                continue
  116.             mod_url=mod_urls[x].parent['href']
  117.         else:
  118.             mod_url=mod_urls[0].parent['href']
  119.  
  120.         url=home_url+mod_url
  121.  
  122.         req_filelist = requests.get(url, 'html.parser')
  123.         html_filelist=req_filelist.text
  124.         p_htmlfilelist= bs(html_filelist)
  125.  
  126.         latest=p_htmlfilelist.body.find('tr', attrs={"latest"})
  127.         fileid=latest.find('a', {'href':re.compile(r'/download\?fileid.')})['href']
  128.         downlink=home_url+fileid
  129.  
  130.         #os.system('cls')
  131.         print('\n\n'+full_mod)
  132.         print(url)
  133.         print(downlink)
  134.         response=wget.download(downlink, out=path_out)
  135.     else:
  136.         break_=False
  137.  
  138. c=input("\n\nDownload complete press any key to exit\n")
Advertisement
RAW Paste Data Copied
Advertisement