2016-10-28 00:29:36 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
import os.path
|
2016-11-03 12:41:27 +03:00
|
|
|
try:
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
except ImportError:
|
|
|
|
import sys
|
|
|
|
sys.path.append('./.pip')
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
2016-10-28 00:29:36 +03:00
|
|
|
|
|
|
|
__author__ = 'Alexander Popov'
|
2016-11-03 12:41:27 +03:00
|
|
|
__version__ = '0.1.0'
|
2016-10-28 00:29:36 +03:00
|
|
|
__license__ = 'Unlicense'
|
|
|
|
|
|
|
|
IMAGES_DIR = './images'
|
|
|
|
COOKIES = dict(block='951')
|
|
|
|
URL = 'http://blog.stanis.ru/?back=%d'
|
|
|
|
PAGE = 0
|
2016-11-03 12:41:27 +03:00
|
|
|
if not os.path.exists('%s/.stanis-tits.latest' % IMAGES_DIR):
|
|
|
|
if not os.path.exists('%s' % IMAGES_DIR):
|
|
|
|
os.mkdir('%s' % IMAGES_DIR)
|
|
|
|
|
|
|
|
with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'w') as f:
|
|
|
|
f.write('0')
|
2016-10-28 00:29:36 +03:00
|
|
|
with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'r') as f:
|
|
|
|
LATEST_FILE = f.read()
|
|
|
|
STOP = False
|
|
|
|
NEXT_LATEST = None
|
|
|
|
|
2016-11-03 12:41:27 +03:00
|
|
|
while STOP is False:
|
2016-10-28 00:29:36 +03:00
|
|
|
print('Loading page %d' % PAGE)
|
|
|
|
|
|
|
|
r = requests.get(URL % PAGE, cookies=COOKIES)
|
|
|
|
soup = BeautifulSoup(r.text.encode('cp1251'),
|
2016-11-03 12:41:27 +03:00
|
|
|
"html.parser", from_encoding="windows-1251")
|
2016-10-28 00:29:36 +03:00
|
|
|
images = soup.findAll('img', src=re.compile('img/*'))
|
|
|
|
|
|
|
|
for image in images:
|
2016-10-28 01:15:49 +03:00
|
|
|
if image['src'].split('/')[1].split('.')[0] == LATEST_FILE:
|
2016-10-28 00:29:36 +03:00
|
|
|
STOP = True
|
|
|
|
|
|
|
|
if PAGE == 0:
|
2016-11-03 12:41:27 +03:00
|
|
|
if NEXT_LATEST is None:
|
2016-10-28 00:29:36 +03:00
|
|
|
NEXT_LATEST = str(image['src'].split('/')[1].split('.')[0])
|
|
|
|
with open('%s/.stanis-tits.latest' % IMAGES_DIR, 'w+') as f:
|
|
|
|
f.write(NEXT_LATEST)
|
|
|
|
|
2016-11-03 12:41:27 +03:00
|
|
|
if not os.path.exists('%s/%s' % (IMAGES_DIR,
|
|
|
|
image['src'].split('/')[1],)):
|
2016-10-28 00:29:36 +03:00
|
|
|
print('\tDownload %s' % image['src'].split('/')[1])
|
2016-11-03 12:41:27 +03:00
|
|
|
response = requests.get('http://blog.stanis.ru/%s'
|
|
|
|
% image['src'], stream=True)
|
|
|
|
with open('%s/%s' % (IMAGES_DIR, image['src'].split('/')[1]),
|
|
|
|
'wb') as out_image:
|
2016-10-28 00:29:36 +03:00
|
|
|
shutil.copyfileobj(response.raw, out_image,)
|
|
|
|
|
|
|
|
PAGE += 1
|