This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.py
149 lines (114 loc) · 4.84 KB
/
tools.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Functions to do reddit authentication, file saving and loading, and data structure printing,
and varialbes used by multiple files."""
from typing import List, Mapping
import pickle
import praw
import os
import warnings
from mention_search_posts import PostWithMentions
# use this to set up PRAW for the first time:
# reddit = praw.Reddit(user_agent = 'desktop:ucsc-class-info-bot:v1.0 (by /u/ucsc-class-info-bot)',
# site_name = 'ucsc_bot')
# _get_code()
# _save_access_information()
def _get_code(r_):
"""
Use this for first-time PRAW setup. Use this then _save_access_information().
Log into /u/ucsc-class-info-bot then navigate to the url printed.
From http://praw.readthedocs.io/en/v3.6.0/pages/oauth.html
"""
url = r_.get_authorize_url('state', 'identity submit edit read', True)
print(url)
def _save_access_information(r_) -> None:
"""
Use this for first-time PRAW setup.
Paste the code from the redirect into the function below.
From http://praw.readthedocs.io/en/v3.6.0/pages/oauth.html
:param r_: praw instance
:type r_: praw.Reddit
"""
with open('pickle/access_information.pickle', 'wb') as file:
pickle.dump(r_.get_access_information('code'), file)
file.close()
class ExistingComment:
"""Info about an existing comment with class info."""
def __init__(self, comment_id_: str, mentions_: list):
self.comment_id = comment_id_
self.mentions_list = mentions_
def __str__(self):
return f"existing comment: {self.comment_id} -> {self.mentions_list}"
# widths of column for printing tables to console.
_column_widths = {'num': 2,
"id": 7,
"author": 15,
"title": 35,
"action": 17}
def trunc_pad(string_: str, column_name: str = None) -> str:
"""Truncates and pads with spaces string_ to be printed in a table.
The padding widths are given in the dict _column_widths.
If column_name isn't specifeid, the string is used as the column name.
:param string_: string to be truncated and padded
:type string_: str
:param column_name: string identifying which column the string is in.
:type column_name: str
:return: the input string, truncated and padded
:rtype: str
"""
if column_name is None:
column_name = string_
width = _column_widths[column_name]
if len(string_) > width:
return string_[:width - 3] + '...'
else:
return string_.ljust(width)
def auth_reddit() -> praw.Reddit:
"""Loads access information and returns PRAW reddit api context.
:return: praw instance
:rtype praw.Reddit
"""
with warnings.catch_warnings():
warnings.simplefilter("ignore") # suppress PRAW warning about user agent containing 'bot'
red = praw.Reddit(user_agent = 'desktop:ucsc-class-info-bot:v0.0.1 (by /u/ucsc-class-info-bot)',
site_name = 'ucsc_bot')
with open('pickle/access_information.pickle', 'rb') as file:
access_information = pickle.load(file)
file.close()
red.set_access_credentials(**access_information)
return red
def load_posts_with_comments() -> Mapping[str, ExistingComment]:
"""Loads from disk the dict of posts that have already been commented on.
:return: dict of <string,ExistingComment> of posts that have already been commented on
:rtype: dict
"""
if not os.path.isfile("pickle/posts_with_comments.pickle"):
return dict()
with open("pickle/posts_with_comments.pickle", 'rb') as file:
a_c = pickle.load(file)
file.close()
return a_c
def save_posts_with_comments(posts_with_comments: Mapping[str, ExistingComment]) -> None:
"""Saves to disk the dict of posts that have already been commented on.
:param posts_with_comments: dict of <string,ExistingComment> of posts that already have comments on them
:type posts_with_comments: dict
"""
with open("pickle/posts_with_comments.pickle", 'wb') as file:
pickle.dump(posts_with_comments, file)
file.close()
def load_found_mentions() -> List[PostWithMentions]:
"""Loads from disk the list of found mentions from the last run of find_mentions().
:return: list of PostWithMentions objects
:rtype: list
"""
with open("pickle/found_mentions.pickle", 'rb') as file:
mentions = pickle.load(file)
file.close()
return mentions
def save_found_mentions(found_mentions: List[PostWithMentions]) -> None:
"""Saves to disk mentions found from from the last run of find_mentions().
This is used in both post_comments.py and in mention_search_posts.py so I have put it in tools.py.
:param found_mentions: list of PostWithMentions objects
:type found_mentions: list
"""
with open("pickle/found_mentions.pickle", 'wb') as file:
pickle.dump(found_mentions, file)
file.close()