Skip to content

Commit a8434f4

Browse files
authored
Merge pull request #139 from creativecommons/github-auth
Add authentication to GitHub fetch script
2 parents 146d911 + 0d769d0 commit a8434f4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

.github/workflows/1-fetch.yml

+2
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ jobs:
6565
run: |
6666
./scripts/1-fetch/github_fetch.py \
6767
--enable-save --enable-git
68+
env:
69+
GH_TOKEN: ${{ secrets.BOT_TOKEN }}

env.example

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# This file must be copied to .env and the appropriate variables populated.
22

33

4+
# GitHub
5+
6+
# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api#primary-rate-limit-for-authenticated-users
7+
#
8+
# https://docs.github.com/en/rest/authentication/authenticating-to-the-rest-api
9+
10+
# GH_TOKEN =
11+
12+
413
## GCS (Google Custom Search)
514

615
# https://developers.google.com/custom-search/v1/introduction
@@ -21,7 +30,9 @@
2130

2231
# GCS_CX =
2332

33+
2434
## Flickr
35+
2536
# "The flickr developer guide: https://www.flickr.com/services/developer/"
2637

2738
# FLICKR_API_KEY =

scripts/1-fetch/github_fetch.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232
# Constants
3333
FILE1_COUNT = os.path.join(PATHS["data_phase"], "github_1_count.csv")
34+
GH_TOKEN = os.getenv("GH_TOKEN")
3435
GITHUB_RETRY_STATUS_FORCELIST = [
3536
408, # Request Timeout
36-
422, # Unprocessable Content
37-
# (Validation failed, or the endpoint has been spammed)
37+
422, # Unprocessable Content (Validation failed, or endpoint spammed)
3838
429, # Too Many Requests
3939
500, # Internal Server Error
4040
502, # Bad Gateway
@@ -94,7 +94,10 @@ def get_requests_session():
9494
)
9595
session = requests.Session()
9696
session.mount("https://", HTTPAdapter(max_retries=max_retries))
97-
session.headers.update({"Accept": "application/vnd.github+json"})
97+
headers = {"accept": "application/vnd.github+json"}
98+
if GH_TOKEN:
99+
headers["authorization"] = f"Bearer {GH_TOKEN}"
100+
session.headers.update(headers)
98101

99102
return session
100103

0 commit comments

Comments
 (0)