Advertisement
Guest User

Bash script to get reddit user's karma. [CURL version]

a guest
Oct 20th, 2012
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.15 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##########################
  4. ##~REDDIT KARMA GRABBER~##
  5. ## v1.2 using curl not  ##
  6. ## lynx for beter comp- ##
  7. ## -atibility. NOTE:    ##
  8. ## THIS SCRIPT WILL NOT ##
  9. ## WORK ON A NON-LINUX  ##
  10. ## OPERATING SYSTEM.    ##
  11. ##----------------------##
  12. ##~~~~~2012 xereeto~~~~~##
  13. ##########################
  14.  
  15. #checks if the script was invoked with a redditor as an argument, if not it
  16. #asks for one.
  17. if [ -z "$1" ]; then
  18.     read -p "Which redditor would you like to look up?>" REDDITOR
  19. else
  20.     REDDITOR=$1
  21. fi
  22.  
  23. #SUPER-HAPPY-FUNTIME-EASTER-EGG!
  24. #yes I know it's easy to decode, but what the hell.
  25. if [[ "${REDDITOR,,}" == `echo "eGVyZWV0bwo=" | base64 -d` ]]; then
  26.         MESSAGE=`echo "RUFTVEVSIEVHRyEgVGhhbmtzIGZvciB1c2luZyBteSBzY3JpcHQhCg==" | base64 -d`
  27.         echo -e "\e[1;32m"
  28.         echo "****************************************"
  29.         echo "${MESSAGE}"
  30.         echo "****************************************"
  31.         echo -e "\e[00m"
  32. fi
  33. #</easteregg>
  34.  
  35. #code to get the json for the redditor
  36. I_CAN_HAZ_INFO=$(curl --silent --fail --location "http://reddit.com/user/$REDDITOR/about.json")
  37.  
  38. #check if redditor exists
  39. IS_REAL=$(echo "$I_CAN_HAZ_INFO" | grep link_karma)
  40. if [ -z "$IS_REAL" ]; then
  41.     echo -e "\e[1;31mERROR:\e[00m redditor \"$REDDITOR\" not found."
  42.     exit 1
  43. fi
  44.  
  45. #inserts line breaks where the commas are so I can grep the list
  46. I_CAN_HAZ_INFO=$(echo "$I_CAN_HAZ_INFO" | tr "," "\n")
  47.  
  48. #first and foremost, get the redditor's name with correct capitalization
  49. REDDITOR=$(echo "$I_CAN_HAZ_INFO" | grep "\"name\":")
  50. REDDITOR=${REDDITOR//\"/}
  51. REDDITOR=${REDDITOR:7}
  52.  
  53. #gets the timestamp the account was created
  54. REDDIT_TIME=$(echo "$I_CAN_HAZ_INFO" | grep created_utc)
  55. REDDIT_TIME=${REDDIT_TIME:16}
  56.  
  57. #converts the timestamp to a proper date
  58. REDDIT_TIME="$(date -ud @$REDDIT_TIME +%c)"
  59.  
  60. #gets age of the account in days
  61. let DIFF=(`date +%s -d now`-`date +%s -d "$REDDIT_TIME"`)/86400
  62.  
  63. #self explanatory
  64. MYD="$DIFF days"
  65. if [ $DIFF -ge 30 ]; then
  66.     #gets account age in months if $DIFF >= 30 days
  67.     let DIFF=$DIFF/30
  68.     MYD="$DIFF months"
  69.     if [ $DIFF -ge 12 ]; then
  70.         #gets account age in years if $DIFF >= 12 months
  71.         let DIFF=$DIFF/12
  72.         MYD="$DIFF years"
  73.     fi
  74. fi
  75.  
  76. #finds link karma
  77. LINK_K=$(echo "$I_CAN_HAZ_INFO" | grep link_karma)
  78. LINK_K=${LINK_K:15}
  79.  
  80. #inserts a thousands separator to make it look nice.
  81. LINK_K=$(printf "%'d" $LINK_K)
  82. echo
  83. echo "$REDDITOR has:"
  84. echo -e "   $LINK_K \e[1;31mlink karma\e[00m"
  85.  
  86. #finds comment karma
  87. COMMENT_K=$(echo "$I_CAN_HAZ_INFO" | grep comment_karma)
  88. COMMENT_K=${COMMENT_K:18}
  89.  
  90. #inserts a thousands separator to make it look nice.
  91. COMMENT_K=$(printf "%'d" $COMMENT_K)
  92. echo -e "   $COMMENT_K \e[1;36mcomment karma\e[00m"
  93. echo -e "and has been a redditor for \e[1;33m${MYD}.\e[00m"
  94.  
  95. #searches for reddit gold.
  96. echo
  97. GOLD=$(echo "$I_CAN_HAZ_INFO" | grep "\"is_gold\": true")
  98. if [ -z "$GOLD" ]; then
  99.     echo -e "$REDDITOR \e[1;31mdoes not\e[00m have reddit gold."
  100. else
  101.     echo -e "$REDDITOR \e[1;32mdoes\e[00m have reddit gold."
  102. fi
  103.  
  104. echo
  105.  
  106. #############################
  107. ## for best results, place ##
  108. ## in your PATH and call   ##
  109. ## the script "karma".     ##
  110. #############################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement