Updated pytrends dependency in config files and remove temp workaround (#4989)

* Updated dependency `pytrends>=4.9.2`

* Updated `requirements` & `requirements-full` files

* Removed temp workaround

* `ruff`
This commit is contained in:
Pratyush Shukla
2023-05-09 10:30:43 +05:30
committed by GitHub
parent 1a150b0dbc
commit d86e5cc9c4
5 changed files with 12 additions and 42 deletions

View File

@@ -4,7 +4,6 @@ __docformat__ = "numpy"
import logging
import pandas as pd
import requests
from pytrends.request import TrendReq
from openbb_terminal.decorators import log_start_end
@@ -28,8 +27,7 @@ def get_mentions(symbol: str) -> pd.DataFrame:
Dataframe of interest over time
"""
try:
# pytrend = TrendReq()
pytrend = _fetch_data()
pytrend = TrendReq()
pytrend.build_payload(kw_list=[symbol])
return pytrend.interest_over_time()
@@ -58,8 +56,7 @@ def get_regions(symbol: str) -> pd.DataFrame:
"""
try:
# pytrend = TrendReq()
pytrend = _fetch_data()
pytrend = TrendReq()
pytrend.build_payload(kw_list=[symbol])
return pytrend.interest_by_region().sort_values([symbol], ascending=False)
@@ -89,8 +86,7 @@ def get_queries(symbol: str, limit: int = 10) -> pd.DataFrame:
Dataframe of related queries
"""
try:
# pytrend = TrendReq()
pytrend = _fetch_data()
pytrend = TrendReq()
pytrend.build_payload(kw_list=[symbol])
df = pytrend.related_queries()
df = df[symbol]["top"].head(limit)
@@ -123,8 +119,7 @@ def get_rise(symbol: str, limit: int = 10) -> pd.DataFrame:
Dataframe containing rising related queries
"""
try:
# pytrend = TrendReq()
pytrend = _fetch_data()
pytrend = TrendReq()
pytrend.build_payload(kw_list=[symbol])
df = pytrend.related_queries()
df = df[symbol]["rising"].head(limit)
@@ -137,28 +132,3 @@ def get_rise(symbol: str, limit: int = 10) -> pd.DataFrame:
console.print(f"[red]{str(e)}[/red]\n")
return pd.DataFrame()
@log_start_end(log=logger)
def _fetch_data() -> TrendReq:
"""Fetch data from pytrends api. [Source: google trends].
Creates a requests session and fetches the cookie from the
google trends website. This cookie is then used to create a
TrendReq object.
Returns
-------
TrendReq
TrendReq object
"""
session = requests.Session()
session.get("https://trends.google.com")
cookies_map = session.cookies.get_dict()
nid_cookie = cookies_map["NID"]
return TrendReq(
hl="en-US",
tz=360,
retries=3,
requests_args={"headers": {"Cookie": f"NID={nid_cookie}"}},
)

10
poetry.lock generated vendored
View File

@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.4.2 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.4.0 and should not be changed by hand.
[[package]]
name = "absl-py"
@@ -6400,14 +6400,14 @@ test = ["cloudpickle (>=1.3)", "codecov (>=2.1)", "coverage (>=6.4)", "flake8 (>
[[package]]
name = "pytrends"
version = "4.9.1"
version = "4.9.2"
description = "Pseudo API for Google Trends"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "pytrends-4.9.1-py3-none-any.whl", hash = "sha256:fab9dd7f26fe2e6cfff722b7b5fbb093872d0bcb74d860a980f1ae83097e8f52"},
{file = "pytrends-4.9.1.tar.gz", hash = "sha256:9228055d743b00500b4670c5078c8785436a18d79082b354f7a8e57102ea82d1"},
{file = "pytrends-4.9.2-py3-none-any.whl", hash = "sha256:d7d0ee956be2f6e3e9fc09376c4615efb9347039d6d1f46e6f4326a5b7a14f67"},
{file = "pytrends-4.9.2.tar.gz", hash = "sha256:691c6e36b1aeaa4754f3692bdbad0dff446e528ffb052eee2e7f139aaa2c6989"},
]
[package.dependencies]
@@ -9692,4 +9692,4 @@ optimization = ["Riskfolio-Lib"]
[metadata]
lock-version = "2.0"
python-versions = "^3.8,<3.11, !=3.9.7"
content-hash = "29f4161609dbefbe373d25a2f561b1a3d37072236e805195150c802e4eb4108f"
content-hash = "ac38f4768093fffb5de6132acb0137b32303ec6157f0de07eb00039e4adf3cb6"

2
pyproject.toml vendored
View File

@@ -29,7 +29,7 @@ yfinance = "^0.2.12"
pmaw = "^3.0.0"
praw = "^7.1.4"
Quandl = "^3.6.0"
pytrends = "^4.9.1"
pytrends = "4.9.2"
matplotlib = "^3.3.3"
plotly = "^5.0.0"
prompt-toolkit = "^3.0.16"

View File

@@ -261,7 +261,7 @@ python-dateutil==2.8.2 ; python_version >= "3.8" and python_full_version != "3.9
python-dotenv==0.19.2 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
python-i18n==0.3.9 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytorch-lightning==1.6.5 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytrends==4.9.1 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytrends==4.9.2 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytz-deprecation-shim==0.1.0.post0 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytz==2022.7.1 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pywin32-ctypes==0.2.0 ; python_version >= "3.8" and python_version < "3.11" and python_full_version != "3.9.7" and sys_platform == "win32"

2
requirements.txt vendored
View File

@@ -179,7 +179,7 @@ python-coinmarketcap==0.2 ; python_version >= "3.8" and python_full_version != "
python-dateutil==2.8.2 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
python-dotenv==0.19.2 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
python-i18n==0.3.9 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytrends==4.9.1 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytrends==4.9.2 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytz-deprecation-shim==0.1.0.post0 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pytz==2022.7.1 ; python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"
pywin32==305 ; sys_platform == "win32" and platform_python_implementation != "PyPy" and python_version >= "3.8" and python_full_version != "3.9.7" and python_version < "3.11"