Dowload a backup for the different available stream types. This works by generating a temporary download link to the file requested. The request will respond with a 302 redirect
Request Method | HTTP GET |
URL | Found on the stream's API Help page of your dashboard, using the following structure:
https://firestream.stocktwits.com/backups/{file_type}/{year}/{month}/{day} file_type
|
The following responses may be returned by the API for these requests. Most error codes are returned with a string with additional details in the body. For non-200 responses, clients should attempt to reconnect.
Status | Text | Description |
---|---|---|
302 | Success | The file is authorized and a redirect to the temporary download link is being returned. |
400 | Bad Request | Please verify that the date is correct. |
401 | Unauthorized | HTTP authentication failed due to invalid credentials. Verify that your stocktwits login is correct. |
404 | Not Found | The file requested was not found. |
The following example request is accomplished using cURL on the command line.
'username':'password'
will need to be replaced with your user login and password in order to download the file.curl -JLO -u 'username':'password' "https://firestream.stocktwits.com/backups/activity/2016/12/05"
If you are working on Windows, you may need to use double quotes instead of single quotes for the username and password.
curl -JLO -u "username":"password" "https://firestream.stocktwits.com/backups/activity/2016/12/05"
The following example request is accomplished using a python script with the requests library.
''' This script uses the "requests" library to connect to the stream. It should be installed using pip before running this script. ''' import requests from requests.auth import HTTPBasicAuth url = "https://firestream.stocktwits.com/backups/activity/2016/12/05" username = "myusername" password = "mypassword" # Create a new request, the stream parameter is set to True to keep the connection open resp = requests.get(url, auth=HTTPBasicAuth(username, password), stream=True) # Extract filename from url filename = "stocktwits_"+"_".join(url.split('/')[-4:]) + '.gz' # Check for errors on the response a HTTP status code of 200 is expected if resp.status_code != 200: print(f"Error response: {resp.status_code} {resp.reason}") exit() # Save the file try: with open(filename, 'wb') as f: for chunk in resp.iter_content(chunk_size=1024): if chunk: f.write(chunk) except Exception as e: print(f"Error saving file stream: {e}")
The following example show how to read the downloaded file using python.
import gzip, json # This is the name of the file that was downloaded fileName = "stocktwits_messages_2017_09_21.gz" # This is an array of dictionaries # Each dictionary represents a message data = [] # Open the file and read each line, we are using gzip to open the file # since the file is in .gz format with gzip.open(fileName, "rb") as f: for line in f: # Unmashall the JSON object from the line message = json.loads(line) # Append the message to the data array data.append(message)