/src/modules/anonfiles.sh

https://code.google.com/ · Shell · 83 lines · 38 code · 14 blank · 31 comment · 6 complexity · f49db54af1e94dc6a905c26138bf3c61 MD5 · raw file

  1. #!/bin/bash
  2. #
  3. # anonfiles.com module
  4. # Copyright (c) 2012 Plowshare team
  5. #
  6. # This file is part of Plowshare.
  7. #
  8. # Plowshare is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # Plowshare is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Plowshare. If not, see <http://www.gnu.org/licenses/>.
  20. MODULE_ANONFILES_REGEXP_URL="https\?://\([[:alnum:]]\+\.\)\?anonfiles\.com/"
  21. MODULE_ANONFILES_DOWNLOAD_OPTIONS=""
  22. MODULE_ANONFILES_DOWNLOAD_RESUME=yes
  23. MODULE_ANONFILES_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no
  24. MODULE_ANONFILES_DOWNLOAD_SUCCESSIVE_INTERVAL=
  25. MODULE_ANONFILES_UPLOAD_OPTIONS=""
  26. MODULE_ANONFILES_UPLOAD_REMOTE_SUPPORT=no
  27. # Output an AnonFiles.com file download URL
  28. # $1: cookie file (unsued here)
  29. # $2: anonfiles url
  30. # stdout: real file download link
  31. anonfiles_download() {
  32. local -r URL=$2
  33. local PAGE FILE_URL FILENAME
  34. PAGE=$(curl "$URL") || return
  35. FILE_URL=$(echo "$PAGE" | parse_attr_quiet 'download_button' href)
  36. if [ -z "$FILE_URL" ]; then
  37. FILE_URL=$(echo "$PAGE" | \
  38. parse_attr_quiet 'image_preview' src) || return
  39. fi
  40. test "$CHECK_LINK" && return 0
  41. FILENAME=$(echo "$PAGE" | parse_tag '<legend' b)
  42. echo "$FILE_URL"
  43. echo "$FILENAME"
  44. }
  45. # Upload a file to AnonFiles.com
  46. # Use API: https://anonfiles.com/api/help
  47. # $1: cookie file (unused here)
  48. # $2: input file (with full path)
  49. # $3: remote filename
  50. # stdout: download
  51. anonfiles_upload() {
  52. local -r FILE=$2
  53. local -r DESTFILE=$3
  54. local -r BASE_URL='https://anonfiles.com/api'
  55. local JSON DL_URL ERR MSG
  56. # Note1: Accepted file typs is very restrictive!
  57. # Note2: -F "file_publish=on" does not work!
  58. JSON=$(curl_with_log \
  59. -F "file=@$FILE;filename=$DESTFILE" "$BASE_URL") || return
  60. DL_URL=$(echo "$JSON" | parse_json_quiet url)
  61. if match_remote_url "$DL_URL"; then
  62. echo "$DL_URL"
  63. return 0
  64. fi
  65. ERR=$(echo "$JSON" | parse_json status)
  66. MSG=$(echo "$JSON" | parse_json msg)
  67. log_error "Unexpected status ($ERR): $MSG"
  68. return $ERR_FATAL
  69. }