Created
April 18, 2021 04:29
-
-
Save rririanto/0e771f28d27f000191e376f6a1761d07 to your computer and use it in GitHub Desktop.
Creating Python Script To Restore The Image To Ghost CMS
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 json | |
import os | |
import wget #pip install wget | |
from pathlib import Path | |
JSON_PATH = "<YOUR JSON PATH>" | |
def wget_image(image_url): | |
filename = os.path.basename(image_url) | |
location = os.path.dirname(image_url) | |
location = location.replace("/content/images/", "") | |
url = "https://YOURSITE.com/wp-content/uploads/%s/%s" % (location, filename) | |
destination = os.path.join(location, filename) | |
if not os.path.isfile(destination): | |
Path(location).mkdir(parents=True, exist_ok=True) | |
wget.download(url, destination) | |
print ("Success") | |
def initiate_data(file): | |
data = json.loads(file)['data'] | |
for dt in data['posts']: | |
feature_image = dt['feature_image'] | |
if feature_image: | |
print (feature_image) | |
wget_image(feature_image) | |
if __name__ == "__main__": | |
get_data = open(JSON_PATH).read() | |
initiate_data(get_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment