Skip to content

Instantly share code, notes, and snippets.

@xDeda
Last active April 22, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xDeda/da0dd03445b7ff5df9e53e090413c3ea to your computer and use it in GitHub Desktop.
Save xDeda/da0dd03445b7ff5df9e53e090413c3ea to your computer and use it in GitHub Desktop.
Saves images of maps!
# Map image downloader by Tactcat!
# Never forget to save a code!
# 1 Creates /maps folder in your tfmplugins folder
# 2 Map plays
# 3 XML sent to entibo's Map Renderer (ty entibo)
# 4 Downloads return imgur image of map
# 5 Saves image in /maps map category subfolder with filename mapcode_author.png
# If the map perm subfolder doesn't exist, it'll create it
# Filename example: 1360084_Yacii.png for the map @1360084 by Yacii (a P1 map)
# Saved in /maps/p1
import requests
import os
from tfmplugins.tfm.packet import Packet
import zlib
class TFMaps:
async def tear_down(self):
"""ss"""
async def packet_sent(self, client, conn, fp, packet):
"""ss"""
pass
async def packet_received(self, client, conn, packet):
CCC = packet.readCode()
if CCC == (5, 2): # map players - map info - code, xml, author, perm
try:
mapcode = packet.read32() #[Athesdrake] 4 bytes map_code
packet.read16() #[Athesdrake] 2 bytes nbr_players
packet.read8() #[Athesdrake] 1 byte round_code
enclen = packet.read32() #[Athesdrake] 4 bytes xml's length
encxml = packet.readBytes(enclen) #[Athesdrake] compressed xml
#print("at pos:", packet.pos)
#print(CCC, bytes(packet.buffer[packet.pos:]))
xml = zlib.decompress(encxml).decode()
author = packet.readString().decode() #[Athesdrake] string author
permcat = packet.read8() #[Athesdrake] 1 byte map perm
if not os.path.exists('maps'):
os.makedirs('Maps folder created')
if not os.path.exists('maps/p'+str(permcat)):
os.makedirs('maps/p'+str(permcat))
print('Even the category didn\'t exist! /maps/p'+str(permcat)+' created')
if len(os.listdir('maps/p'+str(permcat))) == 0: # if the perm category folder is empty
print('maps/p'+str(permcat)+' is empty')
exists = False
else: # if the map is already downloaded
for f in os.listdir('maps/p'+str(permcat)):
if str(mapcode) in f:
exists = True
break
else:
exists = False
if exists:
print('@'+str(mapcode)+' already exists!')
else: # send to map renderer and download+save
post = { 'xml': xml, 'raw' : False }
response = requests.post('https://miceditor-map-preview.herokuapp.com/', json=post)
url = response.content.decode()
print(xml)
print('@'+str(mapcode)+' by '+author+' (P'+str(permcat)+') url: '+url)
imgfile = requests.get(url, allow_redirects=True)
open('maps/p'+str(permcat)+'/'+str(mapcode)+'_'+author+'.png', 'wb').write(imgfile.content)
print('saved as /maps/p'+str(permcat)+'/'+str(mapcode)+'_'+author+'.png')
print("===============================")
except Exception as e: # tbh no clue what this does except print errors
print(e)
print('err at pos:', packet.pos)
print(CCC, bytes(packet.buffer[packet.pos:]))
print("===============================")
plugin = TFMaps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment