Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #!/bin/bash
  2. IFS=$'\n'
  3.  
  4.  
  5. randomGif() {
  6. apiKey="dc6zaTOxFJmzC"
  7. #First argument passed to this function is searched for in giphy. Should be in format word+word+otherword
  8. search="$1"
  9. apiSearchEndPoint="http://api.giphy.com/v1/gifs/search"
  10. req=$( curl -s "$apiSearchEndPoint?q=$search&api_key=$apiKey")
  11. numberOfResultsReturned=$(echo -e "$req" | jq .data | grep '"id":' | wc -l)
  12.  
  13. #Start at 0 not 1
  14. ((numberOfResultsReturned--))
  15.  
  16. #Get a number between 0 and 1 minus the number of results returned
  17. randomNumber=$(shuf -n1 -i0-$numberOfResultsReturned)
  18.  
  19.  
  20. echo -e "$req" | jq .data[$randomNumber].url
  21.  
  22. }
  23.  
  24.  
  25.  
  26. htllMakeAuthReq() {
  27. #Load the cookie and make the request to the first arg past to this function
  28. req=$(curl -s -b $cookieFile $1)
  29.  
  30. }
  31.  
  32. htllLogin() {
  33. username='username'
  34. password='password'
  35. cookieFile='htll-cookie.txt'
  36. loginUrl="https://hightechlowlife.eu/board/login/login"
  37.  
  38. #Login and get cookie silently
  39. loginReq=$(curl -s -c $cookieFile -d "login=$username&password=$password" $loginUrl)
  40.  
  41. }
  42.  
  43. getMessages() {
  44. #locating messages
  45. message=$(echo -e "$req" | grep "siropuChatMessage\">.*</span" | grep -ioP '">.*</span>')
  46.  
  47. #Remove some of the info messages
  48. message=$(echo -e "$message" | grep -v "</a> is our newest member. Welcome!</span>")
  49.  
  50. #Remove more of the info messages
  51. message=$(echo -e "$message" | grep -v "</a> has started a new thread called &quot;<a href=")
  52.  
  53. #Stepping to the message
  54. message=$(echo -e "$message" | cut -d">" -f2- | sed -E 's/<span .*?">//')
  55.  
  56. #Get rid of span tags
  57. message=$(echo -e "$message" | sed 's/<\/span>//g' | sed s'/<span>//g')
  58.  
  59. #Convert html apostraphe
  60. message=$(echo -e "$message" | sed "s/&#039;/'/g")
  61.  
  62. #Convert html qoute
  63. message=$(echo -e "$message" | sed 's/&quot;/"/g')
  64.  
  65. #Remove html around links
  66. message=$(echo -e "$message" | sed -E 's/<a href=.*?">//' | sed -E 's/<\/a>//')
  67.  
  68. #Convert html greater than sign
  69. message=$(echo -e "$message" | sed 's/&gt;/>/g')
  70. }
  71.  
  72. getUsernames() {
  73. usernames=$(echo -e "$req" | grep -v "siropuChatBot" | grep -ioP "data-author=\".*?\">" | cut -d'"' -f2)
  74. }
  75.  
  76. getShouty() {
  77. #Get current shoutbox
  78. htllMakeAuthReq $shout
  79.  
  80. #parse for messages
  81. getMessages
  82.  
  83. #parse for usernames
  84. getUsernames
  85.  
  86.  
  87. line=1
  88.  
  89. #print shouty
  90. for i in $(echo -e "$message"); do
  91. currentUser=$(echo -e "$usernames" | sed "$line"'!d')
  92. echo "$currentUser: $i"
  93. ((line++))
  94. done
  95. }
  96.  
  97. #Login
  98. htllLogin
  99.  
  100. #Get shouty
  101. shout="https://hightechlowlife.eu/board/chat/"
  102.  
  103.  
  104.  
  105. while :; do
  106.  
  107. latestShouty=$(getShouty)
  108.  
  109. if [ -z "$oldShouty" ]; then
  110. echo -e "$latestShouty"
  111. fi
  112.  
  113. if [ "$latestShouty" != "$oldShouty" ]; then
  114. if [ -n "$oldShouty" ]; then
  115. lastLineOfOldShouty=$(echo -e "$oldShouty" | tail -1)
  116.  
  117. locationOfLastLineinNewShouty=$(echo -e "$latestShouty" | grep -n "$lastLineOfOldShouty" | cut -f1 -d:)
  118. newShouts=$(echo -e "$latestShouty" | sed '1,'"$locationOfLastLineinNewShouty"'d')
  119. # echo -e "$newShouts"
  120.  
  121. for i in $(echo -e "$newShouts"); do
  122. echo "$i"
  123. if [ 4 -gt $(shuf -n1 -i1-10) ]; then
  124. searchFriendly=$(echo "$i" | tr ' ' '+')
  125. gifURL=$(randomGif $searchFriendly)
  126. echo "GIPHY: $gifURL" | tr -d '"'
  127. fi
  128. done
  129. fi
  130. fi
  131. sleep 2
  132. oldShouty="$latestShouty"
  133. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement