Skip to content

Instantly share code, notes, and snippets.

@connorjan
Last active March 7, 2021 23:58
Show Gist options
  • Save connorjan/f17a832d54eed0debc23 to your computer and use it in GitHub Desktop.
Save connorjan/f17a832d54eed0debc23 to your computer and use it in GitHub Desktop.
import os
import re
import urllib2
class CSServer:
def __init__(self, minTier, maxTier, mapType, mapsFilePath, mapsDirectory, fastDlServer = ""):
self.MinTier = minTier
self.MaxTier = maxTier
self.MapType = mapType
self.MapsFilePath = mapsFilePath
self.MapsDirectory = mapsDirectory
self.FastDlServer = fastDlServer
CheckFastDl = False
FastDlServer = ""
MinTier = ""
MaxTier = ""
MapsFilePath = ""
MapsDirectory = ""
MapsFromDir = []
MapsFromFile = []
MapsNew = []
MapsForServer = []
MapsFromFastDl = []
MapErrors = dict()
MapsInDatabase = dict()
# Get maps that were not added to the map list file
def GetMapErrors(self):
notInDBString = "not in database"
notInFastDlString = "not in FastDl"
for _map in self.MapsFromDir:
self.MapErrors[_map] = []
if _map not in self.MapsInDatabase:
self.MapErrors[_map].append(notInDBString)
if self.CheckFastDl and _map not in self.MapsFromFastDl:
self.MapErrors[_map].append(notInFastDlString)
# Get all specified map files from the maps directory
def GetMapsFromDir(self):
if os.path.isdir(self.MapsDirectory):
self.MapsFromDir = [os.path.splitext(f)[0] for f in os.listdir(self.MapsDirectory) if f.startswith(self.MapType) and f.endswith(".bsp")]
else:
self.MapsFromDir = []
# Get all of the maps we have on the fast dl server
def GetMapsFromFastDl(self):
urlpath = urllib2.urlopen(self.FastDlServer)
if urlpath.getcode() != 200:
return
page = urlpath.read().decode('utf-8')
pattern = "<a href=\"%s" % self.MapType
# Yeah this is hacky and slow, leave me alone
for line in page.splitlines():
if pattern in line:
cut1 = line[line.find(pattern)+len(pattern)-len(self.MapType):]
mapName = cut1[:cut1.find('.')]
self.MapsFromFastDl.append(str(mapName))
if self.MapsFromFastDl:
self.CheckFastDl = True
# Get all of the maps we have in the map list file already
def GetMapsFromFile(self):
if os.path.exists(self.MapsFilePath):
self.MapsFromFile = [m.strip() for m in open(self.MapsFilePath)]
else:
self.MapsFromFile = []
# Figure out what the new maps are
def GetNewMaps(self):
self.MapsNew = [m for m in self.MapsForServer if m not in self.MapsFromFile]
# Get the maps that should go on this server based on tier
def GetMapsForServer(self):
self.MapsForServer = [m for m in self.MapsFromDir if m in self.MapsInDatabase and self.MapsInDatabase[m] >= self.MinTier and self.MapsInDatabase[m] <= self.MaxTier]
# If we are checking the fast dl, remove maps that are not on there
if self.CheckFastDl:
self.MapsForServer = [m for m in self.MapsForServer if m in self.MapsFromFastDl]
# Write the map list file
def WriteMapList(self):
with open(self.MapsFilePath, "w+") as _file:
_file.seek(0)
_file.truncate()
_file.write("\n".join(sorted(self.MapsForServer)))
# Do everything
def Update(self):
self.GetMapsFromDir()
self.GetMapsFromFile()
# If we have a fast dl server address provided
if self.FastDlServer:
self.GetMapsFromFastDl()
self.GetMapsForServer()
self.GetNewMaps()
self.GetMapErrors()
self.WriteMapList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment