Flatten script

A Python script that downloads your backups from the Firestream API and turns them into CSV files, gzipped like the originals (.csv.gz). It calls the same /backups and /symbols/backups endpoints you download from, with your own login, so the CSV holds exactly what is in your download. It can also flatten backup files you already downloaded.

  • User streams: messages, activity
  • Symbol streams: symbols_likes, symbols_messages, symbols_pageviews, symbols_watchlist
  • Daily and monthly files, and ranges of days or months.

  Download flatten-backups.zip

flatten_backups.py (the script), README.md (setup, options and file format) and test_flatten_backups.py (its tests). One Python file, standard library only: nothing to install.


1. What you need

The script can be used in two ways:

UseLogin (username and password)Internet
2. Download and flatten in one stepNeeded Needed
3. Flatten files you already downloadedNot needed Not needed

For both:

  • Python 3.8 or newer. Check with python3 --version. macOS and most Linux systems already have it. On Windows, install it from python.org and use py instead of python3.
  • The script: download the zip above and unzip it. Run the commands below from the flatten-backups folder.
  • Disk space for the flat files. A .csv.gz is about 35-70% of the size of the download:
BackupDownload.csv.gzplain .csv
one weekday of messages (1 December 2026)32.5 MB11.8 MB36.1 MB
one weekday of symbol pageviews (1 December 2026)280 MB191 MB799 MB
one month of messages (October 2026)790 MB285 MB872 MB

2. Download and flatten in one step

Login needed The script downloads the backups from the Firestream API with your login and flattens each file as it arrives. The download itself is not stored unless you add --keep.

You need:

  • Your Firestream username and password, with backup download access for the types and dates you want.
  • Internet access to https://firestream.stocktwits.com.

Step 1: set your username and password

Environment variables, on macOS or Linux:

export FIREHOSE_USER=your_user
export FIREHOSE_PASS=your_password

On Windows (PowerShell):

$env:FIREHOSE_USER="your_user"
$env:FIREHOSE_PASS="your_password"

Or a private file, for example ~/firehose.env (chmod 600 ~/firehose.env), passed with --creds ~/firehose.env:

FIREHOSE_USER=your_user
FIREHOSE_PASS=your_password

Step 2: run it

# one day, user and symbol streams
python3 flatten_backups.py --type messages,activity,symbols_likes --from 2026-11-22 --out flat_out

# a range of days (one file per type per day)
python3 flatten_backups.py --type symbols_watchlist --from 2026-11-01 --to 2026-11-07 --out flat_out

# monthly files: give the month as YYYY-MM
python3 flatten_backups.py --type messages --from 2026-10 --out flat_out

# login from a file, keep the original downloads, plain .csv instead of .csv.gz
python3 flatten_backups.py --creds ~/firehose.env --type messages --from 2026-11-22 --keep originals --no-gzip
OptionDefaultDescription
--typeTypes to download, comma separated: messages, activity, symbols_likes, symbols_messages, symbols_pageviews, symbols_watchlist
--from, --to--to = --from Days as YYYY-MM-DD for daily files, or months as YYYY-MM for monthly files. Both in the same format.
--outcurrent folderFolder for the flat files (created if missing).
--no-gzipWrite plain .csv instead of .csv.gz.
--keepAlso save each original download (.gz) in this folder, so you can flatten it again later without downloading it.
--credsenvironmentFile with FIREHOSE_USER= and FIREHOSE_PASS= lines.
--hosthttps://firestream.stocktwits.comFirestream API host.

Each type and date is one API request, the same as downloading the file with curl. A monthly file exists once the month is over, a daily file the day after.

CommandRequest
--type messages --from 2026-11-22GET /backups/messages/2026/11/22
--type activity --from 2026-10GET /backups/activity/2026/10
--type symbols_likes --from 2026-11-22GET /symbols/backups/likes/2026/11/22

3. Flatten files you already downloaded

No login needed If you already have backup files, for example from curl or from --keep, give them to the script directly. It only reads the files on your disk: no username, no password and no internet access needed.

# one file
python3 flatten_backups.py --out flat_out stocktwits_messages_2026_11_22.gz

# several files
python3 flatten_backups.py --out flat_out stocktwits_messages_2026_11_22.gz stocktwits_activity_2026_11_22.gz

# every backup in a folder (macOS and Linux)
python3 flatten_backups.py --out flat_out originals/*.gz
  • The files can be .gz, as the API sends them, or already unzipped (JSON lines).
  • Each flat file is named after its input: stocktwits_messages_2026_11_22.gz gives stocktwits_messages_2026_11_22.csv.gz. Keep the name the API gives the file, so the flat file gets the same name.
  • Only --out and --no-gzip apply here. --type, --from, --to, --keep, --creds and --host are for downloading, so a run either downloads or flattens local files, not both.

4. Check the result

Each file logs one line when it is done. lines and rows should match:

2026/11/23 09:00:00 INFO flattened file=https://firestream.stocktwits.com/backups/messages/2026/11/22 lines=27748 messages="27748 rows -> flat_out/stocktwits_messages_2026_11_22.csv.gz"

When there are any, the log also counts:

empty_linesLines with no data (not written).
invalid_linesLines that are not valid JSON, or events the script does not cover, such as sentiment (not written).
lines_with_invalid_utf8Lines with bytes that are not UTF-8 (written, each bad byte as U+FFFD).

A file that fails is skipped with an error, the other files still run, and the script exits with code 1 at the end (2 for wrong options).

401When downloading: wrong login, no download access for this type, or a date before your backups start.
404When downloading: no backup file for that date yet.

No flat file is left behind for a failed download (files are written as .part and renamed when complete), and a failed re-run keeps the flat files of the earlier run. A backup with no events gives a flat file with only the header row, so every date you asked for has a file.

5. The flat files

One backup file gives one flat file with the same name: stocktwits_messages_2026_11_22.gz gives stocktwits_messages_2026_11_22.csv.gz. Each JSON line gives one row, in the same order.

Flat fileOne row perColumns
stocktwits_symbols_{likes|messages|pageviews|watchlist}_{date}.csv.gz symbol event created_at, symbol_id, symbol, cusip, isin, metric, user_id, location (user_id is empty for pageviews)
stocktwits_activity_{date}.csv.gz follow, like or block event (create or destroy) time, object, action, id, created_at, user_id, following_user_id, liked_user_id, message_id, blocked_user_id, muted, location
time is when the event happened; for a destroy, created_at is when the follow, like or block was made.
stocktwits_messages_{date}.csv.gz message event (create or destroy) 45 columns:
  • event: time, action, location
  • message: message_id, created_at, body, sentiment, source_id, source_title, parent_message_id, in_reply_to_message_id, conversation_parent, replies, reshared_message_id, reshared_user_id, mentioned_users, links, chart_url, media_count
  • author, as of this message: user_id, username, user_name, user_join_date, user_official, user_identity, user_classification, user_followers, user_following, user_ideas, user_like_count, user_watchlist_stocks_count, user_subscribers_count, user_experience, user_approach, user_holding_period
  • symbols: symbol_count, symbol_ids, symbols, symbol_titles, symbol_exchanges, symbol_sectors, symbol_industries, symbol_cusips, symbol_isins, symbol_prices

Format

  • CSV (RFC 4180) with a header row, UTF-8, \n line endings.
  • Message bodies can contain newlines and commas, so they are quoted and a row can span several lines. Read the files with a CSV parser (pandas, DuckDB, Spark with multiLine=true), not line by line.
  • Empty cell: the field was null or missing.
  • Numbers and times are written exactly as in the JSON (ISO 8601, UTC). Booleans are true/false.
  • List fields are joined with |: mentioned_users, links, user_classification, symbol_*. A | inside an item is written as %7C; this only happens in links, where it is the same URL.
  • Deleted messages, unfollows and unlikes are kept with action=destroy.

Symbols of a message

A message can tag several symbols, but it is still one row. So each symbol detail (ticker, exchange, CUSIP, price...) is one column that lists a value for every symbol, separated by |, always in the same order. The position links the values: the 1st item of every symbol_* column is about the 1st symbol, the 2nd item about the 2nd symbol, and so on.

For example, a message that tags $INDA, $SPX and $NDX has symbol_count = 3 and:

ColumnCell1st symbol2nd symbol3rd symbol
symbolsINDA|SPX|NDXINDASPXNDX
symbol_exchangesBATS|INDEX|INDEXBATSINDEX INDEX
symbol_cusips46429B598||46429B598(none) (none)
symbol_prices52.89|6910.39|52.896910.39 (none)

When a symbol has no value, its slot is left empty but kept, so the positions still line up. Here SPX and NDX are indexes and have no CUSIP, and the message has no price for NDX: 52.89|6910.39| ends with an empty 3rd slot. Without that slot, a reader could not tell which symbol is missing its price. A message without symbols has symbol_count = 0 and empty symbol_* cells.

To analyze by symbol, split the lists into one row per symbol. The message above becomes:

message_idsymbolprice
123INDA52.89
123SPX6910.39
123NDX
-- DuckDB (messages without symbols are left out)
SELECT message_id,
       unnest(string_split(symbols, '|'))       AS symbol,
       unnest(string_split(symbol_prices, '|')) AS price
FROM 'stocktwits_messages_2026_11_22.csv.gz';
# pandas (messages without symbols keep one row with an empty symbol)
import pandas as pd
df = pd.read_csv("stocktwits_messages_2026_11_22.csv.gz", dtype=str, keep_default_na=False)
cols = ["symbols", "symbol_prices"]
df[cols] = df[cols].apply(lambda c: c.str.split("|"))
df.explode(cols)

6. Structure of the flat files

1. The CSV holds exactly what you downloaded

The script downloads the same backup files you get from /backups and /symbols/backups with your login, and only converts them to CSV. It does not add, drop or filter events. If a number looks wrong, compare it with the original file: the two always agree.

2. One backup file gives one CSV, with the same name

stocktwits_messages_2026_11_22.gz becomes stocktwits_messages_2026_11_22.csv.gz. You can always tell which download a CSV came from, and if one day fails you re-run only that day.

3. Each event in the backup is one row, in the same order

Each line of a backup file is one event, and it becomes exactly one CSV row.

  • Check nothing is missing: the log prints both counts, for example lines=27748 and 27748 rows. If they match, every event made it into the CSV.
  • Find the original of any row: row 500 of the CSV is line 500 of the backup.
  • Deletes are kept: a deleted message, an unfollow or an unlike is its own event, with action=destroy. Whether to drop it depends on your analysis, so the CSV keeps it.

4. Details an event has one of are columns on the same row

In the JSON, the author is nested inside the message:

{"id": 1001, "body": "...", "user": {"id": 42, "username": "alice", "followers": 150}}

In the CSV they are on one row, so you don't need a join:

message_idbodyuser_idusernameuser_followers
1001...42alice150

The author columns show the profile at the time of that message. If alice had 100 followers on Nov 1 and 150 on Nov 22, her Nov 1 messages show 100 and her Nov 22 messages show 150.

5. Details an event has several of are | lists

A message can tag several symbols but stays one row, so each symbol detail lists one value per symbol, in the same order (INDA|SPX|NDX). See Symbols of a message for how to read the lists and split them into one row per symbol.

6. Values are copied exactly, not reformatted

The script copies each value as it is in the JSON:

  • ids keep every digit;
  • 230.10 stays 230.10, not 230.1;
  • 1000000 stays 1000000, not 1e+06;
  • times stay in ISO 8601 UTC (2026-11-22T14:05:09Z);
  • message text is unchanged: if the original has it's (HTML for "it's"), the CSV has the same, and you decide whether to decode it.

The one change: a | inside a list item is written as %7C, so a list always splits into one item per value. Only links ever contain a |, and for a URL | and %7C mean the same.

Tip: read ids as text (dtype=str in pandas) so your own tools don't round them.

7. Fields that are only for display are left out

These are dropped to keep the CSV focused on data. They are still in the original JSON if you need them:

  • avatar and logo image URLs;
  • trending scores;
  • is_following ("does the viewer follow this user", which does not apply to a backup);
  • profile bio, website and location;
  • media details (media_count is kept);
  • seq_id, which is always empty in backups.

8. Files are gzipped and never look finished before they are

  • Gzipped: a .csv.gz is usually 35-70% of the download's size, and pandas, DuckDB and Spark read it directly.
  • .part files: while a file is being written it is called stocktwits_messages_2026_11_22.csv.gz.part, and it is renamed only when complete. A file without .part is always complete, even if the script was stopped halfway.

Sentiment backups are not covered.