Last active
January 2, 2019 07:06
-
-
Save BruceZhang1993/c87eb6ec6022a495e54246529b9669cd to your computer and use it in GitHub Desktop.
FeelUOwn collection sync -- demo version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *-- coding: utf-8 --* | |
import logging | |
from .backup import Backup | |
__alias__ = 'FeelUOwn Collection Backup' | |
__version__ = '0.0.1' | |
__feeluown_version__ = '2.3.0' | |
__desc__ = 'Backup FeelUOwn collections with GitHub gist.' | |
logger = logging.getLogger(__name__) | |
def enable(app): | |
global loader | |
loader = Backup(app) | |
loader.subscribe() | |
logger.info(__alias__ + ' enabled.') | |
def disable(app): | |
global loader | |
loader.unsubscribe() | |
logger.info(__alias__ + ' disabled.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *-- coding: utf-8 --* | |
import logging | |
import asyncio | |
import time | |
import base64 | |
import requests | |
import json | |
import os | |
import hashlib | |
from PyQt5.QtCore import QObject | |
from requests.auth import HTTPBasicAuth | |
logger = logging.getLogger(__name__) | |
USERNAME = '' | |
TOKEN = '' | |
GIST_ID = '' | |
COLLECTION_DIR = os.path.expanduser('~') + '/.FeelUOwn/collections' | |
class Backup(QObject): | |
def __init__(self, app): | |
if app.mode & app.GuiMode: | |
super().__init__(parent=app) | |
else: | |
super().__init__(parent=None) | |
self._app = app | |
self._player: Player = self._app.player | |
def update_gist(self): | |
files = { | |
'README.md': { | |
'content': '# Collections syncing from FeelUOwn player\n', | |
'filename': 'README.md' | |
}, | |
} | |
original_md5 = { | |
} | |
md5sums = {} | |
if not os.path.exists(COLLECTION_DIR): | |
return False | |
collection_files = os.listdir(COLLECTION_DIR) | |
for file in collection_files: | |
if os.path.isfile(COLLECTION_DIR + '/' + file) and file.endswith('.md5'): | |
with open(COLLECTION_DIR + '/' + file, 'r') as ff: | |
original_md5[file] = ff.read().strip() | |
for file in collection_files: | |
if os.path.isfile(COLLECTION_DIR + '/' + file) and file.endswith('.fuo'): | |
with open(COLLECTION_DIR + '/' + file, 'r') as f: | |
file_string = f.read() | |
md5sums[file] = hashlib.md5(file_string.encode()).hexdigest() | |
if md5sums[file] != original_md5.get(file + '.md5'): | |
files[file] = { | |
'content': file_string, | |
'filename': file | |
} | |
if len(files) > 1: | |
auth = HTTPBasicAuth(USERNAME, TOKEN) | |
r = requests.patch('https://api.github.com/gists/{}'.format(GIST_ID), json.dumps({ | |
'description': 'Collections syncing from FeelUOwn player', | |
'files': files, | |
}), headers={ | |
'Accept': 'application/vnd.github.v3.full+json' | |
}, auth=auth) | |
result = r.json() | |
if r.status_code == 200: | |
for k, v in md5sums.items(): | |
with open(COLLECTION_DIR + '/' + k + '.md5', 'w') as fff: | |
fff.write(v) | |
logger.debug(result) | |
return True | |
async def _subscribe(self): | |
try: | |
loop = asyncio.get_event_loop() | |
logger.info('Start updating gist.') | |
await loop.run_in_executor(None, self.update_gist) | |
except Exception as e: | |
logger.warning('Something goes wrong: %s' % e) | |
def subscribe(self): | |
asyncio.ensure_future(self._subscribe()) | |
def unsubscribe(self): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment