33 Commits

Author SHA1 Message Date
Ali Parlakçı
91d71565cc Update script.py 2018-07-25 18:30:48 +03:00
Ali Parlakci
c7b7361ded Update version 2018-07-25 18:27:58 +03:00
Ali Parlakci
cd81a6c38b Update changelog 2018-07-25 13:53:33 +03:00
Ali Parlakçı
1623722138 Merge pull request #42 from aliparlakci/verboseMode
Added Verbose mode
2018-07-25 13:52:34 +03:00
Ali Parlakci
dad5669441 Typo fix 2018-07-25 13:50:11 +03:00
Ali Parlakci
35d54d1eb1 Stylize 2018-07-25 13:48:30 +03:00
Ali Parlakci
394b864d86 Updated FAQ 2018-07-25 13:48:02 +03:00
Ali Parlakci
837281c3c6 Added verbose mode 2018-07-25 13:40:06 +03:00
Ali Parlakci
e6b648d8b3 Update changelog 2018-07-25 12:25:39 +03:00
Ali Parlakci
cfaf2de7db Stylize the console output 2018-07-25 12:24:50 +03:00
Ali Parlakci
80546d7094 Update version 2018-07-25 11:36:58 +03:00
Ali Parlakci
139a81a0e7 Merge branch 'master' of https://github.com/aliparlakci/bulk-downloader-for-reddit 2018-07-25 09:29:48 +03:00
Ali Parlakci
9bb0a5da7f Added delays for imgur rate limit 2018-07-25 09:27:41 +03:00
Ali Parlakçı
6f2273f182 Add file name formatting doc 2018-07-24 23:41:02 +03:00
Ali Parlakci
b5d6165802 Update version 2018-07-24 22:13:38 +03:00
Ali Parlakci
b98815376f Bug fix 2018-07-24 22:13:11 +03:00
Ali Parlakci
d9586f99b8 Use else in try blocks 2018-07-24 22:11:12 +03:00
Ali Parlakci
76711892a2 Merge branch 'master' of https://github.com/aliparlakci/bulk-downloader-for-reddit 2018-07-24 22:10:28 +03:00
Ali Parlakci
bfea548eab Print credits in the same line 2018-07-24 22:10:19 +03:00
Ali Parlakçı
2e852db4c3 Typo fix 2018-07-24 19:45:37 +03:00
Ali Parlakci
8ac02e7aff Update version 2018-07-24 19:42:38 +03:00
Ali Parlakci
5eccf4dd3d Update changelog 2018-07-24 19:35:49 +03:00
Ali Parlakçı
7a68ff3efa Merge pull request #41 from aliparlakci/changePostNames
Add submitter to file names
2018-07-24 19:34:02 +03:00
Ali Parlakçı
3ea2e16b62 Merge branch 'master' into changePostNames 2018-07-24 19:33:38 +03:00
Ali Parlakci
fc6787aa28 Update changelog 2018-07-24 19:28:48 +03:00
Ali Parlakci
21533bb78c Improved exception handling 2018-07-24 19:27:52 +03:00
Ali Parlakci
1781ab8ffe Update changelog 2018-07-24 19:10:34 +03:00
Ali Parlakci
821383c465 Deleted # char from file names 2018-07-24 19:09:45 +03:00
Ali Parlakci
9d0fdc7521 Add OP's name first 2018-07-24 18:55:33 +03:00
Ali Parlakci
0387dd5243 Tweaked 'What it can do' 2018-07-24 14:16:46 +03:00
Ali Parlakci
93732b0367 Little refactoring 2018-07-24 13:17:37 +03:00
Ali Parlakci
400ce01918 Added older version support 2018-07-24 13:17:14 +03:00
Ali Parlakci
ccedac4bdc Add submitter to file name 2018-07-24 12:44:53 +03:00
6 changed files with 274 additions and 169 deletions

View File

