Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim232 authored Aug 20, 2020
1 parent c604203 commit e7b2bf9
Showing 1 changed file with 93 additions and 62 deletions.
155 changes: 93 additions & 62 deletions downloader.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,93 @@
redditclientid = ""
redditclientsecret = ""
subreddit = "" #needs to exist

from urllib.parse import urlparse
import aiofiles
import aiohttp
import asyncio
import praw
import os


reddit = praw.Reddit(
client_id=redditclientid,
client_secret=redditclientsecret,
user_agent="Random",
)

def links(limit=10):
print(limit)
hot = reddit.subreddit(subreddit).hot(limit=limit)
submissions = [x for x in hot]

list = []

for submission in submissions:
if submission.url.startswith("http"): # and "i.redd.it" in submission.url:
list.append(submission.url)
# print(list)
else:
print(submission.url)
print(list)
return list


async def down(url):
parsed_url = urlparse(url)
path = parsed_url.path
split_path = path.split("/")

if len(split_path) <= 2:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:

f = await aiofiles.open("downloads/" + split_path[1], mode="wb")
await f.write(await response.read())
return await f.close()
else:
return print(path)

if __name__ == "__main__":
if not os.path.isdir("~/downloads"): os.mkdir('downloads')
limit = int(input("How much ? (Default as 10) : "))
list = links(limit)

error = []
for url in list:
try:
asyncio.run(down(url))
except:
error.append(url)
print(error)
redditclientid = input("Reddit Client Id : ")
redditclientsecret = input("Reddit Client Secret : ")
subreddit = input("Exisiting SubReddit : ")

print("Checking Modules...")

print("Importing Internal Modules...")
import asyncio
import os
from urllib.parse import urlparse
print("Imported Internal Modules!")

print("Importing External Modules...")

try: import aiofiles
except ModuleNotFoundError:
print("aiofiles not found. Installing...")
os.system('python -m pip install -U aiofiles==0.5.0')
import aiofiles

try: import aiohttp
except ModuleNotFoundError:
print("aiohttp not found. Installing...")
os.system('python -m pip install -U aiohttp==3.6.2')
import aiohttp

try: import praw
except ModuleNotFoundError:
print("praw not found. Installing...")
os.system('python -m pip install -U praw==7.1.0')
import praw

print("Imported External Modules!")

reddit = praw.Reddit(
client_id=redditclientid, client_secret=redditclientsecret, user_agent="Random",
)


def links(limit=100):
print(limit)
hot = reddit.subreddit(subreddit).hot(limit=limit)
submissions = [x for x in hot]

list = []

for submission in submissions:
if submission.url.startswith("http"): # and "i.redd.it" in submission.url:
list.append(submission.url)
# print(list)
else:
print(submission.url)
print(list)
return list


async def down(url):
parsed_url = urlparse(url)
path = parsed_url.path
split_path = path.split("/")

if len(split_path) <= 2:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:

f = await aiofiles.open("downloads/" + split_path[1], mode="wb")
await f.write(await response.read())
return await f.close()
else:
return print(path)


print("Checking whether 'downloads' folder exists...")
if not os.path.isdir("~/downloads"):
print("Creating 'downloads' folder...")
os.mkdir("downloads")
print("Created!")
print("Checked!")

limit = int(input("How much images? (Default as 100) : "))
list = links(limit)

print("May take a while...")

error = []
for url in list:
try:
asyncio.run(down(url))
except:
error.append(url)
print(error)

input("Press any key to close this console.")

0 comments on commit e7b2bf9

Please sign in to comment.