3
3
import zipfile
4
4
import requests
5
5
6
+
6
7
# Constants
7
8
datapack_prefix = "purpurpack_"
8
9
datapack_folder = "packs"
13
14
print ("The required environment variable MODRINTH_TOKEN is not set." )
14
15
exit (1 )
15
16
16
- def fetch_modrinth_info (project_id ):
17
+ def fetch_versions_json (project_id ):
17
18
"""Fetches information about available versions of a project from Modrinth."""
18
19
url = f"https://api.modrinth.com/v2/project/{ project_id } /version"
19
20
headers = {
@@ -26,70 +27,85 @@ def fetch_modrinth_info(project_id):
26
27
print (f"Failed to fetch info for project ID: { project_id } (HTTP { response .status_code } )" )
27
28
return None
28
29
29
- def get_latest_version ( versions ):
30
- """Extracts the latest version number from a JSON array of version data."""
30
+ def get_latest_modrinth_version ( project_id ):
31
+ versions = fetch_versions_json ( project_id )
31
32
for version_data in versions :
32
33
if "datapack" in version_data .get ("loaders" , []):
33
34
return version_data .get ("version_number" )
34
35
return None
35
36
36
- def main ():
37
- for datapack_path in os .listdir (datapack_folder ):
38
- full_datapack_path = os .path .join (datapack_folder , datapack_path )
39
- modrinth_file_path = os .path .join (full_datapack_path , "modrinth.json" )
40
-
41
- # Continue if not a directory
42
- if not os .path .isdir (full_datapack_path ):
43
- continue
44
-
45
- # Continue if datapack folder does not include modrinth.json
46
- if not os .path .exists (modrinth_file_path ):
47
- print (f"No modrinth.json file located in { full_datapack_path } " )
48
- continue
37
+ def get_datapack_info (modrinth_json_path ):
38
+ with open (modrinth_json_path , "r" ) as modrinth_file :
39
+ file_info = json .load (modrinth_file )
40
+ return file_info
41
+
42
+ def get_current_version (json_file ):
43
+ return json_file ["version_number" ]
44
+
45
+ def get_project_id (json_file ):
46
+ return json_file ["project_id" ]
47
+
48
+ def should_update_pack (modrinth_json ):
49
+ if modrinth_json is None :
50
+ print ("Modrinth JSON is empty or missing, skipping" )
51
+ return False
52
+ project_id = get_project_id (modrinth_json )
53
+ current_version = get_current_version (modrinth_json )
54
+ if current_version is None :
55
+ print ("Pack does not have a version number, skipping" )
56
+ return False
57
+ if project_id is None :
58
+ print ("Pack does not have a project ID, skipping" )
59
+ return False
60
+ latest_version = get_latest_modrinth_version (project_id )
61
+ if latest_version >= current_version :
62
+ print ("Pack does not need to be updated, skipping" )
63
+ return False
64
+ if latest_version is None :
65
+ return True
66
+ if latest_version < current_version :
67
+ return True
68
+
69
+ def zip_and_post (modrinth_info , root ):
70
+ version = get_current_version (modrinth_info )
71
+ project_id = get_project_id (modrinth_info )
72
+ relative_path = os .path .relpath (root , datapack_folder )
73
+ datapack_name = f"{ relative_path .replace (os .sep , '_' )} _v{ version } .zip"
74
+ full_datapack_path = os .path .join (root , datapack_name )
75
+ dist_dir = os .path .join (full_datapack_path , "dist" )
76
+ os .makedirs (dist_dir , exist_ok = True )
77
+ zipped_file_path = os .path .join (dist_dir , datapack_name )
78
+ with zipfile .ZipFile (zipped_file_path , "w" , zipfile .ZIP_DEFLATED ) as zipped_file :
79
+ for root , dirs , files in os .walk (full_datapack_path ):
80
+ for file in files :
81
+ if file == 'modrinth.json' :
82
+ continue
83
+ zipped_file .write (os .path .join (root , file ), arcname = file )
84
+ print (f"zipping { file } " )
85
+
86
+ files = {
87
+ 'data' : (None , json .dumps (modrinth_info ), 'application/json' ),
88
+ 'file' : (datapack_name , open (zipped_file_path , 'rb' ), 'application/zip' )
89
+ }
90
+ response = requests .post (modrinth_post_version_route , headers = {"Authorization" : modrinth_token }, files = files )
91
+ upload_response = response .json ()
49
92
50
- print (f"Processing { datapack_path } ..." )
93
+ if 'error' not in upload_response :
94
+ print (f"Successfully uploaded version { version } for { project_id } ! Output:\n { upload_response } " )
95
+ else :
96
+ print (f"Failed to upload { datapack_name } . Output:\n { upload_response } " )
51
97
52
- with open ('modrinth.json' ) as base_file , open (modrinth_file_path ) as datapack_file :
53
- base_json = json .load (base_file )
54
- datapack_json = json .load (datapack_file )
55
- modrinth_json = {** base_json , ** datapack_json }
56
98
57
- project_id = modrinth_json ["project_id" ]
58
- current_version = modrinth_json ["version_number" ]
59
99
60
- versions = fetch_modrinth_info (project_id )
61
- if versions is None :
100
+ def main ():
101
+ for root , dirs , files in os .walk (datapack_folder ):
102
+ if 'pack.mcmeta' not in files :
103
+ print (f"{ root } is not a pack directory, skipping" )
62
104
continue
63
-
64
- latest_version = get_latest_version (versions )
65
- if latest_version is None or latest_version == current_version :
66
- print (f"No new version available for project { project_id } . Skipping..." )
105
+ if 'modrinth.json' not in files :
106
+ print (f"{ root } does not have a modrinth json file, skipping" )
67
107
continue
68
-
69
- zipped_file_name = f"{ datapack_prefix } { datapack_path } _{ current_version } .zip"
70
- dist_dir = os .path .join (full_datapack_path , "dist" )
71
- os .makedirs (dist_dir , exist_ok = True )
72
- zipped_file_path = os .path .join (dist_dir , zipped_file_name )
73
-
74
- with zipfile .ZipFile (zipped_file_path , 'w' , zipfile .ZIP_DEFLATED ) as zipf :
75
- for root , _ , files in os .walk (full_datapack_path ):
76
- for file in files :
77
- if file != 'modrinth.json' and 'dist' not in root :
78
- zipf .write (os .path .join (root , file ), os .path .relpath (os .path .join (root , file ), full_datapack_path ))
79
-
80
- print (f"Datapack located at: { zipped_file_path } " )
81
-
82
- files = {
83
- 'data' : (None , json .dumps (modrinth_json ), 'application/json' ),
84
- 'file' : (zipped_file_name , open (zipped_file_path , 'rb' ), 'application/zip' )
85
- }
86
- response = requests .post (modrinth_post_version_route , headers = {"Authorization" : modrinth_token }, files = files )
87
- upload_response = response .json ()
88
-
89
- if 'error' not in upload_response :
90
- print (f"Successfully uploaded version { current_version } for { project_id } ! Output:\n { upload_response } " )
91
- else :
92
- print (f"Failed to upload { datapack_path } . Output:\n { upload_response } " )
93
-
94
- if __name__ == "__main__" :
95
- main ()
108
+ datapack_info = get_datapack_info (os .path .join (root , "modrinth.json" ))
109
+ if not should_update_pack (datapack_info ):
110
+ continue
111
+ zip_and_post (datapack_info , root )
0 commit comments