Advertisement
pizzahut2

parse_purchases.sh

Mar 17th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script retrieves ratings and rating counts from a user's Itch.io library.
  4.  
  5. i=1
  6. rm -v json/my-purchases*.json
  7. while true; do
  8. echo "Reading purchases page $i."
  9.  
  10. # Use your web browser's dev tools to copy this link as curl (POSIX) address:
  11. # https://itch.io/my-purchases?page=1&format=json
  12. # Then insert the headers in the following command:
  13. curl -s 'https://itch.io/my-purchases?page='$i'&format=json' ***headers go here*** >json/my-purchases$i.json
  14.  
  15. sleep 1
  16. if grep '"num_items":0' json/my-purchases$i.json; then
  17. rm -v json/my-purchases$i.json
  18. break
  19. fi
  20. ((i++))
  21. done
  22.  
  23. # Notes
  24. # - Using find because it can sort file names numerically. So file1, file2, file10 instead of file1, file10, file2.
  25. # - The awk command prints unique lines only. So like sort -u, but without sorting.
  26. cat $(find json -name "*.json") | tr -d '\\' | grep -o 'https://[^.]*\.itch\.io/[^/]*' | awk '!x[$0]++' > "url_list.txt"
  27.  
  28. rm -v parse_purchases.txt
  29.  
  30. for url in $(cat "url_list.txt")
  31. do
  32.  
  33. pathname=$(echo "$url" | cut -d / -f 4)
  34.  
  35. mkdir -p "html"
  36. filename="html/$pathname.html"
  37.  
  38. if test ! -e "$filename"; then
  39. echo "$pathname"
  40. wget -O "$filename" -q "$url"
  41. sleep 1
  42. fi
  43.  
  44. ratingCount=$(grep -oP 'ratingCount":\d*' "$filename" | cut -d : -f 2)
  45. if test -z "$ratingCount"
  46. then ratingCount="0"
  47. fi
  48.  
  49. ratingValue=$(grep -oP 'ratingValue":"[^"]*' "$filename" | cut -d '"' -f 3)
  50. if test -z "$ratingValue"
  51. then ratingValue="0.0"
  52. fi
  53.  
  54. echo -e "$ratingValue\t$ratingCount\t$pathname\t$url" >>parse_purchases.txt
  55.  
  56. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement