This repository was archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
64 lines (44 loc) · 1.35 KB
/
utilities.py
File metadata and controls
64 lines (44 loc) · 1.35 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import unicodedata
import string
import requests as r
import os
import shutil
import config as cfg
from dataclasses import dataclass
@dataclass
class MessageStruct:
msg_type: str
msg_cont: object
msg_catg: list
def get_file(file):
response = r.get(
url=file['url_private_download'],
headers={
"Authorization": f"Bearer {os.environ['SLACK_API_TOKEN']}"
},
allow_redirects=False
)
path = f'tmp/file/{file["timestamp"]}.{file["filetype"]}'
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'wb') as i:
i.write(response.content)
return path
def cleanup():
if os.path.isdir('tmp'):
shutil.rmtree('tmp')
def post(url, file=None, params=None, headers=None, body=None):
response = r.post(url=url, files=file, params=params, headers=headers, data=body).json()
assert response['ok']
return response
def safe_format(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
return re.sub(r'[^\w\s-]', '', value.decode().strip())
def remove_directory_and_contents(directory):
if not os.path.isdir(directory):
return
shutil.rmtree(directory, ignore_errors=False)