Skip to main content

Stack Exchange CLI for searching

Project description

Stack Exchange Search CLI

PyPI Build status

Search stack exchange websites in your terminal!

With beautiful terminal formatting using Rich.

All stack exchange websites available for searching: https://stackexchange.com/sites

Fast Search

Fast search Demo

Interactive Search

Interactive search Demo

Table of Contents

  1. How it works
  2. Install
  3. Usage
  4. Command Line Arguments
  5. Configuration
  6. Testing
  7. TODO

How it works

Displays the highest up-voted question and top answer for your search request
Inspired by: https://github.com/chubin/cheat.sh

Install

Supported platforms
  • Linux
  • Windows
  • Mac
Requirements
  • Python 3.10 or higher

Main Installation

Just pip install it! Fast and easy.

python3.10 -m pip install stack-exchange-cli

Install Python 3.10

Mac

brew install python@3.10

Linux

sudo apt install software-properties-common -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.10 -y

Windows

Download from here: https://www.python.org/downloads/

If you can't use 'se' after installing stack-exchange-cli via pip, ensure your Python310\Scripts is set in your PATH. You will recieve a warning to add to PATH when pip installing stack-exchange-cli.

Alternative methods to install

  • Install the requirements file manually

    python3.10 -m pip install -r requirements.txt
    
  • Use poetry to install dependencies locally (https://python-poetry.org/docs/)

    poetry install
    
  • Run the scripts/install_ubuntu.sh script [UBUNTU ONLY]

    # run in root directory
    sudo bash scripts/install_ubuntu.sh
    
  • Install to venv named .stack-exchange-venv using make

    # run in root directory
    make
    
  • Build from source using poetry

    # run in root directory
    poetry build
    python3.10 -m pip install dist/stack_exchange_cli*.whl
    

Usage

Fast Search

Use the -q command followed by the search query:

se -q="BFS vs DFS"

The above command uses fast search, which fetches the top-voted question and answer and displays them to the console.

Interactive Search

Use the -q command followed by the search query and -i or --interactive

se -q="BFS vs DFS" -i

Interactive search allows the user to interact while searching, analogous to browsing stack-exchange questions in your browser, except in the terminal!

Run directly with python interpreter

python3.10 -m stack_exchange -q="BFS vs DFS"

Command Line Arguments

Short Long Description Example Default
-q --query [REQUIRED FOR SEARCH] Search query se -q="How to merge two dictionaries" N/A
-s --site [OPTIONAL] Stack Exchange website to search on View all sites here: (https://stackexchange.com/sites) se -q="Big O" -s="softwareengineering" "stackoverflow"
-t --tags [OPTIONAL] Search tags (space-delimited) se -q="Segmentation fault cause" -t="c c++" N/A
-i --interactive [OPTIONAL] Allow the user to interact while searching se -q="Tree traversal" -i False
-n --num [OPTIONAL] [INTERACTIVE ONLY] Number of results to display se -q="Segmentation fault cause" -i -n=20 30
-sb --sortby [OPTIONAL] Method to sort the search results by choices = ["votes", "creation", "relevance", "activity"] se -q="Python memory" -sb="relevance" "votes"
-vv --verbose [OPTIONAL] Verbose logging flag, set log level to DEBUG se -q="Dictionary internals" -vv False
-c --config [OPTIONAL] config.yaml file path to use for API, Redis and logging settings se -q="Directed graph" -c="/mnt/c/config.yaml" N/A
-k --key [OPTIONAL] Use stack exchange API key for requests se -q="Min heap vs max heap" -k="12345" N/A
-sk --set-key [OPTIONAL] Set stack exchange API key in config.yaml, to avoid repeating using -k in search commands se -sk="12345" N/A
-fc --flush-cache [OPTIONAL] Flush all keys/values in redis cache se -fc False
-oc --overwrite-cache [OPTIONAL] Overwrite cache value if key exists se -q="DFS vs BFS" -oc False
-j --json [OPTIONAL] Print search results as json to stdout se -q="DFS vs BFS" -j False
-a --alias [OPTIONAL] View the cached search result under the specified alias se -a ="my_alias_i_saved_my_search_result" N/A
-h --help [OPTIONAL] Displays help text se -h N/A
-v --version [OPTIONAL] Displays version number se -v N/A

Configuration

The application can be configured by using the -c cmd line argument to point it to a yaml config file path.

se -q="DFS vs BFS" -c="/mnt/c/my_config_file.yaml"

Note: Most users won't need to configure the application, it's supposed to be easy to use out of the box! These are optional configuration settings the user can use. By default, the application will be packaged up with the config.yaml in the root directory.

API Configuration

Fill out yaml api values with a stack exchange API key to prevent request throttling.
Read more here: https://api.stackexchange.com/docs/throttle

You can get an API Key by registering as a new app from here: http://stackapps.com/apps/oauth/register
If you use an api key, you will have a daily request limit of 10000

Note: You probably won't need an API key if you are a light-user.

From stack-exchange: "Every application is subject to an IP based concurrent request throttle. If a single IP is making more than 30 requests a second, new requests will be dropped. The exact ban period is subject to change, but will be on the order of 30 seconds to a few minutes typically."

Redis Configuration

Fill out yaml redis values with redis credentials if you want to hook up the application to a redis db for request caching.

Speed benefits are minor, but it will help with being throttled as it will just read the cache instead of going over the network to the stack exchange API if you request the same thing more than once.

There are also command line arguments for interfacing with the cache, i.e. overwrite values in the cache or flush the cache.

This isn't needed but if you want to use the redis free tier, check out: https://redis.com/try-free/

Logging configuration

Modify logging values to adjust application log settings.

By default, logging to a file will be disabled and the log level will be critical to avoid polluting the output.

Example Config File

config.yaml

api:
  api_key: your_api_key # optional
  default_site: "stackoverflow"  # required
  version: 2.3 # required

redis: # all fields optional
  host: redis-notarealhost.redislabs.com
  port: 12345
  password: redisdbfakepassword

logging: # all fields required
  log_to_file: true 
  log_filename: "stackexchange.log"
  log_level: "DEBUG"

Testing

Run tests using pytest

python3.10 -m pytest

TODO

Refactor CLI to use https://github.com/Textualize/textual for interactive search

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stack-exchange-cli-1.0.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stack_exchange_cli-1.0.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file stack-exchange-cli-1.0.0.tar.gz.

File metadata

  • Download URL: stack-exchange-cli-1.0.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.10.102.1-microsoft-standard-WSL2

File hashes

Hashes for stack-exchange-cli-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7f4a26c705073236b64b35caed48bf512aa6cb09bd1babc6028c8459da5333ef
MD5 405e2431643deefd0bf3d662a2129d2b
BLAKE2b-256 3d6b41be6671b2d95e622c9d915432f53e022f032c8e8418c29bbd21cae1f37b

See more details on using hashes here.

File details

Details for the file stack_exchange_cli-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: stack_exchange_cli-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.10.102.1-microsoft-standard-WSL2

File hashes

Hashes for stack_exchange_cli-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5e2e84ada305222ddd5b5ad28066787e6e81905f35d10e13347dcc8e8eb9ca7
MD5 2d4039da3645abe04578968f52bcee83
BLAKE2b-256 bb60e1bfe3fec54401615630d8686baf9ee21f0ef1d55aaf516adbb56e6df3ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page