Package: clown_sort
CLOWN SORTSometimes someone is being a clown on the internet. Somewhere on your hard drive is the perfect screenshot to prove to the world that the clown in question is a fool, a hypocrite, a criminal, or worse. But then - horrors - you can't find the screenshot! It has been lost in your vast archive of screenshots of clowns clowning themselves on the internet.Clown Sort[^1] solves this.What It DoIt sorts screenshots, PDFs, etc. based on their name and/or their textual contents into folders based on a list of rules. The contents of the tweet/reddit post/whatever are prepended to the filename and theImageDescriptionEXIF tag is set to the OCR text. As you can configure your own arbitrary rules and run it against any set of images it works on many things other than screenshots of social media clowns, though the default configuration is for cryptocurrency clowns.For example this screenshot of a tweet by a noteworthy cryptocurrency "reporter"[^2] on the eve of FTX's implosion:Would be renamed fromScreen Shot 2023-02-17 at 7.11.37 PM.pngtoTweet by @lawmaster: "I will say though before this thread gets taken over: 1. I do believe Alameda has the size to easily buy Binance\'s FIT OTC 2. I think the chance of FTX insolvency is near" Screen Shot 2023-02-17 at 7.11.37 PM.pngOther stuff that happens:TheImageDescriptionEXIF tag will be written (for images).All timestamps will be preserved.Files that match multiple patterns will be copied to multiple destination folders.The original file will be moved into aProcessed/directory after it has been handled.Note also that:This works on images that are more substantive than just self-clowning screenshots.So far only Tweets and Reddit screenshots have special handling beyond OCR text extraction.PDFs can be sorted by contents or filename, e.g. a PDF namedNorton Anthology of Crypto Bro Poetry.pdfcontaining iambic verse like"Fuck u justin sun  and fuck ur dick face... u all play with investing and money of the people !!!!"by the noted bard JOKER_OF_CRYPTO will be copied to theJustin Sun/folder but not renamed.Videos are not OCRed and can only be moved based on filename matches, e.g. a file calledSBF is a big fat liar.movwill be moved to theFTX/folder but otherwise left alone.Quick Start# Installation with pipx is preferred if you have it but you can also use pip which comes standard# on almost all systems. pipx is only a noticeably better answer if you're a python programmer who# is concerned about side effects of pip upgrading system python packages.pipinstallclown_sort[gui,pdf]# Get helpsort_screenshots-h# Dry run with default cryptocurrency sort rules (dry runs don't actually move anything,# they just show you what will happen if you run again with the --execute flag)sort_screenshots# Execute default cryptocurrency sort rules against ~/Pictures/Screenshots with debug loggingsort_screenshots--execute--debug# Sort a different directory of screenshotssort_screenshots--screenshots-dir/Users/hrollins/Pictures/get_in_the_van/tourphotos--execute# Sort with custom rulessort_screenshots--rules-csv/Users/hrollins/my_war.csv--execute# Sort pdfssort_screenshots-f'.*pdf$'-e# Sort all but put up the manual folder / filename selector window if file doesn't match any sort rulessort_screenshots-a-mf-eSetuppipxis recommended because it keeps your system python environment safe but you can also just usepip.# Install with all packages including GUI and PDF handlers:pipxinstall'clown_sort[gui,pdf]'# Install with optional GUI packages (required for --manual-fallback and --manual-sort options):pipxinstall'clown_sort[gui]'--force# Install the minimal setup (no GUI, no encrypted PDF parsing):pipxinstall'clown_sort'Some (not many) PDFs require thepycryptodomepackage to be parsed. This is installed when you installclown_sort[pdf]. If you don't install it these PDFS will not have their contents parsed and only the filename will be used for sorting.Updating to the latest version can be accomplished withpipx upgrade clown_sort.Configuring With.clown_sortFileIf there are command line options you find yourself specifying repeatedly you can place them in a.clown_sortfile. When you invokesort_screenshotsthe following locations will be checked for.clown_sort:The current directoryYour home directorySeethe examplefor more information on what can be configured this way.Optional ComponentsIf you want to use the popup window to manually tag youmayneed to install:Python TK:brew install python-tk@3.10(if you don't havehomebrewyou need to install it to runbrew install)Not required for standard PNG, JPG, etc. images but you may optionally installexiftoolfor other file types if you want excessive debugging.ExifTool:brew install exiftoolor download fromhttps://exiftool.orgUsageThe default is for the tool to run in "dry run" mode, meaning it doesn't actually do anything - it just shows you what itwoulddo if you added the--executeflag.YOU ARE ADVISED TO MAKE A BACKUP OF YOUR SCREENSHOTS FOLDER BEFORE HITTING THE--executeFLAG.While every effort has been made to use Python's cross platformPathlibmodule as much as possible sometimes shit gets wonky on other platforms. This is 100x as true on Windows - Clown Sort has never been tested on a Windows platform.Help Screen(In my personal usuage I tend to run the tool with the--alland--manual-fallbackoptions.)Custom Sorting RulesThe default is to sort cryptocurrency related content but you can define your own CSV of rules with two columnsfolderandregex.Lines whose first non-whitespace character is#will be considered to be comments and skipped as will empty linesfolderspecifies the subdirectory to sort intoregexis the pattern to match against. Seethe default crypto related configurationfor an example. An explanation of regular expressions is beyond the scope of this README but many resources are available to help. If you're not good at regexes just remember that any alphanumeric string is a regex that will match that string.pythexis a great website for testing your regexes.Example Output (Automated Sorting)Manually Sorting (Experimental)This is an experimental feature.It's only been tested on macOS.If you run with the--manual-sortcommand line the behavior is quite different. Rather than automatically sort files for you for every image file you will be greeted with a popup asking you for a desired filename and a radio button select of possible subdirectories off yourSorted/directory.A related command line option is--manual-fallbackwhich will popup a window only when the file is an image and has not matched any of the configured sorting rules.To use this feature you must install the optionalPySimpleGUIpackage which can be accomplished like this:pipxinstallclown_sort[gui]One Off ExtractionsThere are several utilities / convenience scripts / whatever you want to call them that are installed along withclown_sort. They work with the same toolset assort_screenshotsbut use it to simplify the extraction of text from images (or PDFs that are actually just a set of page sized images).Extracting textextract_text_from_filesis a convenient script you can use to extract text from a single file, multiple files, or all the files in a given directory using Google's best in class Tesseract library.As an example for extracting multiple files and/or directories:extract_text_from_files MY_FILE1 MY_FILE2 SOME_DIR3This will parse and display the text inMY_FILE1,MY_FILE2, and all the files inSOME_DIR3.Extracting pages of a PDF to a new PDFextract_pages_from_pdfis a small script that can extract page ranges (e.g. "10-25") from PDFs on the command line.ContributingFeel free to file issues or open pull requests.This package is managed withPython Poetry. To get going:Install Poetry.git clonethis repo.cd clown_sortpoetry installOptional components can be install withpoetry install --all-extrasOnly requirement is that tests should pass before you open it which you can check by runningpytest.Running TestsTest suite can be launched withpytest. If you do some development and something goes weird and you're continually running into errors about non-empty directories try clearing out the test suite's temp directory by deleting the contents oftests/tmp/(rm -fr tests/tmp/*on Linux or macOS etc.).[^1]: The nameclown_sortwas suggested byParrotCapitaland while the tool can work on any kind of screenshot it was too good not to use.[^2]: Perhaps notable that the "reporter" in question for years maintained a private list of the blockchain addresses of Sam Bankman-Fried's various scams as part of his commitment to "unrivaled transparency".