Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import listdir
- from os.path import isfile, join, dirname
- from os import walk
- import requests
- CONTENT_PATH = '/mnt/usb/softdir/content/posts/'
- IMG_PATH = '/mnt/usb/softdir/content/images/'
- SCREENSHOTS_PATH = '/mnt/usb/softdir/content/images/screenshots/'
- #get all files
- files = []
- for (dirpath, dirnames, filenames) in walk(CONTENT_PATH):
- for file in filenames:
- if file.split('.')[-1] == 'md':
- files.append(join(dirpath,file))
- for file in files:
- with open(file) as fp:
- new_file = []
- updated = False
- for line in fp:
- if line.split(':')[0] == 'image' and not line.split(':')[1].strip().startswith('/'):
- updated = True
- image_url = (line.split(':')[1] + ':' + line.split(':')[2]).strip()
- ext = '.' + image_url.split('.')[-1]
- if image_url.startswith('http'):
- local_img = join(IMG_PATH,file.split('/')[-1].split('.')[0] + ext)
- # download and save image file
- img_data = requests.get(image_url, verify = False).content
- with open(local_img, 'wb') as handler:
- handler.write(img_data)
- # update new image: line
- new_file.append('image: /images/'+file.split('/')[-1].split('.')[0] + ext+ '\n')
- elif line.split(':')[0] == 'screenshot' and not line.split(':')[1].strip().startswith('/'):
- updated = True
- image_url = (line.split(':')[1] + ':' + line.split(':')[2]).strip()
- ext = '.' + image_url.split('.')[-1]
- if image_url.startswith('http'):
- local_img = join(SCREENSHOTS_PATH,file.split('/')[-1].split('.')[0] + ext)
- # download and save image file
- img_data = requests.get(image_url, verify = False).content
- with open(local_img, 'wb') as handler:
- handler.write(img_data)
- # update new image: line
- new_file.append('screenshot: /images/screenshots/'+file.split('/')[-1].split('.')[0] + ext+ '\n')
- else:
- # save unmodified line
- new_file.append(line)
- # print(new_file)
- # Update the .md file
- if updated:
- thefile = open(file, 'w')
- for item in new_file:
- thefile.write("%s" % item)
- print(file)
- thefile.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement