diff --git a/stanis-tits.py b/stanis-tits.py index 837b876..c40fc28 100644 --- a/stanis-tits.py +++ b/stanis-tits.py @@ -3,59 +3,57 @@ import re import shutil import os.path -try: - import requests - from bs4 import BeautifulSoup -except ImportError: - import sys - sys.path.append('./.pip') - import requests - from bs4 import BeautifulSoup +import requests +from bs4 import BeautifulSoup __author__ = 'Alexander Popov' __version__ = '0.1.0' __license__ = 'Unlicense' -IMAGES_DIR = './images' -COOKIES = dict(block='951') -URL = 'http://blog.stanis.ru/?back=%d' -PAGE = 0 -if not os.path.exists('%s/.stanis-tits.latest' % IMAGES_DIR): - if not os.path.exists('%s' % IMAGES_DIR): - os.mkdir('%s' % IMAGES_DIR) +dwnImageDir = './images' +cookies = dict(block='951') +siteUrl = 'http://blog.stanis.ru/?back=%d' +imgPage = 0 - with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'w') as f: +# create .stanis-tits.latest file and download image directory +if not os.path.exists('%s/.stanis-tits.latest' % dwnImageDir): + if not os.path.exists('%s' % dwnImageDir): + os.mkdir('%s' % dwnImageDir) + + with open('%s/.stanis-tits.latest' % dwnImageDir, 'w') as f: f.write('0') -with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'r') as f: - LATEST_FILE = f.read() + +with open('%s/.stanis-tits.latest' % dwnImageDir, 'r') as f: + latestDwnFile = f.read() + STOP = False NEXT_LATEST = None while STOP is False: - print('Loading page %d' % PAGE) + print('Loading page %d' % imgPage) - r = requests.get(URL % PAGE, cookies=COOKIES) + r = requests.get(siteUrl % imgPage, cookies=cookies) soup = BeautifulSoup(r.text.encode('cp1251'), "html.parser", from_encoding="windows-1251") images = soup.findAll('img', src=re.compile('img/*')) for image in images: - if image['src'].split('/')[1].split('.')[0] == LATEST_FILE: + if image['src'].split('/')[1].split('.')[0] == latestDwnFile: STOP = True - if PAGE == 0: + if imgPage == 0: if NEXT_LATEST is None: NEXT_LATEST = str(image['src'].split('/')[1].split('.')[0]) - with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'w+') as f: + with open('%s/.stanis-tits.latest' % dwnImageDir, 'w+') as f: f.write(NEXT_LATEST) - if not os.path.exists('%s/%s' % (IMAGES_DIR, + if not os.path.exists('%s/%s' % (dwnImageDir, image['src'].split('/')[1],)): print('\tDownload %s' % image['src'].split('/')[1]) response = requests.get('http://blog.stanis.ru/%s' % image['src'], stream=True) - with open('%s/%s' % (IMAGES_DIR, image['src'].split('/')[1]), + with open('%s/%s' % (dwnImageDir, image['src'].split('/')[1]), 'wb') as out_image: shutil.copyfileobj(response.raw, out_image,) - PAGE += 1 + imgPage += 1