打开mblock
:
URL: https://ide.makeblock.com/
开F12,切到角色
页面:
点一下添加,看到有很多素材:
切到Network
页的XHR
找接口:
其中sprites.json
引起了我的注意,点开看看:
是一个列表套dict的结构
其中一个dict:
{
name: "Abby", // 角色名
md5: "afab2d2141e9811bd89e385e9628cb5f.svg", // 图片url
type: "sprite"
}
看一眼sprites.json
的url:
https://res-cn.makeblock.com/mblock/static/sprites/scratch/sprites.json
刷新几次发现url是固定的。
先倒包:
import os
import sys
import glob
import requests
from bs4 import BeautifulSoup
try:
import simplejson as json
except ModuleNotFoundError:
import json
写一个load_json
:
def load_json():
url = 'https://res-cn.makeblock.com/mblock/static/sprites/makeblock/sprites.json'
response = requests.get(url)
response.encoding = 'utf-8'
result = json.loads(response.text)
print('INFO: loaded sprites json-file')
return result
在写一个download_image
:
def download_image(name, file_name):
url = f'https://res-cn.makeblock.com/mblock/static/assets/scratch/{name}'
response = requests.get(url)
image_format = name.split('.')[-1]
file_name = file_name + '.' + image_format
with open(f'downloads/{file_name}', 'wb') as fp:
fp.write(response.content)
print(f'INFO: downloaded {file_name}')
串起来:
import os
import sys
import glob
import requests
from bs4 import BeautifulSoup
try:
import simplejson as json
except ModuleNotFoundError:
import json
def download_image(name, file_name):
url = f'https://res-cn.makeblock.com/mblock/static/assets/scratch/{name}'
response = requests.get(url)
image_format = name.split('.')[-1]
file_name = file_name + '.' + image_format
with open(f'downloads/{file_name}', 'wb') as fp:
fp.write(response.content)
print(f'INFO: downloaded {file_name}')
def load_json():
url = 'https://res-cn.makeblock.com/mblock/static/sprites/makeblock/sprites.json'
response = requests.get(url)
response.encoding = 'utf-8'
result = json.loads(response.text)
print('INFO: loaded sprites json-file')
return result
def install():
if not os.path.exists('downloads'):
os.mkdir('downloads')
print('INFO: created downloads folder')
sprites = load_json()
for sprite_info in sprites:
name = sprite_info.get('name', 'nullify')
md5 = sprite_info.get('md5', None)
if md5:
download_image(md5, name)
def clear():
if os.path.exists('downloads'):
for fp in glob.glob('downloads/*.*'):
os.remove(fp)
print(f'INFO: deleted {fp}')
else:
print('ERROR: not installed mblock-images')
def main():
if len(sys.argv) != 1:
name = sys.argv[1]
if name in ('install', 'download'):
install()
sys.exit(0)
elif name in ('clear', 'clean'):
clear()
sys.exit(0)
if __name__ == '__main__':
main()
最后运行一下:
python -m install-mblock-images.py download
等待一分钟:
nice!