Advertisement
Masoko

Pelican image/screenshots down-loader freeapps.ml

Apr 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. from os import listdir
  2. from os.path import isfile, join, dirname
  3. from os import walk
  4. import requests
  5.  
  6. CONTENT_PATH = '/mnt/usb/softdir/content/posts/'
  7. IMG_PATH = '/mnt/usb/softdir/content/images/'
  8. SCREENSHOTS_PATH = '/mnt/usb/softdir/content/images/screenshots/'
  9. #get all files
  10. files = []
  11. for (dirpath, dirnames, filenames) in walk(CONTENT_PATH):
  12.     for file in filenames:
  13.         if file.split('.')[-1] == 'md':
  14.             files.append(join(dirpath,file))
  15.  
  16. for file in files:
  17.     with open(file) as fp:
  18.         new_file = []
  19.         updated = False
  20.         for line in fp:
  21.             if line.split(':')[0] == 'image' and not line.split(':')[1].strip().startswith('/'):
  22.                 updated = True
  23.                 image_url = (line.split(':')[1] + ':' + line.split(':')[2]).strip()
  24.                 ext = '.' + image_url.split('.')[-1]
  25.                 if image_url.startswith('http'):
  26.                     local_img = join(IMG_PATH,file.split('/')[-1].split('.')[0] + ext)
  27.                     # download and save image file
  28.                     img_data = requests.get(image_url, verify = False).content
  29.                     with open(local_img, 'wb') as handler:
  30.                         handler.write(img_data)
  31.                     # update new image: line
  32.                     new_file.append('image: /images/'+file.split('/')[-1].split('.')[0] +  ext+ '\n')
  33.             elif line.split(':')[0] == 'screenshot' and not line.split(':')[1].strip().startswith('/'):
  34.                 updated = True
  35.                 image_url = (line.split(':')[1] + ':' + line.split(':')[2]).strip()
  36.                 ext = '.' + image_url.split('.')[-1]
  37.                 if image_url.startswith('http'):
  38.                     local_img = join(SCREENSHOTS_PATH,file.split('/')[-1].split('.')[0] + ext)
  39.                     # download and save image file
  40.                     img_data = requests.get(image_url, verify = False).content
  41.                     with open(local_img, 'wb') as handler:
  42.                         handler.write(img_data)
  43.                     # update new image: line
  44.                     new_file.append('screenshot: /images/screenshots/'+file.split('/')[-1].split('.')[0] +  ext+ '\n')
  45.             else:
  46.                 # save unmodified line
  47.                 new_file.append(line)
  48.         # print(new_file)
  49.     # Update the .md file
  50.     if updated:
  51.         thefile = open(file, 'w')
  52.         for item in new_file:
  53.             thefile.write("%s" % item)
  54.         print(file)
  55.         thefile.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement