-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_reddit.py
More file actions
78 lines (70 loc) · 2.64 KB
/
scrape_reddit.py
File metadata and controls
78 lines (70 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Simple Reddit Scraper
Scrapes posts from subreddit using praw
"""
__author__ = "anonekama"
__version__ = 0.3
import argparse
import logging
import pathlib
import sys
import yaml
from cyoa_archives.scrapers.reddit_scraper import scrape_subreddit
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Parse a subreddit for submissions using praw."
)
parser.add_argument("subreddit", help="Name of subreddit to parse (example: makeyourchoice)")
parser.add_argument("-u", "--username", help="Reddit API account username")
parser.add_argument("-p", "--password", help="Reddit API account password")
parser.add_argument("-i", "--clientid", help="Reddit API account client id")
parser.add_argument("-s", "--clientsecret", help="Reddit API account client secret")
parser.add_argument("-a", "--useragent", help="User agent to display")
parser.add_argument("-l", "--limit", help="Limit number of submissions to return", default=None)
parser.add_argument("-c", "--config_file", help="Configuration file to use")
parser.add_argument("-o", "--outfile", help="Output results to filename")
parser.add_argument("-v", "--verbose", help="Be verbose", action="store_const", dest="loglevel", const=logging.INFO)
parser.add_argument("--version", action="version", version=str(__version__))
# Parse arguments
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
username = args.username
password = args.password
clientid = args.clientid
clientsecret = args.clientsecret
useragent = args.useragent
# Load arguments from configuration file if provided
if args.config_file:
filepath = pathlib.Path(args.config_file)
try:
with open(filepath) as f:
config = yaml.safe_load(f).get('reddit_scraper')
username = username if username else config.get('username')
password = password if password else config.get('password')
clientid = clientid if clientid else config.get('clientid')
clientsecret = clientsecret if clientsecret else config.get('clientsecret')
useragent = useragent if useragent else config.get('useragent')
except OSError:
print(f"Could not read file: {filepath}")
sys.exit(1)
# Raise error if required arguments are missing
if not username:
parser.error("username is required")
if not password:
parser.error("password is required")
if not clientid:
parser.error("clientid is required")
if not clientsecret:
parser.error("clientsecret is required")
if not useragent:
parser.error("useragent is required")
# Pass to main function
scrape_subreddit(
args.subreddit,
username,
password,
clientid,
clientsecret,
useragent,
int(args.limit) if args.limit else None,
args.outfile
)