-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.py
55 lines (41 loc) · 1.36 KB
/
upload.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
50
51
52
53
54
55
import glob
import logging
import os
from bln import Client
def main():
"""Upload the provided files to biglocalnews.org."""
# Get env variables
api_key = os.getenv("api-key")
project_id = os.getenv("project-id")
path = os.getenv("path")
# Set the logger
logging.basicConfig(
level="DEBUG",
format="%(asctime)s - %(name)s - %(message)s"
)
logger = logging.getLogger(__name__)
# Upload files
client = Client(api_key)
# If the path is a directory ...
if path.endswith("/"):
# Get all the CSV files in the directory
file_list = glob.glob(path + "**/*.csv", recursive=True)
# Make sure we have at least one
if not file_list:
raise ValueError("No CSV file found in directory.")
# Upload the files
logger.debug(f"Uploading {len(file_list)} files")
client.upload_files(project_id, file_list)
# If a single file ...
else:
# Make sure it's a CSV
if not path.lower().endswith("csv"):
raise ValueError("Files must be CSVs")
# Make sure the path exists
if not os.path.exists(path):
raise ValueError(f"File path does not exist {path}")
# Upload the file
logger.debug(f"Uploading {path}")
client.upload_file(project_id, path)
if __name__ == '__main__':
main()