最近开始使用masscode整理代码片段,但masscode仅支持Google Drive, Dropbox 等网盘实现多端同步,于是做了这个程序,利用阿里云盘同步masscode数据。
[Python] 纯文本查看 复制代码 from aligo import Aligo
import os
from datetime import datetime ,timedelta
import hashlib
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s - %(funcName)s:%(lineno)d: %(message)s')
LOG_FILE = "masscode.log"
def utc_str_to_timestamp(utc: str) -> int:
return int(datetime.fromisoformat(utc[:-1]).timestamp()) + 28800
def core_sha1(param):
sha1 = hashlib.sha1()
with open(param, 'rb') as f:
while True:
data = f.read(8192)
if not data:
break
sha1.update(data)
return sha1.hexdigest()
def get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler
def get_file_handler():
file_handler = TimedRotatingFileHandler(LOG_FILE, when='midnight')
file_handler.setFormatter(FORMATTER)
return file_handler
def get_logger(logger_name):
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler())
logger.propagate = False
return logger
if __name__ == '__main__':
ali = Aligo()
my_logger = get_logger("masscodesync")
local_folder = 'D:/Code'#masscode存储路径
local_file = local_folder + '/db.json'
remote_folder = '/Code'#云端备份路径
remote_file = remote_folder + '/db.json'
obj_remote_folder = ali.get_folder_by_path(remote_folder)
if obj_remote_folder is None:
my_logger.info('云端文件夹不存在,创建云端文件夹')
ali.create_folder(remote_folder)
obj_remote_folder = ali.get_folder_by_path(remote_folder)
if not os.path.exists(local_folder):
os.mkdir(local_folder)
my_logger.info('本地文件夹不存在,创建本地文件夹')
if os.path.exists(local_file):
localfile = True
local_time = os.path.getmtime(local_file)
local_time = int(local_time)
local_sha1 = core_sha1(local_file).lower()
my_logger.info('本地文件修改时间' + str(datetime.utcfromtimestamp(local_time) + timedelta(hours=8)))
my_logger.info('本地文件SHA1 ' + local_sha1)
else:
localfile = False
my_logger.info('本地文件不存在')
obj_remote_file = ali.get_file_by_path(remote_file)
if obj_remote_file is None:
remotefile=False
my_logger.info('云端文件不存在')
else:
remotefile=True
remote_time = utc_str_to_timestamp(obj_remote_file.updated_at)
remote_sha1 = obj_remote_file.content_hash.lower()
my_logger.info('云端文件修改时间' + str(datetime.utcfromtimestamp(remote_time) + timedelta(hours=8)))
my_logger.info('云端文件SHA1 ' + remote_sha1)
if localfile and not remotefile:
my_logger.info('本地有,云端无,上传')
ali.upload_file(local_file, parent_file_id=obj_remote_folder.file_id)
if remotefile and not localfile:
my_logger.info('云端有,本地无 ,下载')
ali.download_file(file=obj_remote_file, local_folder=local_folder)
if remotefile and localfile:
if local_sha1 != remote_sha1:
my_logger.info('本地和云端文件不同')
if local_time > remote_time:
my_logger.info('本地文件比云端新,上传覆盖')
ali.upload_file(local_file, parent_file_id=obj_remote_folder.file_id,check_name_mode='overwrite')
elif remote_time > local_time:
my_logger.info('云端文件比本地新,删除本地,下载云端文件到本地')
os.remove(local_file)
ali.download_file(file=obj_remote_file, local_folder=local_folder)
else:
my_logger.info('本地与云端文件相同,无需操作')
话说做完以后,发现aligo自带文件夹同步功能。
[Python] 纯文本查看 复制代码 from aligo import Aligo
if __name__ == '__main__':
ali = Aligo()
obj_remote_folder = ali.get_folder_by_path('/Code')
ali.sync_folder('D:/Code',obj_remote_folder.file_id) |