-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f8eb7a
commit cef6634
Showing
1 changed file
with
73 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,14 @@ | |
import os as _os | ||
import random | ||
|
||
|
||
class Redpy: | ||
|
||
def __init__(self, user): | ||
def __init__(self, user_agent): | ||
"""Enter a user agent""" | ||
print("hello") | ||
self.user = user | ||
# 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): | ||
|
@@ -21,151 +23,134 @@ 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 sort_option is None: | ||
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 | ||
|
||
self.json_data = self._generateJSON(subreddit, sort_option) | ||
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 = subreddit.strip('/') | ||
if sort_option == None: | ||
if sort_option is None: | ||
sort_option = '' | ||
subreddit = subreddit.split('/')[-1] if "/" in subreddit else subreddit | ||
|
||
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 [email protected]'}) | ||
|
||
user = {'user-agent':self.user} | ||
res = _requests.get(self.url, headers=user) | ||
|
||
if res.status_code != 200: | ||
print("Could not download") | ||
print(res.status_code) | ||
return | ||
|
||
self._DownloadFiles(res.json(), number) | ||
if self.json_data is None: | ||
self.json_data = self._generateJSON(subreddit, sort_option) | ||
|
||
self._DownloadFiles(self._getImages(self.json_data, number)) | ||
|
||
def _DownloadFiles(self, jsonfile, number_of_files): | ||
image_links = self._getImages(jsonfile, number_of_files) | ||
def _DownloadFiles(self, image_links): | ||
|
||
if not self.createFolder(): | ||
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;', '') | ||
f = _requests.get(image_link) | ||
|
||
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() | ||
if f.status_code == 200: | ||
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") | ||
index+=1 | ||
index += 1 | ||
print("Download complete") | ||
global flag | ||
flag=1 | ||
|
||
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']) | ||
except Exception as e: | ||
print("Exception: ", e) | ||
return images | ||
|
||
def download_videos(self, subreddit, number=5, sort_option=None): | ||
"""Downloads images from subreddit. | ||
if sort_option is None: | ||
sort_option = '' | ||
"""Downloads Videos from subreddit. | ||
subreddit="Name of subreddit" | ||
number=Number of images to be downloaded | ||
number=Number of videos to be downloaded | ||
sort_option=new/hot/top | ||
""" | ||
subreddit = subreddit.strip('/') | ||
if sort_option == None: | ||
sort_option = '' | ||
subreddit = subreddit.split('/')[-1] if "/" in subreddit else subreddit | ||
|
||
self.url = 'https://www.reddit.com/r/' + subreddit + '/'+ sort_option + '.json' | ||
print(self.url) | ||
if self.json_data is None: | ||
self.json_data = self._generateJSON(subreddit, sort_option) | ||
|
||
user = {'user-agent':self.user} | ||
res = _requests.get(self.url, headers=user) | ||
self._DownloadVideoFiles(self._getVideos(self.json_data, number)) | ||
|
||
if res.status_code != 200: | ||
print("Could not download") | ||
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) | ||
def _DownloadVideoFiles(self, video_links): | ||
|
||
if not self.createFolder(): | ||
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;', '') | ||
f = _requests.get(video_link) | ||
|
||
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() | ||
if f.status_code == 200: | ||
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") | ||
index+=1 | ||
index += 1 | ||
print("Download complete") | ||
global flag | ||
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) | ||
print("Exception: ", e) | ||
return videos | ||
|
||
def _getImages(self, jsonfile, number_of_files): | ||
def _generateJSON(self, subreddit, sort_option): | ||
self.url = 'https://www.reddit.com/r/' + \ | ||
subreddit + '/' + sort_option + '.json' | ||
print(self.url) | ||
|
||
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']) | ||
except Exception as e: | ||
print(e) | ||
return images | ||
# session = _requests.Session() | ||
# session.headers.update({'User-Agent': self.user}) | ||
# session.headers.update({'user-agent': 'lmao [email protected]'}) | ||
|
||
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") | ||
raise Exception(f"Error Status Code: {res.status_code}") | ||
return res.json() | ||
|
||
@staticmethod | ||
def createFolder(): | ||
try: | ||
if not _os.path.exists(f'{_os.getcwd()}\\red_media'): | ||
_os.mkdir(f'{_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 | ||
return True | ||
except Exception as e: | ||
print(e) | ||
return False | ||
|
||
flag=0 | ||
|
||
if __name__=='__main__': | ||
import time | ||
|
||
while not flag: | ||
r = Redpy(f'lmao [email protected]') | ||
r.download(subreddit='pics', sort_option='top') | ||
time.sleep(3) |