@@ -6,13 +6,12 @@ This program downloads imgur, gfycat and direct image and video links of saved p
## What it can do ## What it can do
- Can get posts from: frontpage, subreddits, multireddits, redditor's submissions, upvoted and saved posts; search results or just plain reddit links - Can get posts from: frontpage, subreddits, multireddits, redditor's submissions, upvoted and saved posts; search results or just plain reddit links
- Sorts posts by hot, top, new and so on - Sorts posts by hot, top, new and so on
- Downloads imgur albums, gfycat links, [self posts](#how-do-i-open-self-post-files) and any link to a direct image - Downloads **REDDIT** images and videos, **IMGUR** images and albums, **GFYCAT** links, **EROME** images and albums, **SELF POSTS** and any link to a **DIRECT IMAGE**
- Skips the existing ones - Skips the existing ones
- Puts post titles to file's name - Puts post title and OP's name in file's name
- Puts every post to its subreddit's folder - Puts every post to its subreddit's folder
- Saves a reusable copy of posts' details that are found so that they can be re-downloaded again - Saves a reusable copy of posts' details that are found so that they can be re-downloaded again
- Logs failed ones in a file to so that you can try to download them later - Logs failed ones in a file to so that you can try to download them later
- Can run with double-clicking on Windows
## [Download the latest release](https://github.com/aliparlakci/bulk-downloader-for-reddit/releases/latest) ## [Download the latest release](https://github.com/aliparlakci/bulk-downloader-for-reddit/releases/latest)
@@ -42,6 +41,19 @@ It should redirect to a page which shows your **imgur_client_id** and **imgur_cl
\* Select **OAuth 2 authorization without a callback URL** first then select **Anonymous usage without user authorization** if it says *Authorization callback URL: required* \* Select **OAuth 2 authorization without a callback URL** first then select **Anonymous usage without user authorization** if it says *Authorization callback URL: required*
## FAQ ## FAQ
### What do the dots resemble when getting posts?
- Each dot means that 100 posts are scanned.
### Getting posts is taking too long.
- You can press Ctrl+C to interrupt it and start downloading.
### How downloaded files' names are formatted?
- Images that are not belong to an album or self posts are formatted as **`[SUBMITTER NAME]_[POST TITLE]_[REDDIT ID]`**.
You can use *reddit id* to go to post's reddit page by going to link **reddit.com/[REDDIT ID]**
- An image in an imgur album is formatted as **`[ITEM NUMBER]_[IMAGE TITLE]_[IMGUR ID]`**
Similarly, you can use *imgur id* to go to image's imgur page by going to link **imgur.com/[IMGUR ID]**.
### How do I open self post files? ### How do I open self post files?
- Self posts are held at reddit as styled with markdown. So, the script downloads them as they are in order not to lose their stylings. - Self posts are held at reddit as styled with markdown. So, the script downloads them as they are in order not to lose their stylings.
However, there is a [great Chrome extension](https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk) for viewing Markdown files with its styling. Install it and open the files with [Chrome](https://www.google.com/intl/tr/chrome/). However, there is a [great Chrome extension](https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk) for viewing Markdown files with its styling. Install it and open the files with [Chrome](https://www.google.com/intl/tr/chrome/).
@@ -53,6 +65,15 @@ It should redirect to a page which shows your **imgur_client_id** and **imgur_cl
them, there. them, there.
## Changes on *master* ## Changes on *master*
### [25/07/2018](https://github.com/aliparlakci/bulk-downloader-for-reddit/tree/1623722138bad80ae39ffcd5fb38baf80680deac)
- Added verbose mode
- Stylize the console output
### [24/07/2018](https://github.com/aliparlakci/bulk-downloader-for-reddit/tree/7a68ff3efac9939f9574c2cef6184b92edb135f4)
- Added OP's name to file names (backwards compatible)
- Deleted # char from file names (backwards compatible)
- Improved exception handling
### [23/07/2018](https://github.com/aliparlakci/bulk-downloader-for-reddit/tree/7314e17125aa78fd4e6b28e26fda7ec7db7e0147) ### [23/07/2018](https://github.com/aliparlakci/bulk-downloader-for-reddit/tree/7314e17125aa78fd4e6b28e26fda7ec7db7e0147)
- Split download() function - Split download() function
- Added erome support - Added erome support

View File

@@ -10,13 +10,13 @@ usage: script.py [-h] [--directory DIRECTORY] [--link link] [--saved]
[--subreddit SUBREDDIT [SUBREDDIT ...]] [--subreddit SUBREDDIT [SUBREDDIT ...]]
[--multireddit MULTIREDDIT] [--user redditor] [--multireddit MULTIREDDIT] [--user redditor]
[--search query] [--sort SORT TYPE] [--limit Limit] [--search query] [--sort SORT TYPE] [--limit Limit]
[--time TIME_LIMIT] [--NoDownload] [--time TIME_LIMIT] [--NoDownload] [--verbose]
This program downloads media from reddit posts This program downloads media from reddit posts
optional arguments: optional arguments:
-h, --help show this help message and exit -h, --help show this help message and exit
--directory DIRECTORY --directory DIRECTORY, -d DIRECTORY
Specifies the directory where posts will be downloaded Specifies the directory where posts will be downloaded
to to
--link link, -l link Get posts from link --link link, -l link Get posts from link
@@ -40,6 +40,7 @@ optional arguments:
all all
--NoDownload Just gets the posts and store them in a file for --NoDownload Just gets the posts and store them in a file for
downloading later downloading later
--verbose, -v Verbose Mode
``` ```
# Examples # Examples

111
script.py
View File

@@ -22,7 +22,7 @@ from src.tools import (GLOBAL, createLogFile, jsonFile, nameCorrector,
__author__ = "Ali Parlakci" __author__ = "Ali Parlakci"
__license__ = "GPL" __license__ = "GPL"
__version__ = "1.4.0" __version__ = "1.5.2"
__maintainer__ = "Ali Parlakci" __maintainer__ = "Ali Parlakci"
__email__ = "parlakciali@gmail.com" __email__ = "parlakciali@gmail.com"
@@ -62,7 +62,7 @@ def parseArguments(arguments=[]):
description="This program downloads " \ description="This program downloads " \
"media from reddit " \ "media from reddit " \
"posts") "posts")
parser.add_argument("--directory", parser.add_argument("--directory","-d",
help="Specifies the directory where posts will be " \ help="Specifies the directory where posts will be " \
"downloaded to", "downloaded to",
metavar="DIRECTORY") metavar="DIRECTORY")
@@ -144,6 +144,11 @@ def parseArguments(arguments=[]):
action="store_true", action="store_true",
default=False) default=False)
parser.add_argument("--verbose","-v",
help="Verbose Mode",
action="store_true",
default=False)
if arguments == []: if arguments == []:
return parser.parse_args() return parser.parse_args()
@@ -423,21 +428,36 @@ def postFromLog(fileName):
return posts return posts
def postExists(POST): def isPostExists(POST):
"""Figure out a file's name and checks if the file already exists""" """Figure out a file's name and checks if the file already exists"""
title = nameCorrector(POST['postTitle']) title = nameCorrector(POST['postTitle'])
FILENAME = title + "_" + POST['postId']
PATH = GLOBAL.directory / POST["postSubreddit"] PATH = GLOBAL.directory / POST["postSubreddit"]
possibleExtensions = [".jpg",".png",".mp4",".gif",".webm",".md"] possibleExtensions = [".jpg",".png",".mp4",".gif",".webm",".md"]
for i in range(2): for extension in possibleExtensions:
for extension in possibleExtensions:
FILE_PATH = PATH / (FILENAME+extension) OLD_FILE_PATH = PATH / (
if FILE_PATH.exists(): title
return True + "_" + POST['postId']
else: + extension
FILENAME = POST['postId'] )
FILE_PATH = PATH / (
POST["postSubmitter"]
+ "_" + title
+ "_" + POST['postId']
+ extension
)
SHORT_FILE_PATH = PATH / (POST['postId']+extension)
if OLD_FILE_PATH.exists() or \
FILE_PATH.exists() or \
SHORT_FILE_PATH.exists():
return True
else: else:
return False return False
@@ -450,13 +470,12 @@ def downloadPost(SUBMISSION):
"imgur":Imgur,"gfycat":Gfycat,"erome":Erome,"direct":Direct,"self":Self "imgur":Imgur,"gfycat":Gfycat,"erome":Erome,"direct":Direct,"self":Self
} }
print()
if SUBMISSION['postType'] in downloaders: if SUBMISSION['postType'] in downloaders:
print(SUBMISSION['postType'].upper())
if SUBMISSION['postType'] == "imgur": if SUBMISSION['postType'] == "imgur":
if int(time.time() - lastRequestTime) <= 2: while int(time.time() - lastRequestTime) <= 2:
pass pass
credit = Imgur.get_credits() credit = Imgur.get_credits()
@@ -468,20 +487,21 @@ def downloadPost(SUBMISSION):
+ str(int(IMGUR_RESET_TIME%60)) \ + str(int(IMGUR_RESET_TIME%60)) \
+ " Seconds") + " Seconds")
print( if credit['ClientRemaining'] < 25 or credit['UserRemaining'] < 25:
"Client: {} - User: {} - Reset {}".format( print(
credit['ClientRemaining'], "==> Client: {} - User: {} - Reset {}".format(
credit['UserRemaining'], credit['ClientRemaining'],
USER_RESET credit['UserRemaining'],
USER_RESET
),end=""
) )
)
if not (credit['UserRemaining'] == 0 or \ if not (credit['UserRemaining'] == 0 or \
credit['ClientRemaining'] == 0): credit['ClientRemaining'] == 0):
"""This block of code is needed """This block of code is needed
""" """
if int(time.time() - lastRequestTime) <= 2: while int(time.time() - lastRequestTime) <= 2:
pass pass
lastRequestTime = time.time() lastRequestTime = time.time()
@@ -515,17 +535,13 @@ def download(submissions):
FAILED_FILE = createLogFile("FAILED") FAILED_FILE = createLogFile("FAILED")
for i in range(subsLenght): for i in range(subsLenght):
print("\n({}/{})".format(i+1,subsLenght))
print( print(
"https://reddit.com/r/{subreddit}/comments/{id}".format( f"\n({i+1}/{subsLenght}) ({submissions[i]['postType'].upper()}) " \
subreddit=submissions[i]['postSubreddit'], f"(r/{submissions[i]['postSubreddit']})",end=""
id=submissions[i]['postId']
)
) )
if postExists(submissions[i]): if isPostExists(submissions[i]):
print(submissions[i]['postType'].upper()) print("\nIt already exists")
print("It already exists")
duplicates += 1 duplicates += 1
downloadedCount -= 1 downloadedCount -= 1
continue continue
@@ -546,12 +562,26 @@ def download(submissions):
sys.exit() sys.exit()
except ImgurLimitError as exception: except ImgurLimitError as exception:
FAILED_FILE.add({int(i+1):[str(exception),submissions[i]]}) FAILED_FILE.add({int(i+1):[
"{class_name}: {info}".format(
class_name=exception.__class__.__name__,info=str(exception)
),
submissions[i]
]})
downloadedCount -= 1 downloadedCount -= 1
except NotADownloadableLinkError as exception: except NotADownloadableLinkError as exception:
print(exception) print(
FAILED_FILE.add({int(i+1):[str(exception),submissions[i]]}) "{class_name}: {info}".format(
class_name=exception.__class__.__name__,info=str(exception)
)
)
FAILED_FILE.add({int(i+1):[
"{class_name}: {info}".format(
class_name=exception.__class__.__name__,info=str(exception)
),
submissions[i]
]})
downloadedCount -= 1 downloadedCount -= 1
except NoSuitablePost: except NoSuitablePost:
@@ -560,8 +590,17 @@ def download(submissions):
except Exception as exception: except Exception as exception:
# raise exception # raise exception
print(exception) print(
FAILED_FILE.add({int(i+1):[str(exception),submissions[i]]}) "{class_name}: {info}".format(
class_name=exception.__class__.__name__,info=str(exception)
)
)
FAILED_FILE.add({int(i+1):[
"{class_name}: {info}".format(
class_name=exception.__class__.__name__,info=str(exception)
),
submissions[i]
]})
downloadedCount -= 1 downloadedCount -= 1
if duplicates: if duplicates:
@@ -587,9 +626,6 @@ def main():
checkConflicts() checkConflicts()
except ProgramModeError as err: except ProgramModeError as err:
PromptUser() PromptUser()
except Exception as err:
print(err)
sys.exit()
if not Path(GLOBAL.configDirectory).is_dir(): if not Path(GLOBAL.configDirectory).is_dir():
os.makedirs(GLOBAL.configDirectory) os.makedirs(GLOBAL.configDirectory)
@@ -648,7 +684,6 @@ if __name__ == "__main__":
except KeyboardInterrupt: except KeyboardInterrupt:
if GLOBAL.directory is None: if GLOBAL.directory is None:
GLOBAL.directory = Path(".\\") GLOBAL.directory = Path(".\\")
print("\nQUITTING...")
except Exception as exception: except Exception as exception:
if GLOBAL.directory is None: if GLOBAL.directory is None:

