1
+ import praw
2
+ from pprint import pprint
3
+ from textwrap import wrap
4
+ from itertools import izip_longest
5
+ import time
6
+ import signal
7
+
8
+ def two_column (left , right , leftWidth = 16 , rightWidth = 62 , indent = 2 , separation = 2 ):
9
+ lefts = wrap (left , width = leftWidth )
10
+ rights = wrap (right , width = rightWidth )
11
+ results = []
12
+ for l , r in izip_longest (lefts , rights , fillvalue = '' ):
13
+ results .append ('{0:{1}}{2:{5}}{0:{3}}{4}' .format ('' , indent , l , separation , r , leftWidth ))
14
+ return "\n " .join (results )
15
+
16
+
17
+ user_agent = ("bracketMatcher v00.0001 by /u/Iryeress" )
18
+ r = praw .Reddit (user_agent = user_agent )
19
+
20
+ class BracketType :
21
+ def __init__ (self , open , close , name ):
22
+ self .open = open
23
+ self .close = close
24
+ self .name = name
25
+
26
+ def o (self ):
27
+ return self .open
28
+
29
+ def c (self ):
30
+ return self .close
31
+
32
+ def n (self ):
33
+ return self .name
34
+
35
+ brackets = [
36
+ BracketType ("[" , "]" , "square brackets" ),
37
+ BracketType ("(" , ")" , "parentheses" ),
38
+ BracketType ("{" , "}" , "braces" ),
39
+ ]
40
+
41
+
42
+ keep_on = True
43
+
44
+ def kill_handler (sig , frame ):
45
+ global keep_on
46
+ keep_on = False
47
+ signal .signal (signal .SIGUSR1 , kill_handler )
48
+
49
+ subr = r .get_subreddit ('all' )
50
+ submission_generator = subr .get_new (limit = 1000 )
51
+ # submission_generator = r.get_new(limit = 1000)
52
+ cycles = 0
53
+ while (keep_on ):
54
+ posts = []
55
+ count = 0
56
+ for submission in submission_generator :
57
+ if count == 0 :
58
+ before = submission .name
59
+ title = submission .title
60
+ op_text = submission .selftext
61
+ url = submission .permalink
62
+ status = []
63
+ has_bracket = False
64
+ has_negative = False
65
+ for bracket in brackets :
66
+ opening = title .count (bracket .o ()) + op_text .count (bracket .o ())
67
+ closing = title .count (bracket .c ()) + op_text .count (bracket .c ())
68
+ difference = closing - opening
69
+ if difference < 0 :
70
+ has_negative = True
71
+ if opening + closing > 0 :
72
+ has_bracket = True
73
+ if has_negative :
74
+ status .append (
75
+ '{0:{1}}' .format (difference , '+' if difference else '' ).rjust (2 ).ljust (4 )
76
+ + bracket .n ()
77
+ )
78
+ if len (status ) > 0 :
79
+ posts .append ([count , url , title , op_text , status ])
80
+ count += 1
81
+ if count % 100 == 0 :
82
+ print count
83
+
84
+ print "The following %d posts may contain unmatched brackets:\n " % (len (posts ))
85
+ for post in posts :
86
+ # pprint(posts)
87
+ print two_column ("Post No.:" , str (post [0 ]))
88
+ print two_column ("URL:" , '' .join ([i if ord (i ) < 128 else ' ' for i in post [1 ]]))
89
+ print two_column ("Title:" , '' .join ([i if ord (i ) < 128 else ' ' for i in post [2 ]]))
90
+ print two_column ("Self Text:" , '' .join ([i if ord (i ) < 128 else ' ' for i in post [3 ]]))
91
+ for stat in post [4 ]:
92
+ print "%s" % stat
93
+ print " "
94
+ if not keep_on :
95
+ break
96
+ print "========================================================"
97
+ print "Waiting 1 min, cycle: %d" % cycles
98
+ print "========================================================"
99
+ cycles += 1
100
+ time .sleep (60 )
101
+ submission_generator = subr .get_new (limit = 1000 , params = {'before' :before })
0 commit comments