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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. ## Shell-Script to harvest links and titles from subreddits
  3. #
  4. ## Author: mynameistony
  5. #
  6. ## Usage: reddit.sh [subreddit]
  7. ## [subreddit] is optional
  8. # Default File Hierarchy
  9. #
  10. #/$HOME/Pictures/reddit -> set by $photoDir
  11. #----------------------/data/
  12. #----------------------------[subreddit]-links.txt -> contains links for each [subreddit]
  13. #----------------------------[subreddit]-titles.txt -> contains titles for each [subreddit]
  14. #--------------------------------------
  15. #----------------------/[subreddit]/ -> contains files downloaded from links (can be moved to a different location before download)
  16. #-----------------------------------[content]
  17. #-----------------------------------[content]
  18. #-----------------------------------[content]
  19. #
  20. #
  21. #
  22. #
  23. ######If subreddit is not passed via arg then prompt######
  24. if [ $# -eq 1 ]
  25. then
  26. sub=$1
  27. else
  28. echo -n "Enter subreddit: "
  29. read sub
  30. #This is a simple GUI for entering a subreddit for the MATE Desktop environment
  31. #
  32. #sub=`matedialog --entry --text="Enter Subreddit"`
  33. fi
  34. ######Some variables for ease######
  35. photoDir="$HOME/Pictures/reddit"
  36. titles="$photoDir/data/$sub-titles.txt"
  37. urls="$photoDir/data/$sub-urls.txt"
  38. ######Check for reddit folder######
  39. test -e $photoDir
  40. if [ $? -ne 0 ]
  41. then
  42. mkdir -p "$photoDir/data"
  43. fi
  44. ######Check for subreddit folder######
  45. test -e "$photoDir/$sub"
  46. if [ $? -ne 0 ]
  47. then
  48. mkdir $photoDir/$sub
  49. fi
  50. ######Get JSON page -> parse for titles -> add to titles file######
  51. echo -n "Getting titles..."
  52. curl -s --no-keepalive -A "Mozilla" "http://www.reddit.com/r/$sub.json" | grep -o "\"title\": \"[-A-Za-z0-9 \"#/\!?()_,'.\[*\]*]*" | sed s/"\"title\": "/""/ | sed s/", \"created_utc\""/""/ > "$titles"
  53. if [ $? -ne 0 ]
  54. then
  55. echo "Couldn't get Titles"
  56. exit 0
  57. fi
  58. titleCount=`cat $titles | wc -l`
  59. echo "Found $titleCount titles."
  60. ######Get JSON page -> parse for links -> add to links file######
  61. echo -n "Getting links..."
  62. curl -s --no-keepalive -A "Mozilla" "http://www.reddit.com/r/$sub.json" | grep "\"url\": \"[-A-Za-z0-9 %:\"#/\!?()_'.\[*\]*]*" -o | grep "http://[A-Za-z0-9./_]*" -o > "$urls"
  63. if [ $? -ne 0 ]
  64. then
  65. echo "Couldn't get links"
  66. exit 0
  67. fi
  68. linkCount=`cat $urls | wc -l`
  69. echo "Found $linkCount links."
  70. ###### View Titles?######
  71. echo -n "View titles? "
  72. read prompt
  73. if [ $prompt == "y" ]
  74. then
  75. cat "$titles"
  76. fi
  77. ###### View URLS?######
  78. echo -n "View URLs? "
  79. read prompt
  80. if [ $prompt == "y" ]
  81. then
  82. cat "$urls"
  83. fi
  84. ######Download files?######
  85. echo -n "Download $linkCount links? "
  86. read prompt
  87. if [ $prompt == "y" ]
  88. then
  89. ######Download to location?######
  90. echo "Downloading to: $photoDir/$sub"
  91. echo -n "Download to a different folder? "
  92. read prompt
  93. if [ $prompt == "y" ]
  94. then
  95. echo -n "Download to: $HOME/"
  96. read newDir
  97. thisDir="$HOME/$newDir"
  98. test -e "$thisDir"
  99. if [ $? -ne 0 ]
  100. then
  101. echo "Creating folder $thisDir"
  102. mkdir -p "$thisDir"
  103. fi
  104. else
  105. thisDir="$photoDir/$sub"
  106. fi
  107. cd "$thisDir"
  108. echo -n "Downloading links..."
  109. wget -q -N -i "$urls"
  110. echo "Downloaded $linkCount files to $thisDir"
  111. ######View downloaded files?######
  112. echo -n "View downloaded files? "
  113. read prompt
  114. if [ $prompt == "y" ]
  115. then
  116. #This is the image viewer for MATE, change accordingly
  117. eom *
  118. fi
  119. fi
  120. echo "Thanks Bye!"
  121. exit 0