-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_data.py
More file actions
executable file
·110 lines (98 loc) · 3.72 KB
/
store_data.py
File metadata and controls
executable file
·110 lines (98 loc) · 3.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import inspect
import datetime as dt
from pathlib import Path
from ccaoa import raccoon as rc
try:
from . import dma
except ImportError:
import dma
def retrieve_variable_name(var, exclude_vars: list = None):
"""Return the name of the variable passed as a string.
Courtesy of https://stackoverflow.com/a/18425523/15517267"""
# TODO source from ccaoa package. See https://github.com/ccaoa/google-ece-trends/issues/21
if exclude_vars is None:
exclude_vars = []
callers_local_vars = inspect.currentframe().f_back.f_locals.items()
return [
var_name
for var_name, var_val in callers_local_vars
if (
var_val is var
and not var_name.startswith("__")
and var_name not in exclude_vars
)
][0]
def date_from_searchperiod(search_period_date):
"""Use DateTime to extract the date of the search period."""
# Input Format EX: '2020-02-14 2021-02-14'
firstdate = search_period_date[:10].replace("-", "")
seconddate = search_period_date[11:21].replace("-", "")
date_range = firstdate + "-" + seconddate
return date_range
def get_storage_path():
"""Dynamically define the storage path with an external file that you gitignore.
Keeps one from having to constantly edit their file paths in-code if working on different machines.
"""
current_directory = dma.dma_module_directory()
dot_storage_path = os.path.join(current_directory, ".storage_path")
if not os.path.exists(dot_storage_path):
Path(dot_storage_path).touch()
with open(dot_storage_path) as sfile:
path_store = str(sfile.read())
# Make sure there are no pythonic quotations, etc around the path.
path_store = path_store.replace('r"', '"').replace('"', "")
if not os.path.exists(path_store):
print(
"Your file",
path_store,
"doesn't exist.\n"
"Edit your `.storage_path` file in this directory to designate a destination for the Google Trends data.",
)
# # Maybe in a future version, add a user input method to manually define this var in-run.
sfile.close()
return path_store
def store_data(
storage_directory_file_path,
gtrends_data,
search_date_period,
gtrends_file_name=None,
csv_not_xlsx=True,
suppress_prints=False,
):
"""Store the GTrends data you just pulled."""
# Get the name of the variable for downstream storage naming metadata.
if gtrends_file_name is None:
dataset_name = retrieve_variable_name(gtrends_data)
else:
dataset_name = gtrends_file_name
date_range = date_from_searchperiod(search_date_period)
today = dt.datetime.today().strftime("%Y%m%d")
if csv_not_xlsx is True:
ext = ".csv"
else:
ext = ".xlsx"
file_name = dataset_name + "_" + date_range + "_" + today + ext
file_path = os.path.join(storage_directory_file_path, file_name)
rc.df_to_file(gtrends_data, file_path)
if suppress_prints is not True:
short_path = os.path.join(
"~",
os.path.split(
os.path.split(os.path.split(storage_directory_file_path)[0])[0]
)[1],
os.path.split(os.path.split(storage_directory_file_path)[0])[1],
os.path.split(storage_directory_file_path)[1],
)
tidy_today = str(dt.datetime.today().strftime("%d %b"))
print(
dataset_name + ext,
"was stored for time period",
search_date_period,
"on",
tidy_today,
"in",
short_path,
".",
)
return file_path