Skip to content

Commit b125bea

Browse files
authored
feat: include custom user agent in requests (#50)
This makes it possible to identify traffic from us specifically, which can be useful for api providers with gauging our impact on their infrastructure and also (hopefully) make us seem more like a legitimate and positive consumer rather than just "yet another `python-requests/<version>` scraper"
1 parent 277ab28 commit b125bea

5 files changed

Lines changed: 17 additions & 3 deletions

File tree

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ select = [
1818
[lint.isort]
1919
known-first-party = [
2020
'generate_osv_advisories',
21+
'user_agent',
2122
'typings'
2223
]

scripts/download_sa_advisories.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import requests
1515

1616
from typings import drupal
17+
from user_agent import user_agent
1718

1819

1920
def get_most_recent_changed_timestamp() -> int:
@@ -51,7 +52,7 @@ def download_sa_advisories_from_rest_api(last_modified_timestamp: int) -> None:
5152
fetch_again = True
5253
while fetch_again:
5354
print(f'fetching {url}')
54-
response = requests.get(url)
55+
response = requests.get(url, headers={'user-agent': user_agent})
5556
if response.status_code == 200:
5657
data: drupal.ApiResponse[drupal.Advisory] = response.json()
5758
for item in data['list']:

scripts/generate_osv_advisories.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from markdownify import markdownify
1818

1919
from typings import drupal, osv
20+
from user_agent import user_agent
2021

2122

2223
def fetch_drupal_node(nid: str) -> drupal.Node:
@@ -31,7 +32,10 @@ def fetch_drupal_node(nid: str) -> drupal.Node:
3132
except FileNotFoundError as e:
3233
os.makedirs('cache/nodes', exist_ok=True)
3334
print(f' *- fetching https://www.drupal.org/api-d7/node/{nid}.json')
34-
resp = requests.get(f'https://www.drupal.org/api-d7/node/{nid}.json')
35+
resp = requests.get(
36+
f'https://www.drupal.org/api-d7/node/{nid}.json',
37+
headers={'user-agent': user_agent},
38+
)
3539

3640
if resp.status_code == 200:
3741
node: drupal.Node = resp.json()

scripts/precache_nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import requests
1414

1515
from typings import drupal
16+
from user_agent import user_agent
1617

1718

1819
def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
@@ -24,7 +25,7 @@ def fetch_drupal_nodes(nids: list[str]) -> list[drupal.Node]:
2425
for nid in nids:
2526
url += f'nid[]={nid}&'
2627

27-
resp = requests.get(url)
28+
resp = requests.get(url, headers={'user-agent': user_agent})
2829

2930
if resp.status_code == 200:
3031
items = typing.cast(drupal.ApiResponse[drupal.Node], resp.json())['list']

scripts/user_agent.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
user_agent = 'drupal-advisory-database/'
4+
if 'CI' in os.environ:
5+
user_agent += 'ci'
6+
else:
7+
user_agent += 'local'

0 commit comments

Comments
 (0)