Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code with black #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions Package/RedPy/redpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,26 +24,26 @@ 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)
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
"""
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)
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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 [email protected]'})
# 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})
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")
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=['requests'],
install_requires=["requests"],
)
101 changes: 53 additions & 48 deletions redpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 [email protected]'})
## session = _requests.Session()
#### session.headers.update({'User-Agent': self.user})
## session.headers.update({'user-agent': 'lmao [email protected]'})

user = {'user-agent':self.user}
user = {"user-agent": self.user}
res = _requests.get(self.url, headers=user)

if res.status_code != 200:
Expand All @@ -55,53 +55,51 @@ 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)

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;', '')
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:
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)
Expand All @@ -110,62 +108,69 @@ 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

@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 [email protected]')
r.download(subreddit='pics', sort_option='top')
r = Redpy(f"lmao [email protected]")
r.download(subreddit="pics", sort_option="top")
time.sleep(3)