Skip to content

Instantly share code, notes, and snippets.

@TaivasJumala
Last active December 8, 2023 12:00
Show Gist options
  • Save TaivasJumala/c649972c4e8d7ca949b1aff221a6a033 to your computer and use it in GitHub Desktop.
Save TaivasJumala/c649972c4e8d7ca949b1aff221a6a033 to your computer and use it in GitHub Desktop.
Pack memes to a zip-file that is able to be bulk imported in aaa1115910/Rays-Android
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @name: Taivas Jumala
# @email: [email protected]
# @brief: convert a meme fold to meet Rays-Android backup file requirements
import argparse
import hashlib
import json
import pathlib
import os
import shutil
import time
import uuid
import zipfile
parser = argparse.ArgumentParser(
prog='Meme Packager for Rays',
description='Convert a meme folder to meet Rays-Android backup file requirements',
)
# one or more path to meme package folder is available
parser.add_argument('path', metavar='Path', type=pathlib.Path, nargs='+')
args = parser.parse_args()
if not os.path.exists("./processing"):
os.mkdir("processing")
for path in args.path:
meme_package_name=os.path.basename(path)
print("----------")
print("Start packaging "+meme_package_name)
# temporary directory to proceed
if not os.path.exists("processing/"+meme_package_name+"/BackupData"):
os.makedirs("processing/"+meme_package_name+"/BackupData")
if not os.path.exists("processing/"+meme_package_name+"/BackupSticker"):
os.makedirs("processing/"+meme_package_name+"/BackupSticker")
# get a list of image/meme file names
meme_filelist=list(set(os.listdir(path)) - {'desktop.ini', 'whatever.ini'})
for meme_filename in meme_filelist:
print("adding "+meme_filename)
meme_file_md5=hashlib.md5() # calculate MD5
with open(os.path.join(path,meme_filename), "rb") as meme_file:
meme_file_md5 = hashlib.md5(meme_file.read()).hexdigest()
meme_uuid=str(uuid.uuid4()) # generate uuid
meme_filename_without_suffix=os.path.splitext(meme_filename)[0] # filename.png ⟶ filename
# write to BackupData as json
with open(os.path.join(os.path.curdir,"processing",meme_package_name,"BackupData",meme_uuid),'w') as meme_data:
meme={"sticker":{
"uuid":meme_uuid,
"title":meme_filename_without_suffix,
"stickerMd5":meme_file_md5,
"clickCount":"0",
"shareCount":"0",
"createTime":int(time.time()*1000),
},
"tags":[
{
"stickerUuid":meme_uuid,
"tag":meme_package_name,
"createTime":int(time.time())*1000
}
]
}
json.dump(meme,meme_data,indent=4)
# copy image/meme to BackupSticker
shutil.copy2(os.path.join(path,meme_filename), os.path.join(os.path.curdir,"processing",meme_package_name,"BackupSticker",meme_uuid))
# zip file
folder_to_zip=os.path.join(os.path.curdir,"processing",meme_package_name)
with zipfile.ZipFile(meme_package_name+".zip", 'w', zipfile.ZIP_DEFLATED) as zip_file:
for root, dirs, files in os.walk(folder_to_zip):
for file in files:
file_path = os.path.join(root, file)
zip_file.write(file_path, os.path.relpath(file_path, folder_to_zip))
# add extra data as identifier
with open(meme_package_name+".zip", 'ab') as zip_file:
zip_file.write(bytes.fromhex("0d000721"))
shutil.rmtree("processing")
@TaivasJumala
Copy link
Author

TaivasJumala commented Dec 8, 2023

Tested in Windows 11 with Python 3.11

Use case:

python meme_packager_for_rays.py .\your\first\meme\folder1\ .\you\second\meme\folder2\

One or more meme folder paths are available, the command above will create folder1.zip and folder2.zip under your working directory.

Meme title will be image file name.
The script will also add tag 'folder1' for every meme under folder1, same to folder2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment