-
Notifications
You must be signed in to change notification settings - Fork 8
/
stocktwits.py
135 lines (104 loc) · 4.8 KB
/
stocktwits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#interface to stocktwits API
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
class Streamer():
def __init__(self):
self.url = "https://api.stocktwits.com/api/2/"
self.headers = {'Content-Type': 'application/json'}
def get_user_msgs(self, user_id, since=0, max=0, limit=0, callback=None, filter=None):
"""Returns the most recent 30 messages for the specified user.
Args:
user_id (int) = User ID or Username of the stream's user
you want to show (Required)
since (int) = Returns results with an ID greater than (
more recent than) the specified ID.
max (int) = Returns results with an ID less than
(older than) or equal to the specified ID.
limit (int) = Default and max limit is 30.
This limit must be a number under 30.
callback = Define your own callback function name,
add this parameter as the value.
filter (string) = Filter messages by links, charts, or videos.
(Optional)
Return:
raw_json (dict) = The JSON output unparsed
"""
url = self.url + 'streams/user/' + user_id + '.json'
data = {
'since': '{}'.format(since),
'max': '{}'.format(max),
'limit': '{}'.format(limit),
# Fix when you figure out what this is
# 'callback' : '{}'.format(None),
'filter': '{}'.format(filter)
}
r = requests.get(url, headers=self.headers, params=data)
if r.status_code != 200:
raise Exception('Unable to Return Request {}'
.format(r.status_code))
raw_json = r.json()
return raw_json
def get_symbol_msgs(self, symbol_id, since=0, max=0, limit=0, callback=None, filter=None):
'''Returns the most recent 30 messages for the specified symbol.
Args:
symbol_id: Ticker symbol, Stock ID, or
RIC code of the symbol (Required)
since: Returns results with an ID greater than (more recent than)
the specified ID.
max: Returns results with an ID less than (older than) or
equal to the specified ID.
limit: Default and max limit is 30. This limit must be a
number under 30.
callback: Define your own callback function name,
add this parameter as the value.
filter: Filter messages by links, charts, videos,
or top. (Optional)
Return:
raw_json (dict) = The JSON output unparsed
'''
url = self.url + 'streams/symbol/' + symbol_id + '.json'
data = {
'since': '{}'.format(since),
'max': '{}'.format(max),
'limit': '{}'.format(limit),
# Fix when you figure out what this is
# 'callback' : '{}'.format(None),
'filter': '{}'.format(filter)
}
r = requests.get(url, headers=self.headers, params=data)
if r.status_code != 200:
raise Exception('Unable to Return Request {}'
.format(r.status_code))
raw_json = r.json()
return raw_json
def get_specified_conversation_msgs(self, conversation_id, since=0, max=0, limit=0, callback=None):
'''
Args:
conversation_id: The message ID of the parent message
to a conversation. (Required)
since: Returns results with an ID greater than (more recent than)
the specified ID.
max: Returns results with an ID less than (older than) or equal
to the specified ID.
limit: Default and max limit is 30. This limit must be a
number under 30.
callback: Define your own callback function name, add this
parameter as the value.
Return:
raw_json (dict) = The JSON output unparsed
'''
url = self.url + 'streams/conversation/' + conversation_id + '.json'
data = {
'since': '{}'.format(since),
'max': '{}'.format(max),
'limit': '{}'.format(limit)
# Fix when you figure out what this is
# 'callback' : '{}'.format(None),
}
r = requests.get(url, headers=self.headers, params=data)
if r.status_code != 200:
raise Exception('Unable to Return Request {}'
.format(r.status_code))
raw_json = r.json()
return raw_json