2024-01-31 00:48:20 +00:00
|
|
|
import asyncio
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
import jinja2
|
2024-10-29 09:47:34 +00:00
|
|
|
from database_lib import SQLiteCacheBackend
|
2024-01-31 00:48:20 +00:00
|
|
|
from loguru import logger
|
2024-09-21 15:41:38 +00:00
|
|
|
from medium_parser.api import MediumApi
|
2024-01-31 00:48:20 +00:00
|
|
|
from medium_parser.core import MediumParser
|
|
|
|
|
|
|
|
|
|
jinja2_env = jinja2.Environment(
|
|
|
|
|
loader=jinja2.FileSystemLoader("./"),
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-21 15:41:38 +00:00
|
|
|
|
2024-01-31 00:48:20 +00:00
|
|
|
async def safe_main():
|
|
|
|
|
try:
|
|
|
|
|
await main()
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
logger.exception(ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2024-09-21 15:41:38 +00:00
|
|
|
medium_api = MediumApi(timeout=8)
|
2024-01-31 00:48:20 +00:00
|
|
|
logger.remove()
|
2024-07-07 06:56:46 +00:00
|
|
|
logger.add(sys.stderr, level="INFO")
|
|
|
|
|
# logger.add(sys.stderr, level="TRACE")
|
|
|
|
|
logger.add("trace.log", level="TRACE")
|
2024-01-31 00:48:20 +00:00
|
|
|
|
|
|
|
|
# dl = await MediumParser.from_url("")
|
2024-07-07 06:56:46 +00:00
|
|
|
sqlite = SQLiteCacheBackend("test_db.sqlite")
|
|
|
|
|
sqlite.init_db()
|
2024-09-21 15:41:38 +00:00
|
|
|
dl = MediumParser(sqlite, medium_api, 8, "localhost")
|
2024-10-29 09:47:34 +00:00
|
|
|
query_result = await dl.query("a079819bb465", use_cache=False)
|
2024-01-31 00:48:20 +00:00
|
|
|
|
|
|
|
|
with open("query_result.json", "w") as f:
|
|
|
|
|
json.dump(query_result, f, indent=2)
|
|
|
|
|
|
2024-10-29 09:47:34 +00:00
|
|
|
result = await dl.render_as_html("a079819bb465")
|
2024-01-31 00:48:20 +00:00
|
|
|
|
|
|
|
|
with open("medium.html", "w") as f:
|
|
|
|
|
template = jinja2_env.get_template("example_base_template.html")
|
|
|
|
|
template_result = template.render(body_template=result.data)
|
|
|
|
|
f.write(template_result)
|
|
|
|
|
|
|
|
|
|
print("See medium.html for the result. Press CTRL-C to exit.")
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
asyncio.run(safe_main())
|