usrxprd

sqwoker.sh

Apr 13th, 2022
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.70 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # sqwoker - the sqwok reader
  4. # ----------------------------------------------------------------
  5. # This software is still in alpha stage. Future releases may break
  6. # compatibility. The output of this script should not be treated
  7. # as consistent with the content located at sqwok.im
  8. #
  9. # Sqwoker uses a custom user agent. I believe this may help
  10. # troubleshoot any server-side issues caused by this client.
  11. #
  12. # Sqwoker was tested with bash and dash. Should theoretically work
  13. # with some other shells too.
  14. #
  15. # Sqwoker relies on curl for networking and jq for json parsing.
  16. # ----------------------------------------------------------------
  17. # Example usage
  18. #
  19. # Get post listing for "new" and save to file new.json
  20. #
  21. # $ ./sqwoker.sh --get new > new.json
  22. #
  23. # Parse the post listing from file new.json
  24. #
  25. # $ ./sqwoker.sh -p < new.json
  26.  
  27. set -e
  28.  
  29. # version string
  30. sqwoker_version="0.1.0"
  31.  
  32. # help message
  33. sqwoker_help="sqwoker $sqwoker_version - help
  34. usage:
  35.     $ $0 <flag> [options]
  36. flags:
  37.     -g, --get <sort>    download post listing and print to stdout
  38.     -p, --parse     parse json post listing from stdin
  39.     -b, --brief     like -p but briefer
  40.     -d, --default       execute the default function (-g hot | -b)
  41.     -h, --help      print a help message and exit
  42.     -v, --version       print version string and exit
  43. valid sort options:
  44.     new
  45.     hot
  46. "
  47.  
  48. # the default function - called with -d
  49. sqwokerDefaultFunc () {
  50.     getListing "hot" | parseListingBrief
  51. }
  52.  
  53. # curl request to fetch post listing (output to stdout)
  54. # authorization header token required for guest authorization
  55. getListing () {
  56.     curl -XGET "https://api.sqwok.im/v1/posts/$1" \
  57.         -H "User-Agent: sqwoker/$sqwoker_version" \
  58.         -H "Authorization: uskoa eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODc2MzA3OTYsIm9yaWdfaWF0IjoxNTg3NjI5ODk2LCJhdHRhY2tTaGlwc09uTWFycyI6dHJ1ZSwidGFubkhhdXNlckdhdGUiOnRydWV9.dpf5d0OKdFbas0F7IAis1ACoyikj1-LIMIfNxBFBXKo"
  59. }
  60.  
  61. # jq filter to parse post listing (from stdin)
  62. parseListing () {
  63.         jq -r '.data[] | "[=] \(.attributes.text)\n\(.relationships.nest.data.id) | \(.attributes.created_at) | messages: \(.attributes.message_count) | id: \(.id) | sqwok_id: \(.attributes.sqwok_id)\n"'
  64. }
  65.  
  66. # like parseListing, but without some meta data
  67. parseListingBrief () {
  68.     jq -r '.data[] | "[=] \(.attributes.text)\n\(.relationships.nest.data.id) | \(.attributes.created_at) | messages: \(.attributes.message_count) | id: \(.id)\n"'
  69. }
  70.  
  71. # process command-line parametres
  72.  
  73. # see if any parametres present
  74. if [ ! -z "$1" ]; then
  75.  
  76.     # get - download post listing
  77.     if [ "$1" = "-g" ] || [ "$1" = "--get" ]; then
  78.  
  79.         # requires one additional parametre (sort option)
  80.         if [ ! -z "$2" ]; then
  81.  
  82.             # filter valid sort options
  83.             if [ "$2" = "new" ] || [ "$2" = "hot" ]; then
  84.  
  85.                 getListing "$2"
  86.  
  87.             else
  88.                 printf "%s> ERROR: Invalid sort option (%s). Try -h for help\n" "$0" "$2" 1>&2
  89.             fi
  90.         else
  91.             printf "%s> ERROR: No parametre specified for flag (%s). Try -h for help\n" "$0" "$1" 1>&2
  92.         fi
  93.  
  94.     # parse - parse json post listing
  95.     elif [ "$1" = "-p" ] || [ "$1" = "--parse" ]; then
  96.  
  97.         parseListing
  98.  
  99.     # brief - briefer version of -p
  100.     elif [ "$1" = "-b" ] || [ "$1" = "--brief" ]; then
  101.  
  102.         parseListingBrief
  103.  
  104.     # default - execute the default function
  105.     elif [ "$1" = "-d" ] || [ "$1" = "--default" ]; then
  106.  
  107.         sqwokerDefaultFunc
  108.  
  109.     # print help message
  110.     elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  111.  
  112.         printf "%s" "$sqwoker_help"
  113.  
  114.     # print version string
  115.     elif [ "$1" = "-v" ] || [ "$1" = "--version" ]; then
  116.  
  117.         printf "sqwoker version %s\n" "$sqwoker_version"
  118.  
  119.     else
  120.         printf "%s> ERROR: Invalid parametre (%s). Try -h for help\n" "$0" "$1" 1>&2
  121.     fi
  122.  
  123. else
  124.     printf "%s> ERROR: No command given. Try -h for help\n" "$0" 1>&2
  125. fi
  126.  
Advertisement
Add Comment
Please, Sign In to add comment