/
util.py
49 lines (34 loc) · 1.25 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
import os
import pickle
from typing import Optional
import requests
logging.basicConfig(
level=os.environ.get("LOGLEVEL", logging.INFO),
format="%(asctime)s %(name)s - [%(levelname)s] > %(message)s",
)
logger = logging.getLogger("feedcooker")
cache_folder = "./downloads"
def url_to_valid_filename(url: str) -> str:
return url.replace("/", "_").replace(":", "_").replace("?", "_").replace(" ", "_")
def try_load_resp(url: str) -> Optional[requests.Response]:
key = url_to_valid_filename(url)
try:
with open(os.path.join(cache_folder, key), "rb") as f:
return pickle.load(f)
except FileNotFoundError:
return None
def save_resp(resp: requests.Response):
os.makedirs(cache_folder, exist_ok=True)
key = url_to_valid_filename(resp.url)
with open(os.path.join(cache_folder, key), "wb") as f:
pickle.dump(resp, f)
def put_github_action_env(key: str, value: str):
env_file = os.getenv('GITHUB_ENV')
if env_file is None:
raise Exception("GITHUB_ENV is not set")
with open(env_file, "a") as f:
f.write(f"{key}<<EOF\n{value}\nEOF\n")
def xml_escape(s: str) -> str:
from xml.sax.saxutils import escape
return escape(s, {"'": "'", '"': """})