|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Reddit Wallscraper |
| 4 | +Course: CS 41 |
| 5 | +Name: Danny Du |
| 6 | +SUNet: dannydu |
| 7 | +
|
| 8 | +This is a wallpaper scraping program that queries data from certain subreddits. |
| 9 | +""" |
| 10 | +import wallscraperutils as utils |
| 11 | +import requests |
| 12 | +import json |
| 13 | +import sys |
| 14 | +import os |
| 15 | + |
| 16 | +BASE_URL = 'https://reddit.com/r/' |
| 17 | + |
| 18 | + |
| 19 | +class RedditPost: |
| 20 | + def __init__(self, data): |
| 21 | + self.ok = True |
| 22 | + |
| 23 | + #essential information |
| 24 | + self.title = data['title'] |
| 25 | + self.img_url = data['url'] |
| 26 | + self.author = data['author'] |
| 27 | + self.post_hint = data['post_hint'] |
| 28 | + self.extension = '.' + self.img_url.split('.')[-1] |
| 29 | + |
| 30 | + #checks for unknown formats |
| 31 | + if len(self.extension) != 4: |
| 32 | + self.ok = False |
| 33 | + |
| 34 | + #resolution-related properties |
| 35 | + self.width = data['preview']['images'][0]['source']['width'] |
| 36 | + self.height = data['preview']['images'][0]['source']['height'] |
| 37 | + self.aspect_ratio = utils.get_aspect_ratio(self.width, self.height) |
| 38 | + |
| 39 | + #score-related properties |
| 40 | + self.score = data['score'] |
| 41 | + self.ups = data['ups'] |
| 42 | + self.downs = data['downs'] |
| 43 | + self.num_comments = data['num_comments'] |
| 44 | + |
| 45 | + #other |
| 46 | + self.media = data['media'] |
| 47 | + self.is_video = data['is_video'] |
| 48 | + self.json_data = data |
| 49 | + |
| 50 | + |
| 51 | + def add_x(self, num1, num2): |
| 52 | + #concatenates two items together in their string form, with an 'x' in the middle |
| 53 | + return str(num1) + 'x' + str(num2) |
| 54 | + |
| 55 | + def download(self): |
| 56 | + if self.ok: |
| 57 | + |
| 58 | + aspect_ratio_path = 'wallpapers/' + self.add_x(*self.aspect_ratio) + '/' |
| 59 | + resolution_path = aspect_ratio_path + self.add_x(self.width, self.height) + '/' |
| 60 | + |
| 61 | + #create new directories if necessary in accordance to aspect ratio and resolution |
| 62 | + if not os.path.isdir(aspect_ratio_path): |
| 63 | + os.mkdir(aspect_ratio_path) |
| 64 | + if not os.path.isdir(resolution_path): |
| 65 | + os.mkdir(resolution_path) |
| 66 | + |
| 67 | + #makes request to url of the image |
| 68 | + img = requests.get(self.img_url) |
| 69 | + |
| 70 | + #write the image locally |
| 71 | + file_path = resolution_path + self.title + self.extension |
| 72 | + if not os.path.isfile(file_path): |
| 73 | + with open(file_path, 'wb') as f: |
| 74 | + f.write(img.content) |
| 75 | + return True |
| 76 | + else: |
| 77 | + print('whoops, that image already exists!') |
| 78 | + else: |
| 79 | + print('whoops, that\'s not an image!') |
| 80 | + return False |
| 81 | + |
| 82 | + def name(self): |
| 83 | + return self.title |
| 84 | + |
| 85 | + def __str__(self): |
| 86 | + return self.title + ' (' + str(self.score) + '), posted by: ' + self.author +', post_hint: ' + str(post_hint) |
| 87 | + |
| 88 | + |
| 89 | +def query(to_query): |
| 90 | + try: |
| 91 | + response = requests.get( |
| 92 | + BASE_URL + to_query + '/.json', |
| 93 | + headers={'User-Agent': 'Wallscraper Script by @dannydu'} |
| 94 | + ) |
| 95 | + #catchs connection error in case of failed internet |
| 96 | + except requests.ConnectionError: |
| 97 | + print('no internet connection!') |
| 98 | + return -1 |
| 99 | + |
| 100 | + if response.status_code == 404: |
| 101 | + print('page not found!') |
| 102 | + return -1 |
| 103 | + |
| 104 | + #if the request was successful |
| 105 | + if response.status_code == 200 and response.ok: |
| 106 | + data = response.json()['data']['children'] |
| 107 | + if len(data) == 0: |
| 108 | + print('subreddit empty/not found!') |
| 109 | + return -1 |
| 110 | + else: |
| 111 | + print('reddit has responded with error code: ', response.status_code) |
| 112 | + return -1 |
| 113 | + |
| 114 | + return data |
| 115 | + |
| 116 | + |
| 117 | +def main(): |
| 118 | + #command line functionality |
| 119 | + if len(sys.argv) == 1: |
| 120 | + subreddit = 'wallpapers' |
| 121 | + else: |
| 122 | + subreddit = sys.argv[1] |
| 123 | + |
| 124 | + #makes query |
| 125 | + query_data = query(subreddit) |
| 126 | + #if anythign wrong with query, gracefully exit program with error code -1 |
| 127 | + if query_data == -1: |
| 128 | + print('exiting now') |
| 129 | + return -1 |
| 130 | + |
| 131 | + #converts relevant python dictionaries into RedditPost objects |
| 132 | + posts = [RedditPost(x['data']) for x in query_data if 'preview' in x['data']] |
| 133 | + |
| 134 | + #download the images themselves |
| 135 | + num_downloaded = 0 |
| 136 | + for post in posts: |
| 137 | + print('downloading ' + post.name() + "... ", end='') |
| 138 | + if post.download(): |
| 139 | + num_downloaded += 1 |
| 140 | + print('done!') |
| 141 | + print('images downloaded: ', num_downloaded, ' (of {} total)'.format(len(posts)) ) |
| 142 | + |
| 143 | +if __name__ == '__main__': |
| 144 | + main() |
0 commit comments