steamCmdScript.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import re
  2. from subprocess import call
  3. import os
  4. import sys
  5. argv = sys.argv
  6. REGEX = "https?:\/\/steamcommunity.com\/sharedfiles\/filedetails\/\?id=[0-9]+.*" #just * for now
  7. ID_RE = "[0-9]{4,}"
  8. # Globals
  9. STEAMCMD_LOC = "./steamcmd.sh"
  10. # Functions
  11. def extract_mods_ids(FILENAME):
  12. list_of_links = []
  13. list_of_ids = set()
  14. with open(FILENAME, "r" ) as f:
  15. list_of_links = f.readlines()
  16. for link in list_of_links:
  17. # Remove \n \r
  18. link = link.rstrip()
  19. # If link is valid
  20. if(re.search(REGEX, link)):
  21. id_number = re.findall(ID_RE, link)[-1]
  22. list_of_ids.add(id_number)
  23. # set removes dublicates
  24. uniq_qty = 0
  25. list_of_ids = list(list_of_ids)
  26. for id_number in list_of_ids:#TESTING
  27. uniq_qty += 1
  28. print("unique id number: " + id_number)
  29. print(f'TOTAL unique ids: {uniq_qty}')
  30. return list_of_ids
  31. def controll_steamcmd(list_of_ids, APP_ID, DIRECTORY, STEAMCMD_LOC):
  32. # Steam CMD commandline parametres
  33. login_prm = "+login anonymous"
  34. directory_prm = f"+force_install_dir {DIRECTORY}"
  35. cmd_prm_list = [STEAMCMD_LOC, login_prm, directory_prm]
  36. downnload_cmd = f"+workshop_download_item {APP_ID}"
  37. for id_number in list_of_ids:
  38. #print("unique id number " + id_number)
  39. cmd_prm_list.append(downnload_cmd + " " + id_number)
  40. cmd_prm_list.append("+quit")
  41. print(cmd_prm_list)
  42. try:
  43. call(cmd_prm_list)
  44. except FileNotFoundError:
  45. print(f"[Errno 2] No such file or directory: '{STEAMCMD_LOC}'")
  46. print(f"Put inside the folder with the {STEAMCMD_LOC}")
  47. sys.exit(0)
  48. # Main Function
  49. def main():
  50. print(f"Put inside the folder with the {STEAMCMD_LOC}")
  51. if len(argv)!= 4:
  52. print("Usage: programm_name <path_to_a_file/filename with the mod links> <numeric_steam_app_id> <mods_destination_folder_loc>")
  53. sys.exit(0)
  54. FILENAME = argv[1]
  55. APP_ID = argv[2]
  56. DIRECTORY = argv[3]
  57. if(os.path.isfile(FILENAME) != True):
  58. print("File do not exist")
  59. print("Usage: programm_name <path_to_a_file/filename with the mod links> <numeric_steam_app_id> <mods_destination_folder_loc>")
  60. sys.exit(0)
  61. #steamcmd +login anonymous +force_install_dir ../czero
  62. list_of_ids = extract_mods_ids(FILENAME)
  63. controll_steamcmd(list_of_ids, APP_ID, DIRECTORY, STEAMCMD_LOC)
  64. try:
  65. main()
  66. except KeyboardInterrupt:
  67. print("\nInterrupted, exiting")
  68. sys.exit(0)