diff --git a/Package/RedPy/redpy.py b/Package/RedPy/redpy.py index 1a6ad1b..5aa2caa 100644 --- a/Package/RedPy/redpy.py +++ b/Package/RedPy/redpy.py @@ -4,15 +4,15 @@ class Redpy: - def __init__(self, user_agent): """Enter a user agent""" # print("hello") self.user = user_agent self.json_data = None - def download(self, subreddit, number=5, sort_option=None, - num_of_images=0, num_of_videos=0): + def download( + self, subreddit, number=5, sort_option=None, num_of_images=0, num_of_videos=0 + ): """ Download images and videos from given subreddit - subreddit: name of subreddit from where to download @@ -24,12 +24,12 @@ def download(self, subreddit, number=5, sort_option=None, numbers are used """ if sort_option is None: - sort_option = '' - if ((not num_of_images) and (not num_of_videos)): + sort_option = "" + if (not num_of_images) and (not num_of_videos): num_of_images = random.randint(0, number) num_of_videos = number - num_of_images - subreddit = subreddit.split('/')[-1] if "/" in subreddit else subreddit + subreddit = subreddit.split("/")[-1] if "/" in subreddit else subreddit self.json_data = self._generateJSON(subreddit, sort_option) self.download_images(subreddit, num_of_images, sort_option) @@ -37,13 +37,13 @@ def download(self, subreddit, number=5, sort_option=None, def download_images(self, subreddit, number=5, sort_option=None): """Downloads images from subreddit. - subreddit="Name of subreddit" - number=Number of images to be downloaded - sort_option=new/hot/top + subreddit="Name of subreddit" + number=Number of images to be downloaded + sort_option=new/hot/top """ if sort_option is None: - sort_option = '' - subreddit = subreddit.split('/')[-1] if "/" in subreddit else subreddit + sort_option = "" + subreddit = subreddit.split("/")[-1] if "/" in subreddit else subreddit if self.json_data is None: self.json_data = self._generateJSON(subreddit, sort_option) @@ -58,11 +58,12 @@ def _DownloadFiles(self, image_links): index = 0 # used to name the files for image_link in image_links: - image_link = image_link.replace('amp;', '') + image_link = image_link.replace("amp;", "") f = _requests.get(image_link) if f.status_code == 200: - with open(_os.path.join(_os.getcwd(), "red_media", - f"{index}.jpg"), 'wb') as media_file: + with open( + _os.path.join(_os.getcwd(), "red_media", f"{index}.jpg"), "wb" + ) as media_file: for chunk in f.iter_content(100000): media_file.write(chunk) print("Downloaded") @@ -74,21 +75,24 @@ def _getImages(self, jsonfile, number_of_files): images = [] # contains links of images for index in range(number_of_files): try: - images.append(jsonfile['data']['children'][index]['data'] - ['preview']['images'][0]['source']['url']) + images.append( + jsonfile["data"]["children"][index]["data"]["preview"]["images"][0][ + "source" + ]["url"] + ) except Exception as e: print("Exception: ", e) return images def download_videos(self, subreddit, number=5, sort_option=None): if sort_option is None: - sort_option = '' + sort_option = "" """Downloads Videos from subreddit. subreddit="Name of subreddit" number=Number of videos to be downloaded sort_option=new/hot/top """ - subreddit = subreddit.split('/')[-1] if "/" in subreddit else subreddit + subreddit = subreddit.split("/")[-1] if "/" in subreddit else subreddit if self.json_data is None: self.json_data = self._generateJSON(subreddit, sort_option) @@ -103,12 +107,11 @@ def _DownloadVideoFiles(self, video_links): index = 0 # used to name the files for video_link in video_links: - video_link = video_link.replace('amp;', '') + video_link = video_link.replace("amp;", "") f = _requests.get(video_link) if f.status_code == 200: - with open(f'{_os.getcwd()}/red_media/{index}.mp4' - , 'wb') as media_file: + with open(f"{_os.getcwd()}/red_media/{index}.mp4", "wb") as media_file: for chunk in f.iter_content(100000): media_file.write(chunk) print("Downloaded") @@ -120,23 +123,24 @@ def _getVideos(self, jsonfile, number_of_files): for i in range(number_of_files): try: - videos.append(jsonfile['data']['children'][i] - ['data']['preview'] - ['reddit_video_preview']['fallback_url']) + videos.append( + jsonfile["data"]["children"][i]["data"]["preview"][ + "reddit_video_preview" + ]["fallback_url"] + ) except Exception as e: print("Exception: ", e) return videos def _generateJSON(self, subreddit, sort_option): - self.url = 'https://www.reddit.com/r/' + \ - subreddit + '/' + sort_option + '.json' + self.url = "https://www.reddit.com/r/" + subreddit + "/" + sort_option + ".json" print(self.url) -# session = _requests.Session() -# session.headers.update({'User-Agent': self.user}) -# session.headers.update({'user-agent': 'lmao rofl@matapita.com'}) + # session = _requests.Session() + # session.headers.update({'User-Agent': self.user}) + # session.headers.update({'user-agent': 'lmao rofl@matapita.com'}) - res = _requests.get(self.url, headers={'user-agent': self.user}) + res = _requests.get(self.url, headers={"user-agent": self.user}) if res.status_code != 200: print("Could not download") print("Change the User-Agent header") @@ -146,7 +150,7 @@ def _generateJSON(self, subreddit, sort_option): @staticmethod def createFolder(): try: - red_media = _os.path.join(_os.getcwd(), 'red_media') + red_media = _os.path.join(_os.getcwd(), "red_media") if not _os.path.exists(red_media): _os.mkdir(red_media) return True diff --git a/Package/setup.py b/Package/setup.py index bd1b494..290f309 100644 --- a/Package/setup.py +++ b/Package/setup.py @@ -18,5 +18,5 @@ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], - install_requires=['requests'], + install_requires=["requests"], ) diff --git a/redpy.py b/redpy.py index 853f71f..5a165a2 100644 --- a/redpy.py +++ b/redpy.py @@ -2,15 +2,16 @@ import os as _os import random -class Redpy: +class Redpy: def __init__(self, user): """Enter a user agent""" print("hello") self.user = user - def download(self, subreddit, number=5, sort_option=None, - num_of_images=0, num_of_videos=0): + def download( + self, subreddit, number=5, sort_option=None, num_of_images=0, num_of_videos=0 + ): """ Download images and videos from given subreddit - subreddit: name of subreddit from where to download @@ -21,31 +22,30 @@ def download(self, subreddit, number=5, sort_option=None, if num_of_images and num_of_videos are not given, then random numbers are used """ - if ((not num_of_images) and (not num_of_videos)): + if (not num_of_images) and (not num_of_videos): num_of_images = random.randint(0, number) num_of_videos = number - num_of_images self.download_images(subreddit, num_of_images, sort_option) self.download_videos(subreddit, num_of_videos, sort_option) - def download_images(self, subreddit, number=5, sort_option=None): """Downloads images from subreddit. - subreddit="Name of subreddit" - number=Number of images to be downloaded - sort_option=new/hot/top + subreddit="Name of subreddit" + number=Number of images to be downloaded + sort_option=new/hot/top """ - subreddit = subreddit.strip('/') + subreddit = subreddit.strip("/") if sort_option == None: - sort_option = '' + sort_option = "" - self.url = 'https://www.reddit.com/r/' + subreddit + '/'+ sort_option + '.json' + self.url = "https://www.reddit.com/r/" + subreddit + "/" + sort_option + ".json" print(self.url) -## session = _requests.Session() -#### session.headers.update({'User-Agent': self.user}) -## session.headers.update({'user-agent': 'lmao rofl@matapita.com'}) + ## session = _requests.Session() + #### session.headers.update({'User-Agent': self.user}) + ## session.headers.update({'user-agent': 'lmao rofl@matapita.com'}) - user = {'user-agent':self.user} + user = {"user-agent": self.user} res = _requests.get(self.url, headers=user) if res.status_code != 200: @@ -55,7 +55,6 @@ def download_images(self, subreddit, number=5, sort_option=None): self._DownloadFiles(res.json(), number) - def _DownloadFiles(self, jsonfile, number_of_files): image_links = self._getImages(jsonfile, number_of_files) @@ -63,37 +62,37 @@ def _DownloadFiles(self, jsonfile, number_of_files): print("Error creating folder") return - index = 0 #used to name the files + index = 0 # used to name the files for image_link in image_links: - image_link = image_link.replace('amp;', '') + image_link = image_link.replace("amp;", "") f = _requests.get(image_link) - if f.status_code==200: - media_file = open(f'{_os.getcwd()}/red_media/{index}.jpg', 'wb') + if f.status_code == 200: + media_file = open(f"{_os.getcwd()}/red_media/{index}.jpg", "wb") for chunk in f.iter_content(100000): media_file.write(chunk) media_file.close() print("Downloaded") - index+=1 + index += 1 print("Download complete") global flag - flag=1 + flag = 1 def download_videos(self, subreddit, number=5, sort_option=None): """Downloads images from subreddit. - subreddit="Name of subreddit" - number=Number of images to be downloaded - sort_option=new/hot/top + subreddit="Name of subreddit" + number=Number of images to be downloaded + sort_option=new/hot/top """ - subreddit = subreddit.strip('/') + subreddit = subreddit.strip("/") if sort_option == None: - sort_option = '' + sort_option = "" - self.url = 'https://www.reddit.com/r/' + subreddit + '/'+ sort_option + '.json' + self.url = "https://www.reddit.com/r/" + subreddit + "/" + sort_option + ".json" print(self.url) - user = {'user-agent':self.user} + user = {"user-agent": self.user} res = _requests.get(self.url, headers=user) if res.status_code != 200: @@ -101,7 +100,6 @@ def download_videos(self, subreddit, number=5, sort_option=None): print(res.status_code) return self._DownloadVideoFiles(res.json(), number) - def _DownloadVideoFiles(self, jsonfile, number_of_files): video_links = self._getVideos(jsonfile, number_of_files) @@ -110,41 +108,47 @@ def _DownloadVideoFiles(self, jsonfile, number_of_files): print("Error creating folder") return - index = 0 #used to name the files + index = 0 # used to name the files for video_link in video_links: - video_link = video_link.replace('amp;', '') + video_link = video_link.replace("amp;", "") f = _requests.get(video_link) - if f.status_code==200: - media_file = open(f'{_os.getcwd()}/red_media/{index}.mp4', 'wb') + if f.status_code == 200: + media_file = open(f"{_os.getcwd()}/red_media/{index}.mp4", "wb") for chunk in f.iter_content(100000): media_file.write(chunk) media_file.close() print("Downloaded") - index+=1 + index += 1 print("Download complete") global flag - flag=1 - - - + flag = 1 + def _getVideos(self, jsonfile, number_of_files): videos = [] for i in range(number_of_files): try: - videos.append(jsonfile['data']['children'][i]['data']['preview']['reddit_video_preview']['fallback_url']) + videos.append( + jsonfile["data"]["children"][i]["data"]["preview"][ + "reddit_video_preview" + ]["fallback_url"] + ) except Exception as e: print(e) return videos def _getImages(self, jsonfile, number_of_files): - images = [] #contains links of images + images = [] # contains links of images for index in range(number_of_files): try: - images.append(jsonfile['data']['children'][index]['data']['preview']['images'][0]['source']['url']) + images.append( + jsonfile["data"]["children"][index]["data"]["preview"]["images"][0][ + "source" + ]["url"] + ) except Exception as e: print(e) return images @@ -152,20 +156,21 @@ def _getImages(self, jsonfile, number_of_files): @staticmethod def createFolder(): try: - if not _os.path.exists(f'{_os.getcwd()}\\red_media'): - _os.mkdir(f'{_os.getcwd()}\\red_media') + if not _os.path.exists(f"{_os.getcwd()}\\red_media"): + _os.mkdir(f"{_os.getcwd()}\\red_media") return True return True except Exception as e: print(e) return False -flag=0 -if __name__=='__main__': +flag = 0 + +if __name__ == "__main__": import time while not flag: - r = Redpy(f'lmao rofl@matapita.com') - r.download(subreddit='pics', sort_option='top') + r = Redpy(f"lmao rofl@matapita.com") + r.download(subreddit="pics", sort_option="top") time.sleep(3)