Skip to content

Instantly share code, notes, and snippets.

View mezhgano's full-sized avatar

Dmitry Meshkoff mezhgano

View GitHub Profile
@mezhgano
mezhgano / playwright_persistent_concurrency.py
Last active May 20, 2023 12:01
Playwright persistent context concurrency
import asyncio
import random
from pathlib import Path
from playwright.async_api import BrowserContext, async_playwright
CWD = Path().resolve()
# Example path
user_data_dir = Path.joinpath(CWD, 'playwright/data_dir/chromium')
@mezhgano
mezhgano / playwright_save_mhtml.py
Created May 1, 2023 14:58
Save .mhtml web archive using playwright
from playwright.sync_api import sync_playwright
def save_mhtml(path: str, text: str):
with open(path, mode='w', encoding='UTF-8', newline='\n') as file:
file.write(text)
def save_page(url: str, path: str):
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False)
@mezhgano
mezhgano / epoch_to_datetime.py
Created April 3, 2023 11:17
Convert epoch to datetime
from datetime import datetime
def _epoch_to_datetime(self, epoch:int, strf='%Y-%m-%d_%H-%M-%S') -> str:
'Return human readable datetime string from given epoch int.'
return datetime.fromtimestamp(epoch).strftime(strf)
@mezhgano
mezhgano / set_download_path.py
Last active March 25, 2023 16:14
Script to set download path
import os
import sys
from pathlib import Path
from typing import List, Optional
def _exit(message: str, status=0):
'Gracefully exit with status code and message to stderr.'
if status != 0:
sys.stderr.write(message)
raise SystemExit(status)
@mezhgano
mezhgano / age_str_calc.py
Last active July 30, 2022 11:03
Calculate age from given date string with prefix and postfix (RU)
def age_str_calc(date: str, strp_format: str, bounds=(1, 120)) -> tuple:
'''Calulate age from given date, if age fit the bounds (default is 1-120 years),
then return string: prefix (RU) + age (in years) + postfix (RU). Otherwise return empty string.'''
def age(birthdate: object) -> int:
'''Return age (int) by given datetime object'''
today = datetime.date.today()
age = today.year - birthdate.year - (
(today.month, today.day) < (birthdate.month, birthdate.day)
)
return age