-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathap-uploadmapping.py
More file actions
executable file
·63 lines (49 loc) · 1.66 KB
/
ap-uploadmapping.py
File metadata and controls
executable file
·63 lines (49 loc) · 1.66 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
#!/usr/bin/env python3
from dotenv import load_dotenv
load_dotenv()
import os
import logging
import argparse
import pugsql
import json
import pandas as pd
from uuid import UUID
from articleparser.drive import GoogleDrive
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
logger = logging.getLogger(__name__)
queries = pugsql.module("queries")
def create_producer_id_mapping(queries):
d = list(queries.get_producers())
df = pd.DataFrame(d)[["name", "url", "producer_id"]]
df["producer_id"] = df["producer_id"].apply(lambda pid: str(UUID(pid)))
df.to_csv("producers_id.csv", index=False)
def create_public_file_id_mapping(queries):
d = queries.get_drive_by_name(name="public")
data = json.loads(d["data"])
producer_months = data["files"]["producers"]
json.dump(producer_months, open("public_file_mapping.json", "w"))
def main(args):
queries.connect(os.getenv("DB_URL"))
create_producer_id_mapping(queries)
create_public_file_id_mapping(queries)
# upload to gdrive
drive = GoogleDrive("", "", "{}", args.service_account)
parent_dir_id = os.getenv("GDRIVE_PUBLIC_DATASETS_ID")
drive.upload(
parent_dir_id,
"public_file_mapping.json",
file_id=os.getenv("GDRIVE_PUBLIC_FILE_MAPPING_ID"),
)
drive.upload(
parent_dir_id, "producers_id.csv", file_id=os.getenv("GDRIVE_PRODUCERS_CSV_ID")
)
queries.disconnect()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--service-account",
help="Service account file to access Google Drive",
default="service.json",
)
args = parser.parse_args()
main(args)