View File

@@ -61,13 +61,14 @@ def getFile(fileDir,tempDir,imageURL,indent=0):
tempDir, tempDir,
reporthook=dlProgress) reporthook=dlProgress)
os.rename(tempDir,fileDir) os.rename(tempDir,fileDir)
print(" "*indent+"Downloaded"+" "*10)
break
except ConnectionResetError as exception: except ConnectionResetError as exception:
print(" "*indent + str(exception)) print(" "*indent + str(exception))
print(" "*indent + "Trying again\n") print(" "*indent + "Trying again\n")
except FileNotFoundError: except FileNotFoundError:
raise FileNameTooLong raise FileNameTooLong
else:
print(" "*indent+"Downloaded"+" "*10)
break
else: else:
raise FileAlreadyExistsError raise FileAlreadyExistsError
@@ -87,13 +88,14 @@ class Erome:
extension = getExtension(IMAGES[0]) extension = getExtension(IMAGES[0])
title = nameCorrector(post['postTitle']) title = nameCorrector(post['postTitle'])
print(title+"_" +post['postId']+extension) print(post["postSubmitter"]+"_"+title+"_"+post['postId']+extension)
fileDir = title + "_" + post['postId'] + extension fileDir = directory / (
fileDir = directory / fileDir post["postSubmitter"]+"_"+title+"_"+post['postId']+extension
)
tempDir = title + "_" + post['postId'] + '.tmp' tempDir = directory / (
tempDir = directory / tempDir post["postSubmitter"]+"_"+title+"_"+post['postId']+".tmp"
)
imageURL = "https:" + IMAGES[0] imageURL = "https:" + IMAGES[0]
@@ -106,9 +108,11 @@ class Erome:
else: else:
title = nameCorrector(post['postTitle']) title = nameCorrector(post['postTitle'])
print(title+"_"+post['postId'],end="\n\n") print(post["postSubmitter"]+"_"+title+"_"+post['postId'],end="\n\n")
folderDir = directory / (title+"_"+post['postId']) folderDir = directory / (
post["postSubmitter"] + "_" + title + "_" + post['postId']
)
try: try:
if not os.path.exists(folderDir): if not os.path.exists(folderDir):
@@ -139,9 +143,16 @@ class Erome:
howManyDownloaded -= 1 howManyDownloaded -= 1
except Exception as exception: except Exception as exception:
raise exception # raise exception
print("\n Could not get the file") print("\n Could not get the file")
print(" " + str(exception) + "\n") print(
" "
+ "{class_name}: {info}".format(
class_name=exception.__class__.__name__,
info=str(exception)
)
+ "\n"
)
exceptionType = exception exceptionType = exception
howManyDownloaded -= 1 howManyDownloaded -= 1
@@ -213,13 +224,22 @@ class Imgur:
post['postExt'] = getExtension(post['mediaURL']) post['postExt'] = getExtension(post['mediaURL'])
title = nameCorrector(post['postTitle']) title = nameCorrector(post['postTitle'])
print(title+"_" +post['postId']+post['postExt']) print(post["postSubmitter"]+"_"+title+"_"+post['postId']+post['postExt'])
fileDir = title + "_" + post['postId'] + post['postExt'] fileDir = directory / (
fileDir = directory / fileDir post["postSubmitter"]
+ "_" + title
+ "_" + post['postId']
+ post['postExt']
)
tempDir = directory / (
post["postSubmitter"]
+ "_" + title
+ "_" + post['postId']
+ ".tmp"
)
tempDir = title + "_" + post['postId'] + '.tmp'
tempDir = directory / tempDir
try: try:
getFile(fileDir,tempDir,post['mediaURL']) getFile(fileDir,tempDir,post['mediaURL'])
except FileNameTooLong: except FileNameTooLong:
@@ -235,9 +255,11 @@ class Imgur:
duplicates = 0 duplicates = 0
title = nameCorrector(post['postTitle']) title = nameCorrector(post['postTitle'])
print(title+"_"+post['postId'],end="\n\n") print(post["postSubmitter"]+"_"+title+"_"+post['postId'],end="\n\n")
folderDir = directory / (title+"_"+post['postId']) folderDir = directory / (
post["postSubmitter"] + "_" + title + "_" + post['postId']
)
try: try:
if not os.path.exists(folderDir): if not os.path.exists(folderDir):
@@ -290,7 +312,14 @@ class Imgur:
except Exception as exception: except Exception as exception:
print("\n Could not get the file") print("\n Could not get the file")
print(" " + str(exception) + "\n") print(
" "
+ "{class_name}: {info}".format(
class_name=exception.__class__.__name__,
info=str(exception)
)
+ "\n"
)
exceptionType = exception exceptionType = exception
howManyDownloaded -= 1 howManyDownloaded -= 1
@@ -355,10 +384,15 @@ class Gfycat:
if not os.path.exists(directory): os.makedirs(directory) if not os.path.exists(directory): os.makedirs(directory)
title = nameCorrector(POST['postTitle']) title = nameCorrector(POST['postTitle'])
print(title+"_"+POST['postId']+POST['postExt']) print(POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt'])
fileDir = directory / (title+"_"+POST['postId']+POST['postExt']) fileDir = directory / (
tempDir = directory / (title+"_"+POST['postId']+".tmp") POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt']
)
tempDir = directory / (
POST["postSubmitter"]+"_"+title+"_"+POST['postId']+".tmp"
)
try: try:
getFile(fileDir,tempDir,POST['mediaURL']) getFile(fileDir,tempDir,POST['mediaURL'])
except FileNameTooLong: except FileNameTooLong:
@@ -404,13 +438,14 @@ class Direct:
POST['postExt'] = getExtension(POST['postURL']) POST['postExt'] = getExtension(POST['postURL'])
if not os.path.exists(directory): os.makedirs(directory) if not os.path.exists(directory): os.makedirs(directory)
title = nameCorrector(POST['postTitle']) title = nameCorrector(POST['postTitle'])
print(title+"_"+POST['postId']+POST['postExt']) print(POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt'])
fileDir = title+"_"+POST['postId']+POST['postExt'] fileDir = directory / (
fileDir = directory / fileDir POST["postSubmitter"]+"_"+title+"_"+POST['postId']+POST['postExt']
)
tempDir = title+"_"+POST['postId']+".tmp" tempDir = directory / (
tempDir = directory / tempDir POST["postSubmitter"]+"_"+title+"_"+POST['postId']+".tmp"
)
try: try:
getFile(fileDir,tempDir,POST['postURL']) getFile(fileDir,tempDir,POST['postURL'])
@@ -425,10 +460,11 @@ class Self:
if not os.path.exists(directory): os.makedirs(directory) if not os.path.exists(directory): os.makedirs(directory)
title = nameCorrector(post['postTitle']) title = nameCorrector(post['postTitle'])
print(title+"_"+post['postId']+".md") print(post["postSubmitter"]+"_"+title+"_"+post['postId']+".md")
fileDir = title+"_"+post['postId']+".md" fileDir = directory / (
fileDir = directory / fileDir post["postSubmitter"]+"_"+title+"_"+post['postId']+".md"
)
if Path.is_file(fileDir): if Path.is_file(fileDir):
raise FileAlreadyExistsError raise FileAlreadyExistsError
@@ -451,7 +487,11 @@ class Self:
+ ")\n" + ")\n"
+ post["postContent"] + post["postContent"]
+ "\n\n---\n\n" + "\n\n---\n\n"
+ "submitted by [u/" + "submitted to [r/"
+ post["postSubreddit"]
+ "](https://www.reddit.com/r/"
+ post["postSubreddit"]
+ ") by [u/"
+ post["postSubmitter"] + post["postSubmitter"]
+ "](https://www.reddit.com/user/" + "](https://www.reddit.com/user/"
+ post["postSubmitter"] + post["postSubmitter"]

View File

@@ -1,4 +1,5 @@
import os import os
import sys
import random import random
import socket import socket
import webbrowser import webbrowser
@@ -14,60 +15,62 @@ from src.errors import (NoMatchingSubmissionFound, NoPrawSupport,
print = printToFile print = printToFile
class GetAuth:
def __init__(self,redditInstance,port):
self.redditInstance = redditInstance
self.PORT = int(port)
def recieve_connection(self):
"""Wait for and then return a connected socket..
Opens a TCP connection on port 8080, and waits for a single client.
"""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('localhost', self.PORT))
server.listen(1)
client = server.accept()[0]
server.close()
return client
def send_message(self, message):
"""Send message to client and close the connection."""
self.client.send('HTTP/1.1 200 OK\r\n\r\n{}'.format(message).encode('utf-8'))
self.client.close()
def getRefreshToken(self,*scopes):
state = str(random.randint(0, 65000))
url = self.redditInstance.auth.url(scopes, state, 'permanent')
print("Go to this URL and login to reddit:\n\n",url)
webbrowser.open(url,new=2)
self.client = self.recieve_connection()
data = self.client.recv(1024).decode('utf-8')
param_tokens = data.split(' ', 2)[1].split('?', 1)[1].split('&')
params = {
key: value for (key, value) in [token.split('=') \
for token in param_tokens]
}
if state != params['state']:
self.send_message(
client, 'State mismatch. Expected: {} Received: {}'
.format(state, params['state'])
)
raise RedditLoginFailed
elif 'error' in params:
self.send_message(client, params['error'])
raise RedditLoginFailed
refresh_token = self.redditInstance.auth.authorize(params['code'])
self.send_message(
"<script>" \
"alert(\"You can go back to terminal window now.\");" \
"</script>"
)
return (self.redditInstance,refresh_token)
def beginPraw(config,user_agent = str(socket.gethostname())): def beginPraw(config,user_agent = str(socket.gethostname())):
class GetAuth:
def __init__(self,redditInstance,port):
self.redditInstance = redditInstance
self.PORT = int(port)
def recieve_connection(self):
"""Wait for and then return a connected socket..
Opens a TCP connection on port 8080, and waits for a single client.
"""
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('localhost', self.PORT))
server.listen(1)
client = server.accept()[0]
server.close()
return client
def send_message(self, message):
"""Send message to client and close the connection."""
self.client.send(
'HTTP/1.1 200 OK\r\n\r\n{}'.format(message).encode('utf-8')
)
self.client.close()
def getRefreshToken(self,*scopes):
state = str(random.randint(0, 65000))
url = self.redditInstance.auth.url(scopes, state, 'permanent')
print("Go to this URL and login to reddit:\n\n",url)
webbrowser.open(url,new=2)
self.client = self.recieve_connection()
data = self.client.recv(1024).decode('utf-8')
param_tokens = data.split(' ', 2)[1].split('?', 1)[1].split('&')
params = {
key: value for (key, value) in [token.split('=') \
for token in param_tokens]
}
if state != params['state']:
self.send_message(
client, 'State mismatch. Expected: {} Received: {}'
.format(state, params['state'])
)
raise RedditLoginFailed
elif 'error' in params:
self.send_message(client, params['error'])
raise RedditLoginFailed
refresh_token = self.redditInstance.auth.authorize(params['code'])
self.send_message(
"<script>" \
"alert(\"You can go back to terminal window now.\");" \
"</script>"
)
return (self.redditInstance,refresh_token)
"""Start reddit instance""" """Start reddit instance"""
scopes = ['identity','history','read'] scopes = ['identity','history','read']
@@ -123,7 +126,7 @@ def getPosts(args):
if args["user"] == "me": if args["user"] == "me":
args["user"] = str(reddit.user.me()) args["user"] = str(reddit.user.me())
print("\nGETTING POSTS\n.\n.\n.\n") # print("\nGETTING POSTS\n.\n.\n.\n")
if not "search" in args: if not "search" in args:
if args["sort"] == "top" or args["sort"] == "controversial": if args["sort"] == "top" or args["sort"] == "controversial":
@@ -245,8 +248,6 @@ def getPosts(args):
raise MultiredditNotFound raise MultiredditNotFound
elif "submitted" in args: elif "submitted" in args:
# TODO
# USE REDDIT.USER.ME() INSTEAD WHEN "ME" PASSED AS A --USER
print ( print (
"submitted posts of {user}\nsort: {sort}\n" \ "submitted posts of {user}\nsort: {sort}\n" \
"time: {time}\nlimit: {limit}\n".format( "time: {time}\nlimit: {limit}\n".format(
@@ -263,8 +264,6 @@ def getPosts(args):
) )
elif "upvoted" in args: elif "upvoted" in args:
# TODO
# USE REDDIT.USER.ME() INSTEAD WHEN "ME" PASSED AS A --USER
print ( print (
"upvoted posts of {user}\nlimit: {limit}\n".format( "upvoted posts of {user}\nlimit: {limit}\n".format(
user=args["user"], user=args["user"],
@@ -308,6 +307,7 @@ def redditSearcher(posts,SINGLE_POST=False):
allPosts = {} allPosts = {}
print("GETTING POSTS")
postsFile = createLogFile("POSTS") postsFile = createLogFile("POSTS")
if SINGLE_POST: if SINGLE_POST:
@@ -328,49 +328,56 @@ def redditSearcher(posts,SINGLE_POST=False):
if result is not None: if result is not None:
details = result details = result
orderCount += 1 orderCount += 1
printSubmission(submission,subCount,orderCount) if GLOBAL.arguments.verbose:
printSubmission(submission,subCount,orderCount)
subList.append(details) subList.append(details)
postsFile.add({subCount:[details]}) postsFile.add({subCount:[details]})
else: else:
for submission in posts: try:
subCount += 1 for submission in posts:
subCount += 1
try: if subCount % 100 == 0 and not GLOBAL.arguments.verbose:
details = {'postId':submission.id, sys.stdout.write("")
'postTitle':submission.title, sys.stdout.flush()
'postSubmitter':str(submission.author),
'postType':None,
'postURL':submission.url,
'postSubreddit':submission.subreddit.display_name}
except AttributeError:
continue
result = checkIfMatching(submission) if subCount % 1000 == 0:
sys.stdout.write("\n")
sys.stdout.flush()
if result is not None: try:
details = result details = {'postId':submission.id,
orderCount += 1 'postTitle':submission.title,
printSubmission(submission,subCount,orderCount) 'postSubmitter':str(submission.author),
subList.append(details) 'postType':None,
'postURL':submission.url,
'postSubreddit':submission.subreddit.display_name}
except AttributeError:
continue
allPosts[subCount] = [details] result = checkIfMatching(submission)
if result is not None:
details = result
orderCount += 1
if GLOBAL.arguments.verbose:
printSubmission(submission,subCount,orderCount)
subList.append(details)
allPosts[subCount] = [details]
except KeyboardInterrupt:
print("\nKeyboardInterrupt",end="")
postsFile.add(allPosts) postsFile.add(allPosts)
if not len(subList) == 0: if not len(subList) == 0:
print( print(
"\nTotal of {} submissions found!\n"\ f"\n\nTotal of {len(subList)} submissions found!\n"\
"{} GFYCATs, {} IMGURs, {} EROMEs, {} DIRECTs and {} SELF POSTS\n" f"{gfycatCount} GFYCATs, {imgurCount} IMGURs, " \
.format( f"{eromeCount} EROMEs, {directCount} DIRECTs " \
len(subList), f"and {selfCount} SELF POSTS"
gfycatCount,
imgurCount,
eromeCount,
directCount,
selfCount
)
) )
return subList return subList
else: else:

View File

@@ -102,11 +102,12 @@ def printToFile(*args, **kwargs):
if not path.exists(folderDirectory): if not path.exists(folderDirectory):
makedirs(folderDirectory) makedirs(folderDirectory)
with io.open( if not "file" in kwargs:
folderDirectory / "CONSOLE_LOG.txt","a",encoding="utf-8" with io.open(
) as FILE: folderDirectory / "CONSOLE_LOG.txt","a",encoding="utf-8"
print(*args, file=FILE, **kwargs) ) as FILE:
print(*args, file=FILE, **kwargs)
def nameCorrector(string): def nameCorrector(string):
"""Swap strange characters from given string """Swap strange characters from given string
@@ -132,7 +133,7 @@ def nameCorrector(string):
if len(string.split('\n')) > 1: if len(string.split('\n')) > 1:
string = "".join(string.split('\n')) string = "".join(string.split('\n'))
BAD_CHARS = ['\\','/',':','*','?','"','<','>','|','.',] BAD_CHARS = ['\\','/',':','*','?','"','<','>','|','.','#']
if any(x in string for x in BAD_CHARS): if any(x in string for x in BAD_CHARS):
for char in string: for char in string: