Advertisement
Luksor54

vintage-story-mod-downloader_v1.1.0

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