2024-09-19 00:31:01 +00:00
from os import path , remove
2024-09-18 02:31:30 +00:00
from requests import request
2024-09-18 04:01:58 +00:00
from ast import literal_eval as litEval
2024-09-19 03:55:09 +00:00
from time import sleep
2024-09-18 02:31:30 +00:00
# Try to load cache
print ( " [[ Info ]] Trying to load installed versions from cache... " )
knownVersions = { }
try :
from cache import knownVersions
2024-09-18 21:57:57 +00:00
2024-09-18 02:31:30 +00:00
print ( f " [[ Info ]] Loaded { len ( knownVersions ) } installed versions from cache... " )
except ImportError :
# Fallback to just a warning
2024-09-18 21:57:57 +00:00
print (
""" || Warn || Failed to load versions from cache
| | Warn | | If this is not your first time running this script , it ' s recommended you investigate why. " " "
)
2024-09-18 02:31:30 +00:00
2024-09-18 21:57:57 +00:00
hangarPlugins : dict [ str , dict [ str , str ] ] = {
2024-09-19 00:31:01 +00:00
# "slug-of-plugin": {"Channel": "channel-to-pull", "Version": 'version-to-pull or "latest"'}
" ViaBackwards " : { " Channel " : " Snapshot " , " Version " : " latest " } ,
2024-09-18 02:31:30 +00:00
}
2024-09-18 21:57:57 +00:00
spigotPlugins : dict [ str , dict [ str , str ] ] = {
2024-09-19 00:31:01 +00:00
# "resource-id-of-plugin": {"Version": 'version-id-to-pull or "latest"', "Name": "Friendly name used for local downloads of the plugin"}
" 96927 " : { " Version " : " latest " , " Name " : " DecentHolograms " } ,
2024-09-18 02:31:30 +00:00
}
2024-09-19 00:31:01 +00:00
# MOTE: I *want* to add modrinth here, but I can't figure out how to get the latest version of a project
# Also it'd be a pain to have *more* checks because modrinth also serves client-side mods
2024-09-18 02:31:30 +00:00
geyser = True
floodgate = True
if hangarPlugins :
2024-09-18 21:57:57 +00:00
print ( " [[ Info ]] Checking for updates in plugins from hangar " )
2024-09-18 02:31:30 +00:00
for plugin in hangarPlugins :
2024-09-18 21:57:57 +00:00
print ( f " [[ Info ]] Checking for updates for plugin { plugin } " )
2024-09-18 04:01:58 +00:00
version = hangarPlugins [ plugin ] [ " Version " ]
if version == " latest " :
2024-09-18 21:57:57 +00:00
version = request (
" GET " ,
f " https://hangar.papermc.io/api/v1/projects/ { plugin } /latest?channel= { hangarPlugins [ plugin ] [ ' Channel ' ] } " ,
) . text
print (
2024-09-19 00:31:01 +00:00
f " ?? DBUG ?? Latest version of { plugin } detected to be { version } , currently installed is { knownVersions [ plugin ] if knownVersions . get ( plugin , False ) else ' N/A ' } "
2024-09-18 21:57:57 +00:00
)
2024-09-18 04:01:58 +00:00
if not knownVersions . get ( plugin , " " ) or knownVersions [ plugin ] != version :
# download update
2024-09-19 00:31:01 +00:00
print ( f " [[ Info ]] Updating plugin { plugin } " )
2024-09-19 02:08:38 +00:00
with open ( f " plugins/ { plugin } -hangar- { version } .jar " , " wb " ) as f :
2024-09-19 00:31:01 +00:00
f . write (
request (
" GET " ,
f " https://hangar.papermc.io/api/v1/projects/ { plugin } /versions/ { version } /PAPER/download " ,
) . content
)
if knownVersions . get ( plugin , " " ) and path . exists (
2024-09-19 02:08:38 +00:00
f " plugins/ { plugin } -hangar- { knownVersions . get ( plugin , ' ' ) } .jar "
2024-09-19 00:31:01 +00:00
) :
2024-09-19 02:08:38 +00:00
remove ( f " plugins/ { plugin } -hangar- { knownVersions . get ( plugin , ' ' ) } .jar " )
2024-09-18 21:57:57 +00:00
print (
f " [[ Info ]] Updated plugin { plugin } from { knownVersions [ plugin ] if knownVersions . get ( plugin , False ) else ' N/A ' } to { version } "
)
2024-09-18 04:01:58 +00:00
knownVersions [ plugin ] = version
2024-09-18 21:57:57 +00:00
else :
print ( f " [[ Info ]] No updates available for { plugin } . " )
2024-09-18 02:31:30 +00:00
if spigotPlugins :
2024-09-18 21:57:57 +00:00
print ( " [[ Info ]] Checking for updates in plugins from spigot " )
2024-09-18 02:31:30 +00:00
for plugin in spigotPlugins :
2024-09-18 21:57:57 +00:00
pluginName = spigotPlugins [ plugin ] [ " Name " ]
print ( f " [[ Info ]] Checking for updates for plugin { pluginName } " )
2024-09-18 02:31:30 +00:00
version = spigotPlugins [ plugin ] [ " Version " ]
2024-09-18 04:01:58 +00:00
if version == " latest " :
2024-09-18 21:57:57 +00:00
version = litEval (
request (
" GET " ,
f " https://api.spiget.org/v2/resources/ { plugin } /versions/latest " ,
) . text
) [ " id " ]
print (
f " ?? DBUG ?? Latest version of { pluginName } ( { plugin } ) detected to be { version } , currently installed is { knownVersions [ plugin ] if knownVersions . get ( plugin , False ) else ' N/A ' } "
)
2024-09-18 02:31:30 +00:00
if not knownVersions . get ( plugin , " " ) or knownVersions [ plugin ] != version :
2024-09-19 18:10:45 +00:00
r = request (
" GET " ,
f " https://api.spiget.org/v2/resources/ { plugin } /versions/ { version } /download/proxy " ,
allow_redirects = True ,
)
while r . status_code == 429 :
print ( " || Warn || Spiget has ratelimited me, waiting 1s " )
sleep ( 1 )
2024-09-19 03:55:09 +00:00
r = request (
" GET " ,
f " https://api.spiget.org/v2/resources/ { plugin } /versions/ { version } /download/proxy " ,
allow_redirects = True ,
)
2024-09-19 18:10:45 +00:00
if r . content == b " " :
print (
f """
2024-09-19 03:55:09 +00:00
! ! Notice ! ! For whatever reason , { pluginName } is not available through the download proxy API .
! ! Notice ! ! The plugin download should be at https : / / api . spiget . org / v2 / resources / { plugin } / versions / { version } / download
! ! Notice ! ! I apologize for not being able to get this plugin on my own , but there ' s nothing I can do here.
! ! Notice ! ! I will mark this plugin ' s version as ' NULL - { version } '
! ! Notice ! ! Which will allow you to see if this plugin needs updates on later script runs by comparing the two NULL versions .
"""
2024-09-19 18:10:45 +00:00
)
version = f " NULL- { version } "
else :
with open ( f " plugins/ { pluginName } -spigot- { version } .jar " , " wb " ) as f :
2024-09-19 03:55:09 +00:00
f . write ( r . content )
2024-09-19 00:31:01 +00:00
if knownVersions . get ( plugin , " " ) and path . exists (
2024-09-19 02:08:38 +00:00
f " plugins/ { pluginName } -spigot- { knownVersions . get ( plugin , ' ' ) } .jar "
2024-09-19 00:31:01 +00:00
) :
remove (
2024-09-19 02:08:38 +00:00
f " plugins/ { pluginName } -spigot- { knownVersions . get ( plugin , ' ' ) } .jar "
2024-09-19 00:31:01 +00:00
)
2024-09-18 21:57:57 +00:00
print (
f " [[ Info ]] Updated plugin { pluginName } from { knownVersions [ plugin ] if knownVersions . get ( plugin , False ) else ' N/A ' } to { version } "
)
2024-09-18 02:31:30 +00:00
knownVersions [ plugin ] = version
if geyser :
2024-09-19 00:31:01 +00:00
print (
""" [[ Info ]] Updating geyser even if there isn ' t an update availiable
[ [ Info ] ] Because my author is too lazy to implement proper update checking just for geyser and floodgate """
)
2024-09-19 15:42:21 +00:00
with open ( " plugins/Geyser-Spigot.jar " , " wb " ) as f :
2024-09-19 00:31:01 +00:00
f . write (
request (
" GET " ,
" https://download.geysermc.org/v2/projects/geyser/versions/latest/builds/latest/downloads/spigot " ,
) . content
)
2024-09-18 02:31:30 +00:00
if floodgate :
2024-09-19 00:31:01 +00:00
print (
""" [[ Info ]] Updating floodgate even if there isn ' t an update availiable
[ [ Info ] ] Because my author is too lazy to implement proper update checking just for geyser and floodgate """
)
with open ( " plugins/floodgate.jar " , " wb " ) as f :
f . write (
request (
" GET " ,
" https://download.geysermc.org/v2/projects/floodgate/versions/latest/builds/latest/downloads/spigot " ,
) . content
)
2024-09-18 02:31:30 +00:00
# write cache file
2024-09-19 00:31:01 +00:00
with open ( " cache.py " , " w " ) as f :
f . write ( f " knownVersions= { knownVersions } " )
print ( " [[ Info ]] Updates complete! " )