Skip to content

Instantly share code, notes, and snippets.

@rishipr
Created May 14, 2020 04:51
Show Gist options
  • Save rishipr/dbd65e4cbc5502f71cab0ca5644bbf1e to your computer and use it in GitHub Desktop.
Save rishipr/dbd65e4cbc5502f71cab0ca5644bbf1e to your computer and use it in GitHub Desktop.
from flask import Flask, request, jsonify
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import json
from support import getCIK
from flask_cors import CORS
import pprint
app = Flask(__name__)
CORS(app)
chrome_options = Options()
chrome_options.add_argument("--headless")
def form_or_json():
data = request.get_json(silent=True)
return data if data is not None else request.form
@app.route('/', methods=['POST'])
def main():
data = form_or_json()
ticker = data['ticker']
terms = data['terms']
CIK = getCIK(ticker)
if (CIK == None):
return {
'error': 'Invalid Ticker'
}
driver = webdriver.Chrome(
executable_path="./chromedriver", options=chrome_options)
res_dict = {}
for t in terms:
res_dict[t] = {}
res_dict[t]['numResults'] = 0
res_dict[t]['results'] = []
t = t.replace(" ", "%20")
url = f"https://searchwww.sec.gov/EDGARFSClient/jsp/EDGAR_MainAccess.jsp?search_text=\"{t}\"&sort=Date&formType=Form10K&isAdv=true&stemming=true&numResults=100&numResults=100&queryCik={CIK}&startDoc=1"
driver.get(url)
results = driver.find_elements_by_css_selector(
"#result font.normalbold")
t = t.replace("%20", " ")
if (results[0].text == "No Results were Found." or len(results) < 2):
continue
elif (len(results) > 1):
num_results = results[1].text
snippets = driver.find_elements_by_css_selector("i.small")
final_results = []
for s in snippets:
s = s.text.lower()
if t in s:
final_results.append(s)
res_dict[t]['numResults'] = len(final_results)
res_dict[t]['results'] = final_results
return res_dict
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment