|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import sys |
| 4 | +import subprocess |
| 5 | +import json |
| 6 | +from sqlalchemy import create_engine |
| 7 | +from sqlalchemy.ext.declarative import declarative_base |
| 8 | +from sqlalchemy import Column, Integer, String |
| 9 | +from sqlalchemy.orm import sessionmaker |
| 10 | + |
| 11 | +engine = create_engine('mysql+oursql://root@localhost/legiinternet') |
| 12 | +Base = declarative_base() |
| 13 | +Session = sessionmaker(bind=engine) |
| 14 | + |
| 15 | + |
| 16 | +class Page(Base): |
| 17 | + __tablename__ = 'pages' |
| 18 | + |
| 19 | + uid = Column(Integer, primary_key=True) |
| 20 | + title = Column(String) |
| 21 | + |
| 22 | + |
| 23 | +class Content(Base): |
| 24 | + __tablename__ = 'tt_content' |
| 25 | + |
| 26 | + uid = Column(Integer, primary_key=True) |
| 27 | + bodytext = Column(String) |
| 28 | + |
| 29 | + |
| 30 | +class UrlCache(Base): |
| 31 | + __tablename__ = 'tx_realurl_urlencodecache' |
| 32 | + |
| 33 | + url_hash = Column(String, primary_key=True) |
| 34 | + page_id = Column(Integer) |
| 35 | + content = Column(String) |
| 36 | + |
| 37 | + |
| 38 | +session = Session() |
| 39 | + |
| 40 | + |
| 41 | +def get_data(url): |
| 42 | + page_id = ( |
| 43 | + session.query(UrlCache.page_id) |
| 44 | + .filter_by(content=url) |
| 45 | + .scalar() |
| 46 | + ) |
| 47 | + title = session.query(Page).get(page_id).title |
| 48 | + bodytext = session.query(Content).get(page_id).bodytext |
| 49 | + return (title, bodytext) |
| 50 | + |
| 51 | + |
| 52 | +def export(url): |
| 53 | + (title, bodytext) = get_data(url) |
| 54 | + folder = url.rsplit('/', 1)[0] |
| 55 | + assert '..' not in folder |
| 56 | + subprocess.check_call(['mkdir', '-p', folder]) |
| 57 | + with open(url, 'wb') as f: |
| 58 | + f.write('---\n') |
| 59 | + f.write('title: ' + json.dumps(title) + '\n') |
| 60 | + f.write('---\n') |
| 61 | + f.write(bodytext.encode('utf-8')) |
| 62 | + |
| 63 | + |
| 64 | +export(sys.argv[1]) |
0 commit comments