Skip to content

Commit a80ebb0

Browse files
committed
Implement .env for secrets
- replace query_secrets.py with .env for secrets management - add example.env for APIs that require client credentials - make necessary path update
1 parent 53b477c commit a80ebb0

10 files changed

Lines changed: 47 additions & 26 deletions

File tree

deviantart/deviantart_scratcher.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212

1313
# Third-party
1414
import pandas as pd
15-
import query_secrets
1615
import requests
16+
from dotenv import load_dotenv
1717
from requests.adapters import HTTPAdapter
1818
from urllib3.util.retry import Retry
1919

20+
CWD = os.path.dirname(os.path.abspath(__file__))
21+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
22+
load_dotenv(dotenv_path)
23+
2024
today = dt.datetime.today()
21-
API_KEYS = query_secrets.API_KEYS
25+
API_KEYS = os.getenv("API_KEYS").split(",")
2226
API_KEYS_IND = 0
23-
CWD = os.path.dirname(os.path.abspath(__file__))
2427
DATA_WRITE_FILE = (
2528
f"{CWD}" f"/data_deviantart_{today.year}_{today.month}_{today.day}.csv"
2629
)
27-
PSE_KEY = query_secrets.PSE_KEY
30+
PSE_KEY = os.getenv("PSE_KEY")
2831

2932

3033
def get_license_list():
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# "Custom Search JSON API requires the use of an API key. An API key is a way
22
# to identify your client to Google."
33
# https://developers.google.com/custom-search/v1/introduction
4-
API_KEYS = []
4+
# API_KEYS = key1,key2
5+
56
# "The identifier of an engine created using the Programmable Search Engine
67
# Control Panel [https://programmablesearchengine.google.com/about/]"
78
# https://developers.google.com/custom-search/v1/reference/rest/v1/Search
8-
PSE_KEY = ""
9+
# PSE_KEY =

flickr/photos.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Standard library
22
import json
3+
import os
34
import os.path
45
import sys
56
import traceback
67

78
# Third-party
89
import flickrapi
9-
import query_secrets
10+
from dotenv import load_dotenv
1011

1112
CWD = os.path.dirname(os.path.abspath(__file__))
13+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
14+
load_dotenv(dotenv_path)
1215

1316

1417
def main():
1518
flickr = flickrapi.FlickrAPI(
16-
query_secrets.api_key, query_secrets.api_secret, format="json"
19+
os.getenv("API_KEY"), os.getenv("API_SECRET"), format="json"
1720
)
1821

1922
# use search method to pull general photo info under each cc license data

flickr/photos_detail.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Standard library
1111
import json
12+
import os
1213
import os.path
1314
import sys
1415
import time
@@ -17,9 +18,12 @@
1718
# Third-party
1819
import flickrapi
1920
import pandas as pd
20-
import query_secrets
21+
from dotenv import load_dotenv
2122

2223
CWD = os.path.dirname(os.path.abspath(__file__))
24+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
25+
load_dotenv(dotenv_path)
26+
2327
RETRIES = 0
2428

2529

@@ -149,7 +153,7 @@ def main():
149153
hs_csv_path = os.path.join(CWD, "hs.csv")
150154

151155
flickr = flickrapi.FlickrAPI(
152-
query_secrets.api_key, query_secrets.api_secret, format="json"
156+
os.getenv("API_KEY"), os.getenv("API_SECRET"), format="json"
153157
)
154158
# below is the cc licenses list
155159
license_list = [1, 2, 3, 4, 5, 6, 9, 10]
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# "Custom Search JSON API requires the use of an API key. An API key is a way
22
# to identify your client to Google."
33
# https://developers.google.com/custom-search/v1/introduction
4-
API_KEYS = []
4+
# API_KEYS = key1,key2
5+
56
# "The identifier of an engine created using the Programmable Search Engine
67
# Control Panel [https://programmablesearchengine.google.com/about/]"
78
# https://developers.google.com/custom-search/v1/reference/rest/v1/Search
8-
PSE_KEY = ""
9+
# PSE_KEY =

google_custom_search/google_scratcher.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212

1313
# Third-party
1414
import pandas as pd
15-
import query_secrets
1615
import requests
16+
from dotenv import load_dotenv
1717
from requests.adapters import HTTPAdapter
1818
from urllib3.util.retry import Retry
1919

20+
CWD = os.path.dirname(os.path.abspath(__file__))
21+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
22+
load_dotenv(dotenv_path)
23+
2024
today = dt.datetime.today()
21-
API_KEYS = query_secrets.API_KEYS
25+
API_KEYS = os.getenv("API_KEYS").split(",")
2226
API_KEYS_IND = 0
23-
CWD = os.path.dirname(os.path.abspath(__file__))
2427
DATA_WRITE_FILE = (
2528
f"{CWD}"
2629
f"/data_google_custom_search_{today.year}_{today.month}_{today.day}.csv"
@@ -36,7 +39,7 @@
3639
f"{today.year}_{today.month}_{today.day}.csv"
3740
)
3841
SEARCH_HALFYEAR_SPAN = 20
39-
PSE_KEY = query_secrets.PSE_KEY
42+
PSE_KEY = os.getenv("PSE_KEY")
4043

4144

4245
def get_license_list():
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# mobile application, a dynamic web page, or a three-line script. If it's
44
# making API calls, it's an app."
55
# https://developer.vimeo.com/api/guides/start#register-your-app
6-
ACCESS_TOKEN = ""
7-
CLIENT_ID = ""
6+
# ACCESS_TOKEN =
7+
# CLIENT_ID =

vimeo/vimeo_scratcher.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
import traceback
1515

1616
# Third-party
17-
import query_secrets
1817
import requests
18+
from dotenv import load_dotenv
1919
from requests.adapters import HTTPAdapter
2020
from urllib3.util.retry import Retry
2121

22-
today = dt.datetime.today()
23-
ACCESS_TOKEN = query_secrets.ACCESS_TOKEN
24-
CLIENT_ID = query_secrets.CLIENT_ID
2522
CWD = os.path.dirname(os.path.abspath(__file__))
23+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
24+
load_dotenv(dotenv_path)
25+
26+
today = dt.datetime.today()
27+
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
28+
CLIENT_ID = os.getenv("CLIENT_ID")
2629
DATA_WRITE_FILE = (
2730
f"{CWD}" f"/data_vimeo_{today.year}_{today.month}_{today.day}.csv"
2831
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# Your API key is available in the Developer Console's API Access pane
33
# [https://console.developers.google.com/] for your project."
44
# https://developers.google.com/youtube/v3/docs
5-
API_KEY = ""
5+
# API_KEY =

youtube/youtube_scratcher.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
import traceback
1212

1313
# Third-party
14-
import query_secrets
1514
import requests
15+
from dotenv import load_dotenv
1616
from requests.adapters import HTTPAdapter
1717
from urllib3.util.retry import Retry
1818

19-
today = dt.datetime.today()
20-
API_KEY = query_secrets.API_KEY
2119
CWD = os.path.dirname(os.path.abspath(__file__))
20+
dotenv_path = os.path.join(os.path.dirname(CWD), ".env")
21+
load_dotenv(dotenv_path)
22+
23+
today = dt.datetime.today()
24+
API_KEY = os.getenv("API_KEY")
2225
DATA_WRITE_FILE = (
2326
f"{CWD}" f"/data_youtube_{today.year}_{today.month}_{today.day}.csv"
2427
)

0 commit comments

Comments
 (0)