Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to import a JSONFeed file and referenced images to
# -*- coding: utf-8 -*-
import json
import pypandoc
import re
import requests
import os.path
import sys
import time
headers = {
'Authorization': 'Bearer XXXXXX',
count = 0
with open('feed2.json') as f:
data = json.load(f)
for i in data['items']:
count = count + 1
print ("%s" % count)
print (i['title'])
print (i['date_published'])
md = pypandoc.convert_text(i['content_html'], 'md', format='html')
# print ("Before uploading images...")
# print (md)
urls = re.findall(r'(?:[\.\w\d\/\_\-]+)', md)
for u in urls:
# Make sure the file exists to upload
filepath = '.../' % u
if not os.path.isfile(filepath):
print ("Cannot find %s" % filepath)
fsize = os.path.getsize(filepath)
print ("Found %s (%s)..." % (filepath, fsize))
# Upload the image to
files = {
'file': (filepath, open(filepath, 'rb')),
response ='', headers=headers, files=files)
print (response)
# get location header
new_image = response.headers['location']
print ("%s -> %s" % (u, new_image))
# input("Press Enter to continue...")
# Update the markdown to use the new URL
md = re.sub(rf'(?:{u}', new_image, md)
# print ("After uploading images...")
# print (md)
print ("Sending to")
data = {
'name': i['title'],
'content': md,
'published': i['date_published']
print (data)
response ='', headers=headers, data=data)
print (response.text)
# input("Press Enter to continue...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment