Skip to content

Commit

Permalink
- packaged for pip, updated folder structure.
Browse files Browse the repository at this point in the history
- Updated README installation section and minor details.
- Exit on error. BSE.py __exit__
- Updated import statements in examples folder.
- Correction in examples/news.py for argv length check.
  • Loading branch information
BennyThadikaran committed Oct 9, 2023
1 parent a82deb1 commit 75d5a71
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 34 deletions.
60 changes: 38 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# BseIndiaApi

An unofficial python api for BSE India stock exchange.

## Installation
Python version: >= 3.6

Download or clone the repo and install the dependencies: `pip install requests`
## Install with PIP

```
pip install bse
```

## Usage

Using with statement

```python
from bse import BSE

with BSE() as bse:
code = bse.getScripCode('tcs') # 532540 bse scrip code

Expand All @@ -19,8 +26,12 @@ with BSE() as bse:

bse.quote(scrip_code) # Open, High, Low, LTP
```

or

```python
from bse import BSE

bse = BSE()

code = bse.getScripCode('tcs') # 532540 bse scrip code
Expand All @@ -31,6 +42,7 @@ bse.exit() # close the request session
```

## Sample Responses

`src/samples` contain the sample responses from the various methods in JSON format.

The files are named after the corresponding method in `src/BSE.py`. Use it to understand the API response structure.
Expand All @@ -42,49 +54,50 @@ Help on class BSE in module BSE:
class BSE(builtins.object)
| Unofficial api for BSE India
|
|
| Methods defined here:
|
|
| __init__(self)
| Initialize self. See help(type(self)) for accurate signature.
|
|
| advanceDecline(self)
| returns list of dictionary of advance decline values for each index
|
|
| allAnnouncements(self, fromDate, toDate, category='-1')
| Returns all news items by date
|
|
| allCorporateActions(self)
| returns list of dictionary of all corporate actions
|
|
| announcements(self, scripcode, fromDate, toDate)
| Return Corporate announcements by scripcode and date
|
|
| corporateActions(self, scripcode)
| Returns Corporate Actions by scripcode
|
| exit(self)
|
|
| getScripCode(self, scripname)
| returns bse symbol code for stock symbol name
|
|
| getScripName(self, scripcode)
| returns stock symbol name for bse scrip code
|
|
| listSecurities(self, industry='', scripcode='', group='', segment='Equity', status='Active')
| List all securities with their meta info or filter by industry etc.
|
|
| near52WeekHighLow(self, scripcode='', indexcode='', group='')
| Returns stocks near 52 week highs and lows
|
|
| quote(self, scripcode)
| Get OHLC quotes for given scripcode
|
|
| scripMeta(self, scripcode='', segment='Equity', status='Active', group='', industry='')
| Return scrip meta info for all stocks or filtered by industry etc
```

## CONSTANTS

```python
# Corporate Announcement categories
CATEGORY_AGM = 'AGM/EGM'
Expand Down Expand Up @@ -134,15 +147,18 @@ INDUSTRY_UTILITIES = 'Utilities'
```

## Example Folder

`src/examples` contains scripts that use the `BSE.py`. These are script i wrote some years back. All scripts incorporate color and fancy formatting to look good. 😄

To use the scripts download or clone the repo.

- **ann.py**: This is the only script i still use to date. It display all corporate announcements and corporate actions relating to your watchlist or portfolio.
- To get started, store your watchlist symbols in a text file each symbol on a new line.
- At first, execute `ann.py` passing the text file as an argument. `py ann.py watchlist.txt`. This will generate a `watchlist.json` file and print out all announcements and actions related to your watchlist.
- Next time, simply execute `py ann.py` to see announcements for the day.
- To get started, store your watchlist symbols in a text file each symbol on a new line.
- At first, execute `ann.py` passing the text file as an argument. `py ann.py watchlist.txt`. This will generate a `watchlist.json` file and print out all announcements and actions related to your watchlist.
- Next time, simply execute `py ann.py` to see announcements for the day.
- **news.py**: `news.py` simply prints the last 10 announcements for a scrip.
- Execute it as `py news.py infy`.
- You can optionally limit the results by passing a number after the symbol.
- `py news.py infy 3`. This returns the last 3 announcements.
- Execute it as `py news.py infy`.
- You can optionally limit the results by passing a number after the symbol.
- `py news.py infy 3`. This returns the last 3 announcements.
- **actions.py**: `py actions.py infy` to print the recent corporate actions. Nothing more.
- **advances.py**: `py advances.py` to print the advance decline ratios for various bse Indexes.
- **advances.py**: `py advances.py` to print the advance decline ratios for various bse Indexes.
24 changes: 24 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "bse"
version = "0.0.4"
authors = [
{ name="Benny Thadikaran" },
]
description = "Unofficial Python Api for BSE India stock exchange"
readme = "README.md"
requires-python = ">=3.6"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
]
keywords = ["bse", "bse-stock-data", "stock-market-api", "stock-news-api"]
dependencies = ["requests==2.31.0"]

[project.urls]
"Homepage" = "https://github.com/BennyThadikaran/BseIndiaApi"
"Bug Tracker" = "https://github.com/BennyThadikaran/BseIndiaApi/issues"
5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from bse import BSE
4 changes: 2 additions & 2 deletions src/BSE.py → src/bse/BSE.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from requests import Session
from requests.exceptions import ReadTimeout
from Throttle import Throttle
from .Throttle import Throttle
from re import search

throttle_config = {
Expand Down Expand Up @@ -85,7 +85,7 @@ def __enter__(self):
def __exit__(self, exc_type, exc_value, exc_traceback):
self.session.close()
if exc_type:
print(f'{exc_type}: {exc_value} | {exc_traceback}')
exit(f'{exc_type}: {exc_value} | {exc_traceback}')
return True

def exit(self):
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/bse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .BSE import BSE
2 changes: 1 addition & 1 deletion src/examples/actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from BSE import BSE
from bse import BSE
from sys import argv
from datetime import datetime

Expand Down
2 changes: 1 addition & 1 deletion src/examples/advances.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from BSE import BSE
from bse import BSE
from os import system
from sys import platform

Expand Down
2 changes: 1 addition & 1 deletion src/examples/ann.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from BSE import BSE
from bse import BSE
from datetime import datetime
from re import findall
from os import system
Expand Down
4 changes: 2 additions & 2 deletions src/examples/news.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from BSE import BSE
from bse import BSE
from datetime import datetime, timedelta
from sys import argv

Expand All @@ -9,7 +9,7 @@
sym = argv[1]
toDate = datetime.today()
daysBack = 10
count = int(argv[2]) if len(argv) == 2 else None
count = int(argv[2]) if len(argv) == 3 else None

attach_url = 'https://www.bseindia.com/xml-data/corpfiling/AttachLive'

Expand Down

0 comments on commit 75d5a71

Please sign in to comment.