Skip to content

Commit

Permalink
Added option -I to supply URLs/IDs in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ailothaen committed May 19, 2024
1 parent abaa0c4 commit a8d3077
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.yml
28 changes: 21 additions & 7 deletions RedditArchiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import datetime, os, sys, argparse, re

__NAME__ = "RedditArchiver-standalone"
__VERSION__ = "2.0.0"
__VERSION__ = "2.0.2"


# -------------------------- #
Expand All @@ -18,17 +18,18 @@

parser_g1 = parser.add_argument_group(title='Selection', description="Use at least one of these options to select what you want to save. Arguments can be used several times to specify more than one ID, URL or author.")
parser_g1.add_argument('-i', '--id', help='ID or URL of a submission', metavar='ID/URL', action='append')
parser_g1.add_argument('-I', '--file', help='same as -i, but reads IDs or URLs from a file', metavar='file', action='append')
parser_g1.add_argument('-s', '--saved', help='your saved submissions', action="store_true")
parser_g1.add_argument('-S', '--saved-extended', help='same as -s, but also saves the submissions that you saved a comment from', action="store_true")
parser_g1.add_argument('-a', '--author', help='submissions posted from someone (by default: yourself)', metavar="name", nargs='?', action='append')
parser_g1.add_argument('-A', '--author-extended', help='same as -a, but also saves the submissions where the person posted a comment in', metavar="name", nargs='?', action='append')
parser_g1.add_argument('-u', '--upvoted', help='submissions that you upvoted', action="store_true")

parser_g2 = parser.add_argument_group(title='Various', description="Other options controlling various things such as configuration, output directory...")
parser_g2.add_argument('-l', '--limit', help='Limits the number of submissions retrieved with -s/-S, -a/-A and -u (newest first). Please note that the maximum is 1000 and Reddit will refuse to give anything past this limit.', metavar="N", type=int, default=1000)
parser_g2.add_argument('-c', '--config', help='Uses a different config file (default: ./config.yml).', metavar='path', default="./config.yml")
parser_g2.add_argument('-o', '--output', help='Output directory (default: current directory)', metavar='path', default="./")
parser_g2.add_argument('-q', '--quiet', help='Will not generate any message (except for errors)', action='store_true')
parser_g2.add_argument('-l', '--limit', help='limits the number of submissions retrieved with -s/-S, -a/-A and -u (newest first). Please note that the maximum is 1000 and Reddit will refuse to give anything past this limit.', metavar="N", type=int, default=1000)
parser_g2.add_argument('-c', '--config', help='uses a different config file (default: ./config.yml).', metavar='path', default="./config.yml")
parser_g2.add_argument('-o', '--output', help='output directory (default: current directory)', metavar='path', default="./")
parser_g2.add_argument('-q', '--quiet', help='will not generate any message (except for errors)', action='store_true')
parser_g2.add_argument('-h', '--help', action='help', help='Show this help message and exit')

# Advanced arguments (normally hidden)
Expand All @@ -45,8 +46,8 @@ def extract_id(url):
Extracts the submission ID from a supplied URL
"""
regexes = (r"^([a-z0-9]+)/?$",
r"^https?:\/\/(?:old|www)?\.reddit\.com\/([a-z0-9]+)\/?$",
r"^https?:\/\/(?:old|www)?\.reddit\.com\/r\/[a-zA-Z0-9\-_]+\/comments\/([a-z0-9]+)\/?")
r"^https?:\/\/(?:old|new|www)?\.reddit\.com\/([a-z0-9]+)\/?$",
r"^https?:\/\/(?:old|new|www)?\.reddit\.com\/r\/[a-zA-Z0-9\-_]+\/comments\/([a-z0-9]+)\/?")

for regex in regexes:
result = re.search(regex, url)
Expand Down Expand Up @@ -341,6 +342,19 @@ def myprint(message, color, stderr=False):
raise SystemExit(1)
else:
submission_id_list.append(submission_id)

if args.file:
for filename in args.file:
with open(filename, "r") as f:
urls_in_file = f.read().splitlines()

for url in urls_in_file:
submission_id = extract_id(url)
if submission_id is None:
myprint(f'[x] The URL or ID "{url}" in "{filename}" looks incorrect. Please check it.', 9, True)
raise SystemExit(1)
else:
submission_id_list.append(submission_id)

if args.saved or args.saved_extended:
try:
Expand Down

0 comments on commit a8d3077

Please sign in to comment.