Created
December 24, 2019 11:25
-
-
Save bwhli/fe13d269fc979a0c4d2ca5805031e212 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 datetime | |
import io | |
import os | |
import pyperclip | |
import re | |
import shutil | |
import sys | |
from google.cloud import storage | |
from PIL import Image | |
def create_file_path(): | |
year = datetime.datetime.now().strftime("%Y") | |
month = datetime.datetime.now().strftime("%m") | |
file_path = f"/uploads/{year}/{month}" | |
return file_path | |
def upload_to_dropbox(file): | |
filename = re.sub(r"\/.*\/(.*\.\w{1,4})",r'\1',file) | |
destination_path = f"/Users/brianli/Dropbox/brianli.com/static{create_file_path()}/{filename}" | |
shutil.copy(file, destination_path) | |
def upload_to_gcs(file): | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/brianli/Documents/GitHub/brian-tools/brianli-c4aab32d485e.json" #Set gloud credentials. | |
client = storage.Client() #Set gcloud client. | |
bucket = client.get_bucket("cdn.brianli.com") #Specify gcloud bucket. | |
filename = re.sub(r"\/.*\/(.*\.\w{1,4})",r'\1',file) | |
blob = bucket.blob(f"{create_file_path()[1:]}/{filename}") #Set filename format (uploads/year/month/filename). | |
blob.upload_from_filename(file) | |
blob.make_public() | |
url = blob.public_url | |
print(f"Image URL - {url}") | |
def copy_img_shortcode(file): | |
filename = re.sub(r"\/.*\/(.*\.\w{1,4})",r'\1',file) | |
destination_path = f"{create_file_path()}/{filename}" | |
img_shortcode = f"{{{{< img src=\"{destination_path[1:]}\" alt=\"\" >}}}}" | |
pyperclip.copy(img_shortcode) | |
if __name__ == '__main__': | |
file = sys.argv[1] | |
file = "/Users/brianli/Documents/GitHub/brian-tools/image.jpg" | |
upload_to_dropbox(file) | |
upload_to_gcs(file) | |
copy_img_shortcode(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment