Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Last active November 12, 2019 08:40
Show Gist options
  • Save tecoholic/ca4f9933335b34388375bceb213a5801 to your computer and use it in GitHub Desktop.
Save tecoholic/ca4f9933335b34388375bceb213a5801 to your computer and use it in GitHub Desktop.
Post Nifty movers to Mastodon
"""A Mastodon bot that collects the Nifty data and creates a post"""
import json
import urllib.request
import urllib.parse
import os
from datetime import datetime
def build_request(url):
"""Builds a request with appropriate headers for NSE"""
headers = {
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Host': 'nseindia.com',
'Referer': "https://www.nseindia.com/live_market\
/dynaContent/live_watch/get_quote/GetQuote.jsp?symbol=INFY&illiquid=0&smeFlag=0&itpFlag=0",
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0',
'X-Requested-With': 'XMLHttpRequest'
}
request = urllib.request.Request(url, {}, headers)
return request
def toot(message):
host_instance = os.getenv("HOST_INSTANCE")
if not host_instance:
return {"status": "error", "message": "Access Token Not Available"}
token = os.getenv("ACCESS_TOKEN")
if not token:
return {"status": "error", "message": "Access Token Not Available"}
headers = {
"Authorization": f"Bearer {token}"
}
data = {
"status": message,
"visibility": "public"
}
request = urllib.request.Request(
url=host_instance+"/api/v1/statuses",
data=urllib.parse.urlencode(data).encode("utf-8"),
headers=headers
)
with urllib.request.urlopen(request) as res:
toot_status = json.loads(res.read())
return toot_status
def lambda_handler(event, context):
# Let us load the JSONs
index_url = "http://www.nseindia.com/homepage/Indices1.json"
gainers_url = "http://www.nseindia.com/live_market/dynaContent/live_analysis/gainers/niftyGainers1.json"
losers_url = "http://www.nseindia.com/live_market/dynaContent/live_analysis/losers/niftyLosers1.json"
with urllib.request.urlopen(build_request(index_url)) as response:
index = json.loads(response.read())
date_string = index["time"]
file_date = datetime.strptime(date_string, "%b %d, %Y").date()
today = datetime.now().date()
if file_date != today:
return {"status": "skipped", "message": "Market data is not from today"}
with urllib.request.urlopen(build_request(gainers_url)) as response:
gainers = json.loads(response.read())
with urllib.request.urlopen(build_request(losers_url)) as response:
losers = json.loads(response.read())
lines = ['#Nifty Closing Report']
for entry in index["data"]:
if entry["name"] == "NIFTY 50":
lines.append(f"NIFTY 50\nIndex: {entry['lastPrice']} Change: {entry['change']} {entry['pChange']}%")
break
lines.append("\n⬆️ Gainers")
for scrip in gainers["data"]:
lines.append(f"{scrip['symbol']} {scrip['netPrice']}%")
lines.append("\n⬇️ Losers")
for scrip in losers["data"]:
lines.append(f"{scrip['symbol']} {scrip['netPrice']}%")
return toot("\n".join(lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment