Skip to content

Instantly share code, notes, and snippets.

@rririanto
Created April 18, 2021 04:29
Show Gist options
  • Save rririanto/0e771f28d27f000191e376f6a1761d07 to your computer and use it in GitHub Desktop.
Save rririanto/0e771f28d27f000191e376f6a1761d07 to your computer and use it in GitHub Desktop.
Creating Python Script To Restore The Image To Ghost CMS
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