Created
October 31, 2021 22:09
-
-
Save meyer1994/61b15233f4bb9921acb63bdbc1936978 to your computer and use it in GitHub Desktop.
Simple client to search for tickers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import json | |
from enum import Enum | |
from typing import NamedTuple | |
from datetime import datetime | |
from functools import cache | |
from argparse import ArgumentParser | |
import multiprocessing as mp | |
import httpx | |
from bs4 import BeautifulSoup | |
@cache | |
def _search(comp: str) -> tuple: | |
url = 'https://www.marketscreener.com/mods_a/search/findRapidSearch.php' | |
params = {'company_name': comp } | |
res = httpx.get(url, params=params) | |
html = BeautifulSoup(res.text, 'html.parser') | |
class Result(NamedTuple): | |
mid: str | |
name: str | |
ticker: str | |
mid = html.select_one('.AC_link') | |
mid, *_ = re.findall(r'-(\d+)/', mid.text, re.IGNORECASE) | |
name = html.select_one('.AC_kw1').text | |
ticker = html.select_one('b').text | |
return Result(mid, name, ticker) | |
def _fetch(code: str, start: int, end: int) -> dict: | |
url = 'https://www.zonebourse.com/mods_a/charts/TV/function/history' | |
params = { 'symbol': code, 'from': start, 'to': end, 'resolution': 'D' } | |
res = httpx.get(url, params=params) | |
return res.json() | |
def fetch(ticker: str, start: datetime, end: datetime) -> dict: | |
mid, _, _ = _search(ticker) | |
start = start.timestamp() | |
start = int(start) | |
end = end.timestamp() | |
end = int(end) | |
return _fetch(mid, start, end) | |
def download(ticker: str) -> dict: | |
print(ticker) | |
start = datetime(2020, 1, 1) | |
end = datetime.today() | |
try: | |
data = fetch(ticker, start, end) | |
except Exception as e: | |
print(e) | |
return | |
with open(f'{ticker}.json', 'wt') as f: | |
json.dump(data, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment