Created
April 19, 2020 16:53
-
-
Save charlesalexanderlee/dd0d49dabe4b86836e148519bd38b517 to your computer and use it in GitHub Desktop.
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 praw | |
import requests | |
import simplejson | |
from datetime import datetime | |
from praw.models import MoreComments | |
from time import sleep | |
from requests_oauthlib import OAuth1 | |
from config import (api_key, | |
secret, | |
oath_token, | |
oath_secret, | |
client_id, | |
client_secret) | |
# Initializes Reddit API | |
reddit = praw.Reddit(client_id=client_id, | |
client_secret=client_secret, | |
user_agent='wallstreetbets') | |
# Authentication for Ally API | |
auth = OAuth1(api_key, secret, oath_token, oath_secret) | |
# Gets submission | |
submission = reddit.submission(id='g48v33') | |
# Top level comments only | |
submission.comments.replace_more(limit=None) | |
# Iterates through each top level comment | |
for comment in submission.comments: | |
# Splits comment into list of words | |
option = comment.body.upper().split() | |
ticker = '' | |
# Only gets comments with 3 parts | |
if len(option) == 3: | |
# Ticker | |
if option[0][0] == '$': | |
ticker += option[0][1:] | |
else: | |
ticker += option[0] | |
# Date | |
date = option[2].split('/') | |
if len(date) == 3: | |
ticker += date[2][0:2] | |
ticker += date[0].zfill(2) | |
ticker += date[1].zfill(2) | |
# Direction | |
if option[1][-1] == 'C': | |
ticker += 'C' | |
elif option[1][-1] == 'P': | |
ticker += 'P' | |
else: | |
break | |
# Strike | |
strike = '' | |
if option[1][0] == '$': | |
strike = option[1][1:-1] | |
else: | |
strike = option[1][0:-1] | |
if '.' in strike: | |
idx = strike.index('.') | |
ticker += strike[0:idx].zfill(5) | |
ticker += strike[idx+1:].zfill(3) | |
else: | |
ticker += strike.zfill(5) + '000' | |
try: | |
print(comment.body) | |
print(ticker) | |
url = f'https://api.tradeking.com/v1/market/ext/quotes.json?symbols={ticker}' | |
response = requests.get(url, auth=auth).json() | |
print(response['response']['quotes']['quote']['ask']) | |
print() | |
sleep(0.5) | |
except KeyError: | |
pass | |
except simplejson.errors.JSONDecodeError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much man! I really appreciate it :D