Skip to content

Commit

Permalink
Add search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolajlauridsen committed Mar 22, 2018
1 parent 853f01c commit c14421b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Download a bunch of wallpapers from the hot sections of r/wallpapers

And it doesn't even have to be wallpapers! PrawPapers will download images from any subreddit
It doesn't even have to be wallpapers! PrawPapers will download images from any subreddit

Going into a little more detail the script will:

Expand All @@ -27,6 +27,7 @@ Inspirations is drawn from [Daily-Reddit-Wallpaper](https://github.com/ssimunic/
* Multi threaded download for fast downloading
* Database functionality ensuring no redownloads and allows you to keep track of images downloaded
* Scrape by section
* Scrape by subreddit search
* Imgur album support (up to 10 images pr. album, will hopefully be improved)
* Customize behaviour to your liking through an easy to use configurator
* Logging functionality
Expand Down Expand Up @@ -84,4 +85,5 @@ py prawpapers -s MinimalWallpaper -l 100 --log
* --log save a log of posts skipped
* --verbose or -v print skipped posts to console
* --section or -se \<section> Specify which section you want to scrape (hot, new, top, rising)
* --ratiolock or -rlock \<lock strength> Lock downloaded images to a certain aspect ratio, the value of the lock will determine the allowed margin of error, 0 for no lock, 1 for fully locked (only exactly matching aspect ratios), I recommend a value between 0.9 and 1 for decent results.
* --ratiolock or -rlock \<lock strength> Lock downloaded images to a certain aspect ratio, the value of the lock will determine the allowed margin of error, 0 for no lock, 1 for fully locked (only exactly matching aspect ratios), I recommend a value between 0.9 and 1 for decent results.
* --search or -q \<query> scrape all search results of chosen subreddit
13 changes: 10 additions & 3 deletions prawpapers/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def parse_arguments(self):
help="Sort out images with incorrect aspect ratio, 0 for no lock, "
"1 for full lock (Ratio lock: {})".format(self.config['ratiolock']),
default=float(self.config['ratiolock']), type=float)
parser.add_argument('-q', '--search', help="Scrape by search term", default=False,
type=str)
args = parser.parse_args()
if args.ratiolock < 0 or args.ratiolock > 1:
sys.exit("Incorrect ratio lock, please keep it between 0.0 and 1.0 (Currently {})".format(args.ratiolock))
Expand Down Expand Up @@ -135,7 +137,9 @@ def get_submissions(self, subreddit):
"""
section = self.args.section.lower().strip()
limit = self.args.limit
if section == "top":
if self.args.search:
return subreddit.search(self.args.search)
elif section == "top":
return subreddit.top(limit=limit)
elif section == "new":
return subreddit.new(limit=limit)
Expand Down Expand Up @@ -482,8 +486,11 @@ def re_download(self):
def run(self):
"""Run the scraper"""
try:
print('Getting {} posts from the {} section of {}'
.format(self.args.limit, self.args.section, self.args.subreddit))
if not self.args.search:
print('Getting {} posts from the {} section of {}'
.format(self.args.limit, self.args.section, self.args.subreddit))
else:
print('Searching {} for {}'.format(self.args.subreddit, self.args.search))
# Get submissions from sub
submissions = self.get_submissions(self.r.subreddit(self.args.subreddit))
# Handle the submissions and add them to self.posts
Expand Down

0 comments on commit c14421b

Please sign in to comment.