Files
videoreg.ru/scripts/rebuild-v2/recover-sample-gallery.py
T

30 lines
2.3 KiB
Python

from pathlib import Path
from urllib.parse import urljoin,urlsplit
import sys,json,hashlib,concurrent.futures
from bs4 import BeautifulSoup
sys.path.insert(0,str(Path(__file__).parent));from fetch import fetch
P=Path(__file__).resolve().parents[2];D=P/'docs/rebuild-v2';R=D/'raw';(R/'gallery').mkdir(exist_ok=True)
root=BeautifulSoup((R/'narod-samples-live.html').read_bytes(),'html.parser')
albums=[dict(url='http://videoreg-samples.narod.ru/',raw='docs/rebuild-v2/raw/narod-samples-live.html')]
for n in range(1,5):
url='http://www.videoreg-samples.narod.ru/photoalbum'+str(n)+'.html';rec=fetch(url,'gallery/album'+str(n)+'.html',25)
if rec['httpStatus']=='200' and rec['bytes']>300:albums.append(dict(url=url,raw=rec['raw']))
images={}
for album in albums:
s=BeautifulSoup((P/album['raw']).read_bytes(),'html.parser')
album['title']=s.title.get_text(' ',strip=True) if s.title else '';album['text']=s.get_text(' ',strip=True);album['images']=[]
for tag in s.find_all(['a','img']):
value=tag.get('href') if tag.name=='a' else tag.get('src');url=urljoin(album['url'],value or '')
u=urlsplit(url)
if u.hostname in ['videoreg-samples.narod.ru','www.videoreg-samples.narod.ru'] and u.path.lower().endswith(('.jpg','.jpeg')):
key=u.path;album['images'].append(key);images.setdefault(key,dict(path=key,url=url,fetchStatus='pending',rawFile=None))
album['images']=list(dict.fromkeys(album['images']))
def work(item):
name='gallery/'+hashlib.sha256(item['path'].encode()).hexdigest()[:18]+'.jpg';rec=fetch(item['url'],name,25)
if rec['httpStatus']=='200' and (P/rec['raw']).read_bytes().startswith(b'\xff\xd8\xff'):
item.update(fetchStatus='downloaded-unreviewed',rawFile=rec['raw'],sha256=rec['sha256'],bytes=rec['bytes'])
else:item.update(fetchStatus='unresolved')
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as ex:list(ex.map(work,images.values()))
(D/'sample-gallery-inventory.json').write_text(json.dumps(dict(source='Original sample host linked by recovered videoreg homepage; live HTML contains legacy photo series, scripts not executed',albums=albums,images=list(images.values())),ensure_ascii=False,indent=2)+'\n')
print('GALLERY',len(albums),'albums',len(images),'discovered photos',sum(x['fetchStatus']=='downloaded-unreviewed' for x in images.values()),'downloaded')