Created
April 25, 2018 02:27
-
-
Save MCOfficer/64217e66a56eba548da87bdeafeb1812 to your computer and use it in GitHub Desktop.
[Python] Crude script that i use to mirror the endless sky assets on my server.
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
from pydrive.auth import GoogleAuth | |
from pydrive.drive import GoogleDrive | |
import os | |
import time | |
import datetime | |
mirror_dir = "PUT SOMETHING HERE" | |
gauth = GoogleAuth() | |
gauth.LoadCredentialsFile("credentials") | |
if gauth.credentials is None: | |
gauth.CommandLineAuth() | |
elif gauth.access_token_expired: | |
gauth.Refresh() | |
else: | |
gauth.Authorize() | |
gauth.SaveCredentialsFile("credentials") | |
drive = GoogleDrive(gauth) | |
def download_folder_recursive(dir_id: str, path : str): | |
if not os.path.exists(path): | |
os.makedirs(path) | |
folder_contents = drive.ListFile({"q": "'" + dir_id + "' in parents"}).GetList() | |
for file in folder_contents: | |
if file["mimeType"] == "application/vnd.google-apps.folder": | |
print("Checking folder " + path + file["title"]) | |
download_folder_recursive(file["id"], path + file["title"] + "/") | |
else: | |
if os.path.exists(path + file["title"]): | |
file.FetchMetadata(fields="labels") | |
remotemod = file.metadata["modifiedDate"] | |
localmod = os.path.getmtime(path + file["title"]) | |
remotemod = time.mktime( | |
datetime.datetime.strptime(remotemod[:remotemod.index("T")], "%Y-%m-%d").timetuple()) | |
if localmod > remotemod: | |
continue | |
print("Downloading file " + path + file["title"]) | |
file.GetContentFile(path + file["title"]) | |
remote_files = drive.ListFile({"q": "'root' in parents"}).GetList() | |
for remote_file in remote_files: | |
if remote_file["title"] == "assets for endless sky": | |
print("Found asset directory with id " + remote_file["id"]) | |
download_folder_recursive(remote_file["id"], mirror_dir) | |
print("...Done") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment