Package: reddit-user-to-sqlite
reddit-user-to-sqliteStores all the content from a specific user in a SQLite database. This includes their comments and their posts.InstallThe PyPI package isreddit-user-to-sqlite(PyPI Link). Install it globally usingpipx:pipxinstallreddit-user-to-sqliteUsageThe CLI currently exposes two commands:userandarchive. They allow you to archive recent comments/posts from the API orallposts (as read from a CSV file).userFetches all comments and posts for a specific user.reddit-user-to-sqliteuseryour_username
reddit-user-to-sqliteuseryour_username--dbmy-reddit-data.dbParamsNote: the argument order is reversed from most dogsheep packages (which take db_path first). This method allows for use of a default db name, so I prefer it.username: a case-insensitive string. The leading/u/is optional (and ignored if supplied).(optional)--db: the path to a sqlite file, which will be created or updated as needed. Defaults toreddit.db.archiveReads the output of aReddit GDPR archiveand fetches additional info from the Reddit API (where possible). This allows you to store more than 1k posts/comments.FYI: this behavior is built with the assumption that the archive that Reddit provides has the same format regardless of if you selectGDPRorCCPAas the request type. But, just to be on the safe side, I recommend selectingGDPRduring the export process until I'm able to confirm.ParamsNote: the argument order is reversed from most dogsheep packages (which take db_path first). This method allows for use of a default db name, so I prefer it.archive_path: the path to the (unzipped) archive directory on your machine. Don't rename/move the files that Reddit gives you.(optional)--db: the path to a sqlite file, which will be created or updated as needed. Defaults toreddit.db.(optional)--skip-saved: a flag for skipping the inclusion of loading saved comments/posts from the archive.Viewing DataThe resulting SQLite database pairs well withDatasette, a tool for viewing SQLite in the web. Below is my recommended configuration.First, installdatasette:pipxinstalldatasetteThen, add the recommended plugins (for rendering timestamps and markdown):pipxinjectdatasettedatasette-render-markdowndatasette-render-timestampsFinally, create ametadata.jsonfile next to yourreddit.dbwith the following:{"databases":{"reddit":{"tables":{"comments":{"sort_desc":"timestamp","plugins":{"datasette-render-markdown":{"columns":["text"]},"datasette-render-timestamps":{"columns":["timestamp"]}}},"posts":{"sort_desc":"timestamp","plugins":{"datasette-render-markdown":{"columns":["text"]},"datasette-render-timestamps":{"columns":["timestamp"]}}},"subreddits":{"sort":"name"}}}}}Now when you rundatasettereddit.db--metadatametadata.jsonYou'll get a nice, formatted output:MotivationI got nervous when I saw Reddit'snotification of upcoming API changes. To ensure I could always access data I created, I wanted to make sure I had a backup in place before anything changed in a big way.FAQsWhy does this post only show 1k recent comments / posts?Reddit's paging API only shows 1000 items (page 11 is an empty list). If you have more comments (or posts) than than that, you can use theGDPR archive import featurefeature to backfill your older data.Why are my longer posts truncated in Datasette?Datasette truncates long text fields by default. You can disable this behavior by using thetruncate_cells_htmlflag when runningdatasette(see the docs):datasettereddit.db--settingtruncate_cells_html0How do I store a username that starts with-?By default,click(the argument parser this uses) interprets leading dashes on argument as a flag. If you're fetching data for user-asdf, you'll get an error sayingError: No such option: -a. To ensure the last argument is interpreted positionally, put it after a--:reddit-user-to-sqliteuser---asdfWhy do some of my posts say[removed]even though I can see them on the web?If a post is removed, only the mods and the user who posted it can see its text. Since this tool currently runs without any authentication, those removed posts can't be fetched via the API.To load data about your own removed posts, use theGDPR archive import feature.Why is the database missing data returned by the Reddit API?While mostDogsheepprojects grab the raw JSON output of their source APIs, Reddit's API has a lot of junk in it. So, I opted for a slimmed down approach.If there's a field missing that you think would be useful, feel free to open an issue!Does this tool refetch old data?When running theusercommand, yes. It fetches and updates up to 1k each of comments and posts and updates the local copy.When running thearchivecommand, no. To cut down on API requests, it only fetches data about comments/posts that aren't yet in the database (since the archive may include many items).Both of these may change in the future to be more in line withReddit's per-subreddit archiving guidelines.DevelopmentThis section is people making changes to this package.When in a virtual environment, run the following:pipinstall-e'.[test]'This installs the package in--editmode and makes its dependencies available. You can now runreddit-user-to-sqliteto invoke the CLI.Running TestsIn your virtual environment, a simplepytestshould run the unit test suite. You can also runpyrightfor type checking.Releasing New Versionsthese notes are mostly for myself (or other contributors)Runjust releasewhile your venv is activepaste the stored API key (If you're getting invalid password, verify that~/.pypircis empty)