The little things give you away... A collection of various small helper stuff
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

83 lignes
2.2 KiB

  1. #!/bin/bash
  2. # Usage: wpull2-requeue [ACTION] [FILENAME] ( [--where] URLPATTERN_OR_WHERE | --urls-from-stdin )
  3. # ACTION can be 'count' (default), 'print', or 'write'. On 'write', the number of modified records is printed.
  4. # FILENAME defaults to 'wpull.db'
  5. # URLPATTERN_OR_WHERE is URLPATTERN if --where isn't used or WHERE if it is.
  6. # URLPATTERN uses SQLite's LIKE syntax with ESCAPE "\", i.e. % matches any number of characters, _ matches exactly one character, and a backslash can be used to escape these special characters.
  7. # Must not contain quotes.
  8. # WHERE is an arbitrary SQLite 'WHERE' condition. The available tables are 'queued_urls' and 'url_strings', already joined together.
  9. # When --urls-from-stdin is used, one URL per line is expected on stdin. The URL must be in normalised format and not contain any double quotes.
  10. if [[ $# -eq 4 || ( $# -ge 2 && $# -le 3 && ( "$1" == 'count' || "$1" == 'print' || "$1" == 'write' )) ]]
  11. then
  12. action="$1"
  13. shift
  14. else
  15. action=count
  16. fi
  17. if [[ $# -eq 3 || ( $# -eq 2 && "$1" != '--where' ) ]]
  18. then
  19. filename="$1"
  20. shift
  21. else
  22. filename=wpull.db
  23. fi
  24. if [[ ! -f "${filename}" ]]
  25. then
  26. echo "Error: ${filename} does not exist or is not a regular file" >&2
  27. exit 1
  28. fi
  29. urlsfromstdin=
  30. if [[ "$1" == '--urls-from-stdin' ]]
  31. then
  32. urlsfromstdin=1
  33. shift
  34. if [[ $# -ne 0 ]]
  35. then
  36. echo "Error: invalid arguments" >&2
  37. exit 1
  38. fi
  39. fi
  40. where=
  41. if [[ "$1" == '--where' ]]
  42. then
  43. where=1
  44. shift
  45. fi
  46. if [[ "$1" == --* ]]
  47. then
  48. echo "Error: Unknown option $1" >&2
  49. exit 1
  50. fi
  51. if [[ "${where}" ]]
  52. then
  53. where="$1"
  54. elif [[ "${urlsfromstdin}" ]]
  55. then
  56. urls="$(sed 's/^/"/; s/$/", /' | tr -d '\n' | sed 's/, $//')"
  57. if [[ -z "${urls}" ]]
  58. then
  59. exit 0
  60. fi
  61. where="url IN (${urls})"
  62. else
  63. where='url LIKE "'"$1"'" ESCAPE "\" AND status = "skipped" AND try_count > 3'
  64. fi
  65. query='FROM queued_urls JOIN url_strings ON url_string_id = url_strings.id WHERE '"${where}"
  66. if [[ "${action}" == 'write' ]]
  67. then
  68. sqlite3 "${filename}" <<<'UPDATE queued_urls SET status = "todo", try_count = 0, status_code = NULL WHERE id IN (SELECT queued_urls.id '"${query}"'); SELECT changes()'
  69. elif [[ "${action}" == 'print' ]]
  70. then
  71. sqlite3 "${filename}" <<<"SELECT queued_urls.*, url_strings.* ${query}"
  72. else
  73. sqlite3 "${filename}" <<<"SELECT COUNT(queued_urls.id) ${query}"
  74. fi