forked from installgen2/spaghooter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeels.py
More file actions
executable file
·186 lines (165 loc) · 5.06 KB
/
feels.py
File metadata and controls
executable file
·186 lines (165 loc) · 5.06 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import random
from time import time
import json
from HTMLParser import HTMLParser
import urllib2
# Feel fetching stuff by AtaK. (thnx)
html_parser = HTMLParser()
chan_urls = {
"4chan" : "http://a.4cdn.org/",
"8chan" : "http://8ch.net/",
"wiz" : "http://wizchan.org/",
"lain" : "http://lainchan.org/"
}
hdrs = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Accept": "text/html,application/json",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Accept-Encoding": "none",
"Accept-Language": "en-US,en;q=0.8",
"Connection": "keep-alive"
}
def strip_html_shit(post):
search_start = 0
while(post.find("<",search_start) != -1):
tag_start = post.find("<",search_start)
tag_end = post.find(">",tag_start)
if tag_end == -1 :
tag_end = len(post) - 2
post = post.replace(post[tag_start : tag_end+1],"")
search_start = tag_end + 1
return post
def find_feels(post):
feels = []
temp = ""
inside_spoiler = False
search_start = 0
while(post.find("tfw", search_start) != -1):
feel_start = post.find("tfw", search_start)
feel_end = feel_start
while(post.find("<", feel_end) != -1):
feel_end = post.find("<", feel_end)
if not inside_spoiler:
if(post[feel_end:feel_end+22] == "<span class=\"spoiler\">"):
inside_spoiler = True
else:
break
else:
if(post[feel_end:feel_end+7] == "</span>"):
inside_spoiler = False
else:
break
feel_end += 1
if feel_end == feel_start:
feel_end = len(post)
feels.append(strip_html_shit(post[feel_start : feel_end]))
search_start = feel_end
# print feels[-1],"\n"
return feels
def get_threads(board, chan):
global chan_urls, hdrs
if chan not in chan_urls:
return []
try :
threads_str = urllib2.urlopen(urllib2.Request(chan_urls[chan] + board + "/threads.json", headers = hdrs)).read()
except urllib2.HTTPError:
return []
return json.loads(threads_str)
def get_posts(thread, board, chan):
global chan_urls, hdrs
posts_url = chan_urls[chan] + board
if(chan == "4chan"):
posts_url += "/thread/"
else:
posts_url += "/res/"
posts_url += str(thread["no"]) + ".json"
try :
posts_str = urllib2.urlopen(urllib2.Request(posts_url, headers = hdrs)).read()
except urllib2.HTTPError:
return []
return json.loads(posts_str)
def get_feels(board = "r9k", chan = "4chan"):
global html_parser
feels = []
threads_json = get_threads(board, chan)
for page in threads_json:
for thread in page["threads"]:
posts_json = get_posts(thread, board, chan)
for post in posts_json["posts"]:
try:
clean_post = html_parser.unescape(post["com"]).encode('ascii', 'ignore')
except:
continue
new_feels = find_feels(clean_post)
feels += new_feels
return feels
def feel(phenny, input):
# Cancel if timeout hasn't ended yet and ignore exceptions
if input.admin == False:
try:
if time() < globals().get(input.sender + "timeout") or time() < globals().get(input.nick + "nicktimeout"): return
except: pass
# Cancel if re-feeling not done
try:
if refeeling == True:
phenny.say("Still re-feeling, please wait.")
return
except: pass
# Actual feel outputting
try:
# if parameters are given
if input.group(2):
# split params and store in array
inputArray = input.group(2).split(' ')
# if more than one param is given
if len(inputArray) > 1:
# get feel from board=param1 chan=param2
feel = random.choice(globals().get(inputArray[0] + inputArray[1] + "feels"))
else:
# get feel from board=param1 chan=4chan
feel = random.choice(globals().get(inputArray[0] + "4chanfeels"))
else:
# get feel from board=r9k chan=4chan
feel = random.choice(r9k4chanfeels)
# output the feel
phenny.say('3>' + str(feel))
except:
# There was an error finding the feel
phenny.say("404 Feel not Found")
# Set channel and user timeout
globals()[input.sender + "timeout"] = time() + 5
globals()[input.nick + "nicktimeout"] = time() + 15
feel.commands = ['feels', 'feel', 'tfw']
feel.priority = 'low'
def refeel(phenny, input):
global refeeling
# If you are not admin
if input.admin == False: return
try:
if refeeling == True:
phenny.say("Already re-feeling!")
return
except: pass
# Set refeeling status to be true
refeeling = True
# if parameters are given
if input.group(2):
# split params and store in array
inputArray = input.group(2).split(' ')
# if more than one param is given
if len(inputArray) > 1:
# download feel from board=param1 chan=param2
phenny.say("Loading feels from " + inputArray[1] + "/" + inputArray[0] + "/")
globals()[inputArray[0] + inputArray[1] + "feels"] = get_feels(inputArray[0], inputArray[1])
else:
# download feel from board=param1 chan=4chan
phenny.say("Loading feels from 4chan/" + inputArray[0] + "/")
globals()[inputArray[0] + "4chanfeels"] = get_feels(inputArray[0])
else:
# download feel from board=r9k chan=4chan
phenny.say("Loading feels from 4chan/r9k/")
globals()["r9k4chanfeels"] = get_feels()
# Re-feeling done, reset status
refeeling = False
refeel.commands = ['re-feel', 'refeel']
refeel.priority = 'low'