Created
April 8, 2023 17:09
-
-
Save domjancik/c6f813375e5cb82140c33f00341407aa to your computer and use it in GitHub Desktop.
Filter for files in MEGA cloud storage modified between a specific time range using Python
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
import base64 | |
import datetime | |
from mega import Mega | |
# Initiate MEGA client | |
mega = Mega() | |
# Login to MEGA. Make sure not to share your credentials, eg. in Github. For instance use env variables or a secrets manager. | |
EMAIL = "" | |
PASSWORD = "" | |
m = mega.login(EMAIL, PASSWORD) | |
# Get all files, may take a moment. Responds with a dictionary with MEGA node ID keys pointing to decrypted API response. | |
files = m.get_files() | |
def base64urldecode(data: str): | |
""" | |
Base64 decode of MEGA node hash with specific character replacements | |
functionality ported from https://github.com/meganz/webclient/blob/master/nodedec.js with help of ChatGPT | |
""" | |
data += '=='.encode('utf-8')[(2 - len(data) * 3) & 3:].decode('utf-8') | |
data = data.replace('-', '+').replace('_', '/').replace(',', '') | |
try: | |
return base64.urlsafe_b64decode(data.encode('utf-8')) | |
except: | |
return b'' | |
def get_mtime_from_hash(hash: str): | |
""" | |
Extract last modified time from MEGA node hash | |
mtime is a unix style numerical timestamp | |
functionality ported from https://github.com/meganz/webclient/blob/master/nodedec.js with help of ChatGPT | |
""" | |
h = base64urldecode(hash) | |
i = h[16] | |
if i <= 4: # FIXME: change to 5 before the year 2106 | |
t = 0 | |
for j in range(i-1, -1, -1): | |
t = t * 256 + h[17+j] | |
return t | |
else: | |
return None | |
# Set filter range | |
to_ts = datetime.datetime(day=18, year=2020, month=7).timestamp() | |
from_ts = datetime.datetime(day=13, year=2020, month=7).timestamp() | |
# Get files with modified time between specified from and to timestamps | |
filtered_files = [] | |
for file in files: | |
try: | |
c = files[file]["a"]["c"] #hash | |
mtime = get_mtime_from_hash(c) | |
if mtime < to_ts and mtime > from_ts: | |
filtered_files.append(files[file]) | |
except: | |
# We may fail to get hash if "a" field for file failed to decrypt | |
pass | |
# Print names of listed files/folders | |
print([f["a"]["n"] for f in filtered_files]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment