· 6 years ago · Mar 11, 2020, 11:04 AM
1import os
2from cgi import log
3import threading
4import requests
5import random
6from selenium.webdriver.chrome.options import Options as ChromeOptions
7from selenium import webdriver
8
9import time
10import praw
11from twitter import *
12from fbchat import Client, log
13from fbchat.models import *
14
15name = "2004jamvillarosa@gmail.com"
16password = "JamJamPlayz990"
17
18client = Client(name, password)
19if not client.isLoggedIn():
20 client = Client(name, password)
21
22t = Twitter(
23 auth=OAuth("1188804909186994177-n1xVppP8Qdoz21V6Arz6uIvrqFk7So", "7PojL3GBSpr1EV2nd0dmJw2vXO9tpxXJvVtKVoi55eHC2",
24 "eorVoejlzqXr6NoT9QK6VJGdv", "J5NKB1A50SKsIHNXLN9kRQ3xevjtwE4A1n8VRAbtXhsY6LJmIg"))
25
26def getMeme(thread_id, thread_type):
27 reddit = praw.Reddit(client_id="xT6OoQdmBGFyYA", client_secret="0rjK5h8WB6Yn4jSqMi4UkjfCZaI",
28 password="000000000000", user_agent="pybotapp1", username="JPlayz99")
29 subreddit = reddit.subreddit('ProgrammerHumor')
30 top_sub = subreddit.hot(limit=100)
31 print(top_sub)
32
33 topics_dict = {"title": [],
34 "score": [],
35 "id": [], "url": [],
36 "comms_num": [],
37 "created": [],
38 "body": []}
39
40 for submission in top_sub:
41 topics_dict["title"].append(submission.title)
42 topics_dict["score"].append(submission.score)
43 topics_dict["id"].append(submission.id)
44 topics_dict["url"].append(submission.url)
45 topics_dict["comms_num"].append(submission.num_comments)
46 topics_dict["created"].append(submission.created)
47 topics_dict["body"].append(submission.selftext)
48
49 getLenght = len(topics_dict['title'])
50 rand = random.randrange(0, getLenght)
51 client.sendRemoteImage(
52 topics_dict['url'][rand],
53 message=Message(text=topics_dict["title"][rand]),
54 thread_id=thread_id,
55 thread_type=thread_type,
56 )
57
58
59def get_dank(thread_id, thread_type):
60 global submission
61 reddit = praw.Reddit(client_id="xT6OoQdmBGFyYA", client_secret="0rjK5h8WB6Yn4jSqMi4UkjfCZaI",
62 password="000000000000", user_agent="pybotapp1", username="JPlayz99")
63 subreddit = reddit.subreddit('wholesomememes')
64 top_sub = subreddit.hot(limit=100)
65 print(top_sub)
66
67 topics_dict = {"title": [],
68 "score": [],
69 "id": [], "url": [],
70 "comms_num": [],
71 "created": [],
72 "body": []}
73
74 for submission in top_sub:
75 topics_dict["title"].append(submission.title)
76 topics_dict["score"].append(submission.score)
77 topics_dict["id"].append(submission.id)
78 topics_dict["url"].append(submission.url)
79 topics_dict["comms_num"].append(submission.num_comments)
80 topics_dict["created"].append(submission.created)
81 topics_dict["body"].append(submission.selftext)
82
83 getLenght = len(topics_dict['title'])
84 rand = random.randrange(0, getLenght)
85 if submission.over_18:
86 pass
87 else:
88 client.sendRemoteImage(
89 topics_dict['url'][rand],
90 message=Message(text=topics_dict["title"][rand]),
91 thread_id=thread_id,
92 thread_type=thread_type,
93 )
94
95
96def set_interval(func, sec):
97 def func_wrapper():
98 set_interval(func, sec)
99 func()
100
101 t = threading.Timer(sec, func_wrapper)
102 t.start()
103 return t
104
105
106def genQuote(thread_id, thread_type):
107 r = requests.get("https://api.quotable.io/random")
108 dicti = r.json()
109 for key, value in dict.items(dicti):
110 if key != "content":
111 pass
112 else:
113 client.send(Message(text=value),
114 thread_id=thread_id,
115 thread_type=thread_type)
116
117
118def getNews(thread_id, thread_type):
119 url = "http://newsapi.org/v2/top-headlines?country=ph&apiKey=9f1b994fe793409fb2d3967847f0982a"
120 i = requests.get(url)
121 data = i.json()
122 for key, value in dict.items(data):
123 if key != "articles":
124
125 pass
126 else:
127 rand = int(random.randrange(0, len(value)))
128 desc = str(value[rand]["description"])
129 if desc is None:
130 desc = None
131 author = str(value[rand]["author"])
132 title = str(value[rand]["title"])
133 urlToImg = str(value[rand]["urlToImage"])
134 client.sendRemoteImage(
135 urlToImg,
136 message=Message(text="Author: " + author + "\n" + "Title: " + title + "\n" + "Description: " + desc),
137 thread_id=thread_id,
138 thread_type=thread_type,
139 )
140
141
142def check():
143 i = requests.get("https://api.darksky.net/forecast/b2f13b2632333395c57e9dbb5f082cda/14.8528, 120.8154")
144 data = i.json()
145
146 for key, value in dict.items(data):
147 if key != "currently":
148 pass
149 else:
150 a = value
151 val = int(a['windSpeed'])
152 if 30 <= val <= 60:
153 return "1"
154 elif 61 <= val <= 120:
155 return "2"
156 elif 121 <= val <= 170:
157 return "3"
158 elif 171 <= val <= 220:
159 return "4"
160 elif val > 220:
161 return "4"
162 else:
163 return "no"
164
165
166class Coronavirus():
167 def __init__(self):
168 GOOGLE_CHROME_PATH = '/app/.apt/usr/bin/google_chrome'
169 CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
170 chrome_options = webdriver.ChromeOptions()
171 chrome_options.add_argument('--disable-gpu')
172 chrome_options.add_argument('--no-sandbox')
173 chrome_options.add_argument('--headless')
174 chrome_bin = os.environ.get('GOOGLE_CHROME_SHIM', None)
175 opts = ChromeOptions()
176 opts.binary_location = chrome_bin
177
178 self.driver = webdriver.Chrome(executable_path="chromedriver",
179 chrome_options=opts) # webdriver.Chrome(DRIVER)
180
181
182def covid(id, type):
183 bot = Coronavirus()
184 bot.driver.get('https://www.worldometers.info/coronavirus/#countries')
185 table = bot.driver.find_element_by_xpath('//*[@id="main_table_countries"]/tbody[1]')
186
187 time.sleep(7)
188 country_element = table.find_element_by_xpath("//td[contains(text(), 'Philippines')]")
189 row = country_element.find_element_by_xpath("./..")
190 data = row.text.split(" ")
191 total_cases = data[1]
192 total_deaths = data[2]
193 active_cases = data[4]
194 total_recovered = data[3]
195 client.send(Message(text="CORONA VIRUS COUNT IN THE PH: \n"
196 + "Total Cases: " + total_cases + "\n"
197 + "Total Deaths: " + total_deaths + "\n"
198 + "Active Cases: " + active_cases + "\n"
199 + "Total recovered: " + total_recovered + "\n"
200 + "Powered by worldometers.info/coronavirus/#countries"),
201 thread_id=id,
202 thread_type=type)
203
204
205class punkjj(Client):
206 def onMessage(self, author_id=None, message_object=None, thread_id=None, thread_type=ThreadType.USER, **kwargs):
207 global pText
208 toggle = client.fetchThreadMessages(thread_id=client.uid, limit=1) # client.uid means its our own acc
209 for message in toggle:
210 pText = message.text.lower()
211 if ("online" in pText):
212 self.markAsRead(author_id)
213 log.info("Message {} from {} in {}".format(message_object, thread_id, thread_type))
214 msgText = message_object.text.lower()
215
216 if msgText == "say hello, delpi bot!" or msgText == "say hi to them, delpi bot! ":
217 client.send(Message(text="Hello! I am delpi bot, created and managed by "
218 "@Jam Emmanuel Arevalo Villarosa, written in "
219 "python, using the "
220 "python FBChat API! I am currently in progress, "
221 "i would appreciate it if you all could give "
222 "suggestions as to what i can do, thank you! :)"),
223 thread_id=thread_id,
224 thread_type=thread_type)
225 elif msgText == "tell me the weather today" or msgText == "whats the weather like today?" or msgText == "tell me the weather" or msgText == "":
226 i = requests.get("https://api.darksky.net/forecast/b2f13b2632333395c57e9dbb5f082cda/14.8528, 120.8154")
227 data = i.json()
228 for key, value in dict.items(data):
229 if key != "currently":
230 pass
231 else:
232 a = value
233 val = str(a['windSpeed'])
234 t = round(int((a["temperature"]) - 32) / 1.8)
235 temp = str(round(int((a["temperature"]) - 32) / 1.8))
236 precipProb = str(a['precipProbability'])
237 if 30 <= t <= 32:
238 client.sendRemoteImage(
239 "https://scontent.fmnl8-1.fna.fbcdn.net/v/t1.15752-0/p280x"
240 "280/88225254_655687121857925_6008140458004316160_n.png?_nc"
241 "_cat=107&_nc_sid=b96e70&_nc_ohc=_6zpM_NdEu0AX9pxo1N&_nc_ht="
242 "scontent.fmnl8-1.fna&oh=24fd7c985f65333a985eed448c22aab9&oe=5EFCD9D3",
243 message=Message(text="What's the weather like today?\n"
244 + "*OVERCAST*\n" +
245 "Wind Speed: " + val + "\n" +
246 "Rain probability: " + precipProb + "\n" +
247 "Temperature (C): " + temp + "°C \n"
248 + "It's getting hot!"),
249 thread_id=thread_id,
250 thread_type=thread_type,
251 )
252
253 elif 31 <= t <= 35:
254 client.send(Message(text="What's the weather like today?\n "
255 + "*OVERCAST*\n" +
256 "Windspeed: " + val + "\n" +
257 "Rain probability: " + precipProb + "\n" +
258 "Temperature (C): " + temp + "°C \n"
259 + "It's getting REALLY hot! Make sure to drink water!"
260 ),
261 thread_id=thread_id,
262 thread_type=thread_type)
263 elif t <= 30:
264 client.send(Message(text="What's the weather like today?\n "
265 + "*OVERCAST*\n" +
266 "Windspeed: " + val + "\n" +
267 "Rain probability: " + precipProb + "\n" +
268 "Temperature (C): " + temp + "°C \n"
269 + "Normal temperature!"
270 ),
271 thread_id=thread_id,
272 thread_type=thread_type)
273 elif 20 <= t <= 25:
274 client.send(Message(text="What's the weather like today?\n "
275 + "*OVERCAST*\n" +
276 "Windspeed: " + val + "\n" +
277 "Rain probability: " + precipProb + "\n" +
278 "Temperature (C): " + temp + "°C \n"
279 + "It's so cold today!"
280 ),
281 thread_id=thread_id,
282 thread_type=thread_type)
283 elif msgText == "am i a dumbbell" or msgText == "dumbbell ba ako" or msgText == "dumbbell ba ako?" or msgText == "dumbell ba ako?" or msgText == "dumbell ba ako":
284 rand = random.randrange(1, 100)
285 if 1 < rand < 15:
286 client.send(Message(
287 text="Pare isa ka sa mga pinaka-malaking dumbbell sa lahat, sorry to tell you the truth."),
288 thread_id=thread_id,
289 thread_type=thread_type)
290 elif 15 < rand < 25:
291 client.send(Message(
292 text="Pabigat ka, pero hindi ka sobrang pabigat."),
293 thread_id=thread_id,
294 thread_type=thread_type)
295 elif 25 < rand < 50:
296 client.send(Message(
297 text="Nagiging dumbbell ka kapag malungkot or pagod."),
298 thread_id=thread_id,
299 thread_type=thread_type)
300 elif 50 < rand < 75:
301 client.send(Message(
302 text="Dumbbell ka talaga, pero kapag tinatamad ka lang."),
303 thread_id=thread_id,
304 thread_type=thread_type)
305 elif 75 < rand < 80:
306 client.send(Message(
307 text="Dumbbell ka, pero nakakabawi pa rin."),
308 thread_id=thread_id,
309 thread_type=thread_type)
310 elif 80 < rand < 100:
311 client.send(Message(
312 text="It's your lucky day! Hindi ka dumbbell!"),
313 thread_id=thread_id,
314 thread_type=thread_type)
315
316 elif msgText == "ano commands" or "ano commands nung bot" in msgText:
317 client.send(Message(
318 text="COMMANDS:\n"
319 "weather: just say 'tell me the weather today.'\n"
320 ""
321 ""
322 "dumbbell-o-meter: just say 'dumbbell ba ako?'\n"
323 "More explanation of the features @ : https://pastebin.com/6yUEA0sR"),
324 thread_id=thread_id,
325 thread_type=thread_type)
326 elif msgText == "quote" or msgText == "tell me a quote" or "tell me a quote" in msgText:
327 genQuote(thread_id, thread_type)
328 elif msgText == "latest news" or msgText == "whats the latest news?" or "whats the latest news" in msgText:
329 getNews(thread_id, thread_type)
330 elif msgText == "pls meme":
331 getMeme(thread_id, thread_type)
332 elif msgText == "pls wholesome":
333 get_dank(thread_id, thread_type)
334 elif msgText == "covid case":
335 client.send(Message(text="Fetching information..."),
336 thread_id=thread_id,
337 thread_type=thread_type)
338 covid(thread_id, thread_type)
339
340 def sendMsgg():
341 if author_id != self.uid:
342 self.send(Message(text="Hello!"), thread_id=thread_id, thread_type=thread_type)
343 self.markAsDelivered(author_id, thread_id)
344
345 if "online" in pText:
346 sendMsgg()
347
348
349client1 = punkjj(name, password)
350client1.listen()