def title ( self, url ): if re.match("(?:[Hh]?[Tt]?[Tt]?[Pp])?(?:[sS]?)://(localhost|127.0.0.1|0.0.0.0)", url): return url = syscmd.replaceUmlauts(url) url = url.strip().rstrip(".") ## Until I figure out something better... if "http://t.co" in url.strip(): html = syscmd.getHtml( self, url, False, True ) else: html = syscmd.getHtml( self, url, True, True ) if html: try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html.parser") #else: # soup = BeautifulSoup(html, "html5lib") #broken!! ## We only want to parse the title if it has been found if soup.title: title = soup.title.string title = re.sub("\n", "", title) self.send_chan( "~ " + ' '.join(title.split()) ) ##Split words and join them with space else: self.send_chan( "~ Untitled" ) except Exception as e: self.errormsg = "[ERROR]-[title] title() stating: {0} ({1})".format(e, url) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) else: return
def wikiSearch(self): Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range(5, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://{0}.m.wikipedia.org/w/index.php?search={1}&fulltext=Search".format(Cache.lang, parameters_url) try: html = syscmd.getHtml(self, url, True) except Exception as e: self.errormsg = "[ERROR]-[wiki] wikiSearch()(1) stating: {0}".format(e) sys.error_log.log(self) if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) try: soup = BeautifulSoup(html) if len(soup.findAll("p", {"class": "mw-search-nonefound"})) == 0: Cache.dataCache = soup.findAll("div", {"class": "mw-search-result-heading"}) output = "{0}: http://{1}.wikipedia.org{2}".format( Cache.dataCache[Cache.dataIndex].a.get("title"), Cache.lang, Cache.dataCache[Cache.dataIndex].a.get("href"), ) self.send_chan(output) Cache.dataIndex += 1 else: self.send_chan("There were no results matching the query.") except Exception as e: self.errormsg = "[ERROR]-[wiki] wikiSearch()(2) stating: {0}".format(e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def currency( self ): if len(self.msg) < 7: self.send_chan("Usage: !currency <amount> <from> <to>") if len(self.msg) == 7: try: amount = Decimal(re.sub(",", ".", self.msg[4])).quantize(Decimal("0.00")) except Exception as e: amount = Decimal(1.00) frm = self.msg[5].upper() to = self.msg[6].upper() ## If first value is float and currencies are valid if isinstance( amount, Decimal ) and syscmd.checkCurrency( frm, to): frm = urllib.parse.quote(frm) to = urllib.parse.quote(to) url = "https://www.google.com/finance/converter?a={0}&from={1}&to={2}".format(amount, frm, to) html = syscmd.getHtml(self, url, True) try: soup = BeautifulSoup(html) result = soup.findAll("span", {"class" : "bld"}) ## If there's a result, then send it to the chan if result: result = "{0} {1} = {2}".format(amount, frm.upper(), result[0]) trimmed = re.sub('<[^<]+?>', '', result) self.send_chan(trimmed) else: self.send_chan("Google failed to convert your request :(") except Exception as e: self.errormsg = "[ERROR]-[Currency] stating: {0}".format(e) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) else: self.send_chan("Usage: !currency <amount> <from> <to>")
def ylilauta(self): if len(self.msg) >= 4: url = "http://ylilauta.org/satunnainen/" try: html = syscmd.getHtml(self, url, True) except Exception as e: self.errormsg = "[ERROR]-[ylilauta] ylilauta()(1) stating: {0}".format( e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html5lib") data = soup.findAll("div", {"class": "postsubject"}) x = random.randrange(0, len(data)) string = "{0}: http:{1}".format(data[x].a.string, data[x].a.get('href')) self.send_chan(' '.join(string.split())) except Exception as e: self.errormsg = "[ERROR]-[ylilauta] ylilauta()(2) stating: {0}".format( e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def youtubeSearch(self): try: Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range(4, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://m.youtube.com/results?search_query={0}".format( parameters_url) html = syscmd.getHtml(self, url, True) except: self.errormsg = "[ERROR]-[youtube] youtubeSearch()(1) Someting went wrong getting the html" sysErrorLog.log(self) ## Log the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) try: urls = "" try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html5lib") Cache.dataCache = soup.findAll("a", {"class": "yt-uix-tile-link"}) if len(Cache.dataCache) > 0: string = "{1}: http://youtube.com/watch?v={0}".format( Cache.dataCache[Cache.dataIndex].get('href')[-11:], Cache.dataCache[Cache.dataIndex].string) self.send_chan(string) Cache.dataIndex += 1 else: self.send_chan("No results for: {0}".format(parameters)) except Exception as e: self.errormsg = "[ERROR]-[youtube] youtubeSearch()(2) stating: {0}".format( e) sysErrorLog.log(self) ## Log the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def gt(self): if len(self.msg) == 4: self.send_chan("Usage: !gt <from> <to> <word/sentence>") else: try: parameters = "" frm = self.msg[4].strip() to = self.msg[5].strip() length = len(self.msg) for x in range(6, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://translate.google.com/m?hl={0}&sl={1}&ie=UTF-8&q={2}".format( to, frm, parameters_url) html = syscmd.getHtml(self, url, True) except: self.errormsg = "[NOTICE]-[gt] Something went wrong getting the html" sysErrorLog.log(self) if self.config["debug"]: print("{0}{1}{2}".format(self.color("blue"), self.errormsg, self.color("end"))) try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html.parser") #else: # soup = BeautifulSoup(html, "html5lib") Umlauts Broken! ## Get the translation data = soup.findAll("div", {"class": "t0"}) if data: self.send_chan(data[0].string.strip()) else: return except Exception as e: self.errormsg = "[ERROR]-[gt] gt() stating: {0}".format(e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def googleSearch(self): try: Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range(4, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "https://www.google.fi/search?q={0}".format(parameters_url) html = syscmd.getHtml(self, url, True) except: self.errormsg = "[NOTICE]-[google] googleSearch()(1) Something went wrong getting the html" sysErrorLog.log(self) if self.config["debug"]: print("{0}{1}{2}".format(self.color("blue"), self.errormsg, self.color("end"))) try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html5lib") Cache.dataCache = soup.findAll("h3", {"class": "r"}) if len(Cache.dataCache) > 0: title = "{0}".format(Cache.dataCache[Cache.dataIndex].a) title = syscmd.delHtml(title) string = "{0}: {1}".format( title, Cache.dataCache[Cache.dataIndex].a.get('href')) self.send_chan(string) Cache.dataIndex += 1 else: self.send_chan("No results for: {0}".format(parameters)) except Exception as e: self.errormsg = "[ERROR]-[google] googleSearch()(2) stating: {0}".format( e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def fap(self): if len(self.msg) >= 4: if len(self.msg) == 4: page = random.randrange(1,6000) url = "http://www.p**n.com/videos.html?p={0}".format(page) elif len(self.msg) > 4: parameters = "" for x in range (4, len(self.msg)): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://www.p**n.com/search.html?q={0}".format(parameters_url) try: html = syscmd.getHtml(self, url, True ) except Exception as e: self.errormsg = "[ERROR]-[fap] fap()(1) stating: {0}".format(e) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) if html: try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html.parser") data = soup.findAll("a", {"class" : "title"}) if len(data) > 0: x = random.randrange(0,len(data)) string = "{0}: http://www.p**n.com{1}".format(data[x].get('title'), data[x].get('href')) self.send_chan(' '.join(string.split())) else: self.send_chan("No results for: {0}".format(parameters)) except Exception as e: print(e) self.errormsg = "[ERROR]-[fap] fap()(2) stating: {0}".format(e) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) else: self.send_chan("No results for: {0}".format(parameters))
def gt(self): if len(self.msg) == 4: self.send_chan("Usage: !gt <from> <to> <word/sentence>") else: try: parameters = "" frm = self.msg[4].strip() to = self.msg[5].strip() length = len(self.msg) for x in range (6, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://translate.google.com/m?hl={0}&sl={1}&ie=UTF-8&q={2}".format(to, frm, parameters_url) html = syscmd.getHtml(self, url, True ) except: self.errormsg = "[NOTICE]-[gt] Something went wrong getting the html" sysErrorLog.log( self ) if self.config["debug"]: print("{0}{1}{2}".format(self.color("blue"), self.errormsg, self.color("end"))) try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html.parser") #else: # soup = BeautifulSoup(html, "html5lib") Umlauts Broken! ## Get the translation data = soup.findAll("div", {"class" : "t0"}) if data: self.send_chan(data[0].string.strip()) else: return except Exception as e: self.errormsg = "[ERROR]-[gt] gt() stating: {0}".format(e) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def currency(self): if len(self.msg) < 7: self.send_chan("Usage: !currency <amount> <from> <to>") if len(self.msg) == 7: try: amount = Decimal(re.sub(",", ".", self.msg[4])).quantize(Decimal("0.00")) except Exception as e: amount = Decimal(1.00) frm = self.msg[5].upper() to = self.msg[6].upper() ## If first value is float and currencies are valid if isinstance(amount, Decimal) and syscmd.checkCurrency(frm, to): frm = urllib.parse.quote(frm) to = urllib.parse.quote(to) url = "https://www.google.com/finance/converter?a={0}&from={1}&to={2}".format( amount, frm, to) html = syscmd.getHtml(self, url, True) try: soup = BeautifulSoup(html) result = soup.findAll("span", {"class": "bld"}) ## If there's a result, then send it to the chan if result: result = "{0} {1} = {2}".format(amount, frm.upper(), result[0]) trimmed = re.sub('<[^<]+?>', '', result) self.send_chan(trimmed) else: self.send_chan("Google failed to convert your request :(") except Exception as e: self.errormsg = "[ERROR]-[Currency] stating: {0}".format(e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) else: self.send_chan("Usage: !currency <amount> <from> <to>")
def youtubeSearch(self): try: Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range (4, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://m.youtube.com/results?search_query={0}".format(parameters_url) html = syscmd.getHtml(self, url, True ) except: self.errormsg = "[ERROR]-[youtube] youtubeSearch()(1) Someting went wrong getting the html" sysErrorLog.log( self ) ## Log the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) try: urls = "" try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html5lib") Cache.dataCache = soup.findAll("a", {"class" : "yt-uix-tile-link"}) if len(Cache.dataCache) > 0: string = "{1}: http://youtube.com/watch?v={0}".format( Cache.dataCache[Cache.dataIndex].get('href')[-11:], Cache.dataCache[Cache.dataIndex].string) self.send_chan(string) Cache.dataIndex += 1 else: self.send_chan("No results for: {0}".format(parameters)) except Exception as e: self.errormsg = "[ERROR]-[youtube] youtubeSearch()(2) stating: {0}".format(e) sysErrorLog.log ( self ) ## Log the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def googleSearch(self): try: Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range (4, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "https://www.google.fi/search?q={0}".format(parameters_url) html = syscmd.getHtml(self, url, True ) except: self.errormsg = "[NOTICE]-[google] googleSearch()(1) Something went wrong getting the html" sysErrorLog.log( self ) if self.config["debug"]: print("{0}{1}{2}".format(self.color("blue"), self.errormsg, self.color("end"))) try: try: soup = BeautifulSoup(html, "lxml") except: soup = BeautifulSoup(html, "html5lib") Cache.dataCache = soup.findAll("h3", {"class" : "r"}) if len(Cache.dataCache) > 0: title = "{0}".format(Cache.dataCache[Cache.dataIndex].a) title = syscmd.delHtml(title) string = "{0}: {1}".format(title, Cache.dataCache[Cache.dataIndex].a.get('href')) self.send_chan(string) Cache.dataIndex += 1 else: self.send_chan("No results for: {0}".format(parameters)) except Exception as e: self.errormsg = "[ERROR]-[google] googleSearch()(2) stating: {0}".format(e) sysErrorLog.log( self ) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))
def wikiSearch(self): Cache.dataCache = [] Cache.dataIndex = 0 parameters = "" length = len(self.msg) for x in range(5, length): parameters += "{0} ".format(self.msg[x]) parameters_url = urllib.parse.quote(parameters.strip()) url = "http://{0}.m.wikipedia.org/w/index.php?search={1}&fulltext=Search".format( Cache.lang, parameters_url) try: html = syscmd.getHtml(self, url, True) except Exception as e: self.errormsg = "[ERROR]-[wiki] wikiSearch()(1) stating: {0}".format(e) sys.error_log.log(self) if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end"))) try: soup = BeautifulSoup(html) if len(soup.findAll("p", {"class": "mw-search-nonefound"})) == 0: Cache.dataCache = soup.findAll( "div", {"class": "mw-search-result-heading"}) output = "{0}: http://{1}.wikipedia.org{2}".format( Cache.dataCache[Cache.dataIndex].a.get('title'), Cache.lang, Cache.dataCache[Cache.dataIndex].a.get('href')) self.send_chan(output) Cache.dataIndex += 1 else: self.send_chan("There were no results matching the query.") except Exception as e: self.errormsg = "[ERROR]-[wiki] wikiSearch()(2) stating: {0}".format(e) sysErrorLog.log(self) ## LOG the error if self.config["debug"]: print("{0}{1}{2}".format(self.color("red"), self.errormsg, self.color("end")))