Skip to content

Commit

Permalink
Merge pull request vterron#19 from TheSnoozer/provide-full-user-agent
Browse files Browse the repository at this point in the history
Set a random full user agent
  • Loading branch information
vterron authored May 20, 2022
2 parents ae61554 + a46af97 commit 9019e98
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/fear_and_greed/cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@
import requests
import requests_cache

from random import choice

requests_cache.install_cache(
cache_name=os.path.join(tempfile.gettempdir(), "cnn_cache"),
expire_after=datetime.timedelta(minutes=1),
)

URL = "https://production.dataviz.cnn.io/index/fearandgreed/graphdata"

USER_AGENTS = [
# Chrome on Windows 10
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
# Chrome on macOS
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
# Chrome on Linux
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
# Firefox on Windows
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0",
# Firefox on Macos
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12.4; rv:100.0) Gecko/20100101 Firefox/100.0",
]


class FearGreedIndex(typing.NamedTuple):
value: float
Expand All @@ -26,8 +41,9 @@ class Fetcher:
"""Fetcher gets the HTML contents of CNN's Fear & Greed Index website."""

def __call__(self) -> dict:
user_agent = choice(USER_AGENTS)
headers = {
"User-Agent": "Mozilla",
"User-Agent": user_agent,
}
r = requests.get(URL, headers=headers)
r.raise_for_status()
Expand Down

0 comments on commit 9019e98

Please sign in to comment.