Skip to content

Instantly share code, notes, and snippets.

@therealkenc
Last active May 27, 2024 05:40
Show Gist options
  • Save therealkenc/a0c9dc78709730e72fff5ac3cc79ef54 to your computer and use it in GitHub Desktop.
Save therealkenc/a0c9dc78709730e72fff5ac3cc79ef54 to your computer and use it in GitHub Desktop.
tv fetch events
import http.client
import json
import gzip
import io
import pandas as pd
from urllib.parse import urlencode
# Headers found experimentally to work from a web traffic trace
headers = {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
"Cache-Control": "no-cache",
"Origin": "https://www.tradingview.com",
"Pragma": "no-cache",
"Referer": "https://www.tradingview.com/",
"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36"
}
def construct_path(from_date, to_date, countries):
query_params = {
'from': from_date,
'to': to_date,
'countries': ','.join(countries)
}
return "/events?" + urlencode(query_params)
from_date = '2024-05-27T03:00:00.000Z'
to_date = '2024-06-26T03:00:00.000Z'
countries = ['AR', 'AU', 'BR', 'CA', 'CN', 'FR', 'DE', 'IN', 'ID', 'IT', 'JP', 'KR', 'MX', 'RU', 'SA', 'ZA', 'TR', 'GB', 'US', 'EU']
path = construct_path(from_date, to_date, countries)
url = "economic-calendar.tradingview.com"
conn = http.client.HTTPSConnection(url)
conn.request("GET", path, headers=headers)
response = conn.getresponse()
status_code = response.status
content = response.read()
if status_code == 200:
encoding = response.getheader('Content-Encoding')
if encoding == 'gzip':
buf = io.BytesIO(content)
with gzip.GzipFile(fileobj=buf) as f:
content = f.read()
content = content.decode('utf-8')
json_content = json.loads(content)
data = json_content.get('result', [])
df = pd.DataFrame(data, columns=['id', 'title', 'date', 'actual', 'previous', 'forecast'])
df['date'] = pd.to_datetime(df['date'])
df['date'] = df['date'].dt.strftime('%b-%d %H:%M')
df = df.fillna('-')
print(df)
else:
print(f"Failed to fetch data: {status_code}")
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment