The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

75 lines
2.2 KiB

  1. #!/bin/bash
  2. # Read a list of URLs from stdin, replace suitable social media URLs with correctly capitalised version
  3. errorUrls=()
  4. while read -r url
  5. do
  6. if [[ "${url}" == '* '* ]]
  7. then
  8. prefix="${url::2}"
  9. url="${url:2}"
  10. else
  11. prefix=""
  12. fi
  13. if [[ "${url}" =~ ^https?://((www|m|[a-z][a-z]-[a-z][a-z]).)?facebook.com/([^/]+/?(\?|$)|pages/[^/]+/[0-9]+/?(\?|$)|profile\.php\?id=[0-9]+(&|$)) ]]
  14. then
  15. if [[ "${url}" == *profile.php* ]]
  16. then
  17. url="${url%%&*}"
  18. else
  19. url="${url%%\?*}"
  20. fi
  21. page="$(curl -sL -A 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36' -H 'Accept-Language: en-US,en;q=0.5' "https://www.facebook.com/${url#*facebook.com/}")"
  22. user="$(grep -Po '<div\s[^>]*(?<=\s)data-key\s*=\s*"tab_home".*?</div>' <<< "${page}" | grep -Po '<a\s[^>]*(?<=\s)href="/\K[^/]+')"
  23. if [[ "${user}" ]]
  24. then
  25. echo "${prefix}https://www.facebook.com/${user}/"
  26. continue
  27. else
  28. if grep -q 'id="pagelet_loggedout_sign_up"' <<< "${page}"
  29. then
  30. # Profile page which is only visible when logged in
  31. # Extract canonical URL
  32. user="$(grep -Po '<link rel="canonical" href="\K[^"]+' <<< "${page}")"
  33. if [[ "${user}" ]]
  34. then
  35. echo "${prefix}${user}"
  36. continue
  37. fi
  38. fi
  39. fi
  40. errorUrls+=("${url}")
  41. echo "${prefix}${url}"
  42. elif [[ "${url}" =~ ^https?://(www\.)?twitter\.com/[^/]+/?(\?.*)?$ ]]
  43. then
  44. url="${url%%\?*}"
  45. url="${url%/}"
  46. unnormalisedUser="${url##*/}"
  47. user="$(curl -sL -A 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' "https://twitter.com/${unnormalisedUser}" | grep -Po '<a class="([^"]*\s)?ProfileHeaderCard-screennameLink(\s[^"]*)?" href="/\K[^/"]+(?=")')"
  48. if [[ "${user}" ]]
  49. then
  50. echo "${prefix}https://twitter.com/${user}"
  51. else
  52. errorUrls+=("${url}")
  53. echo "${prefix}${url}"
  54. fi
  55. elif [[ "${url}" =~ ^https?://(www\.)?instagram\.com/[^/]+/?$ ]]
  56. then
  57. user="${url%/}"
  58. user="${user##*/}"
  59. echo "${prefix}https://www.instagram.com/${user,,}/"
  60. else
  61. echo "${prefix}${url}"
  62. fi
  63. done
  64. if [[ ${#errorUrls[@]} -gt 0 ]]
  65. then
  66. echo "" >&2
  67. echo "Failed to process URLs:" >&2
  68. for errorUrl in "${errorUrls[@]}"
  69. do
  70. echo "${errorUrl}" >&2
  71. done
  72. fi