示例#1
0
文件: qdb.py 项目: MWisBest/PyBot
def command( message, user, recvfrom ):
	txt = requests.get( "http://qdb.us/random/" ).text
	quoteNum = fixHTMLCharsAdvanced( strbetween( txt, "\">#", "</a>" ) )
	quote = fixHTMLCharsAdvanced( strbetween( txt, "<span class=qt id=qt" + quoteNum , "</span>" ) )
	quote = quote.replace( "<br />", " / " )
	__main__.sendMessage( "Quote #" + quoteNum + ": " + quote, recvfrom )
	return True
示例#2
0
文件: urban.py 项目: MWisBest/PyBot
def command( command, message, user, recvfrom ):
	link = "http://www.urbandictionary.com/"
	if command != "rurban":
		link += "define.php?term=" + message
	else:
		link += "random.php"
	txt = requests.get( link ).text
	definition = fixHTMLCharsAdvanced( strbetween( txt, "<div class='meaning'>\n", "\n</div>" ) )
	word = fixHTMLCharsAdvanced( strbetween( txt, "<title>Urban Dictionary: ", "</title>" ) )
	if definition != "" and word != "" and not definition.startswith( notDefinedPrefix ):
		toSend = word + ": " + definition
		if len( toSend ) >= 370: # This is roughly the longest message I've been able to send.
			shortLink = googlshort( "http://www.urbandictionary.com/define.php?term=" + word ) # Get a short link here in order to send as much as possible
			toCutOff = len( shortLink ) # Get the length of said link to make room for it
			toSend = toSend[0:(366-toCutOff)] # Using 436 here to allow room for "... " of course
			toSend = toSend.rpartition( " " )[0] # In order to make sure it doesn't cut off in the middle of a word
			toSend += "... " + shortLink # Finally finishing it off
		__main__.sendMessage( toSend, recvfrom )
		return True
	elif definition.startswith( notDefinedPrefix ):
		__main__.sendMessage( message + " isn't defined.", recvfrom )
		return True
	else:
		__main__.sendMessage( "There was a problem. Fix your shit.", recvfrom )
		return False
	return False
示例#3
0
文件: riddle.py 项目: MWisBest/PyBot
def command( message, user, recvfrom ):
	txt = requests.get( "http://www.randomriddles.com/" ).text
	riddle = fixHTMLCharsAdvanced( strbetween( txt, "<strong><i>", " <a ;" ) )
	answer = fixHTMLCharsAdvanced( strbetween( txt, "alert('", "')\"" ) )
	if riddle != "" and answer != "":
		__main__.sendMessage( riddle, recvfrom )
		# Use a timer so we don't block
		threading.Timer( 15.0, __main__.sendMessage, args=( "Answer: " + answer, recvfrom ) ).start()
	return True
示例#4
0
文件: weather.py 项目: MWisBest/PyBot
def command( message, user, recvfrom ):
	# .co.uk works with both UK and USA. .com does not!
	wbpostfix = ".co.uk"
	#splitmessage = message.split( ' ', maxsplit=1 )
	#if len( splitmessage ) == 2:
	#	if len( splitmessage[0] ) == 3 and len( splitmessage[1] ) == 3:
	#		wbpostfix = ".co.uk"
	txt = requests.get( "http://weather.weatherbug" + wbpostfix + "/Common/SearchResults.html?loc=" + message + "&is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&submit=GO" ).text
	try:
		location = fixHTMLCharsAdvanced( strbetween( txt, "\r\n<div class=\"boxhdr\">", "</h2>" ) ).replace( "  ", "" ).replace( "<h2>", "" ).strip() # Oh god this is ugly. so very ugly.
		farTemp = fixHTMLChars( strbetween( txt, "<div><strong><span id=\"divTemp\" class=\"entry-title\">", "&deg;F</span></strong>" ) ) #Fahrenheit
		celTemp = str( round( ( float( farTemp ) - 32 ) * 5 / 9, 1 ) ) #celsius
		humidity = fixHTMLChars( strbetween( txt, "<span id=\"divHumidity\" class=\"strong-value\">", "</span></div>" ) )
		feelsLikeLabel = fixHTMLChars( strbetween( txt, "<span id=\"spanFeelsLikeLabel\">", "</span>" ) )
		feelsLikeF = fixHTMLChars( strbetween( txt, "<span id=\"divFeelsLike\" class=\"strong-value\">", "&deg;F</span></div>" ) )
		feelsLikeC = str( round( ( float( feelsLikeF ) - 32 ) * 5 / 9, 1 ) ) #celsius
		rain = fixHTMLChars( strbetween( txt, "<span id=\"divRain\" class=\"strong-value\">", "</span></div>" ) )
		gust = fixHTMLChars( strbetween( txt, "<span id=\"divGust\" class=\"strong-value\">", "</span></div>" ) )
		if farTemp != "":
			toSend = location + " | Temperature: " + farTemp + "°F/" + celTemp + "°C"
			if humidity != "":
				toSend = toSend + " | Humidity: " + humidity
			if feelsLikeLabel != "" and feelsLikeF != "":
				toSend = toSend + " | " + feelsLikeLabel + ": " + feelsLikeF + "°F/" + feelsLikeC + "°C"
			if rain != "":
				toSend = toSend + " | Rain: " + rain
			if gust != "":
				toSend = toSend + " | Gust: " + gust
			__main__.sendMessage( toSend, recvfrom )
			return True
	except:
		pass
	__main__.sendMessage( message + " was not found.", recvfrom )
	return False
示例#5
0
文件: insult.py 项目: MWisBest/PyBot
def command( message, user, recvfrom ):
	insult = fixHTMLCharsAdvanced( strbetween( requests.get( "http://www.randominsults.net/" ).text, "<strong><i>", "</i></strong>" ) )
	if insult != "":
		__main__.sendMessage( message + ": " + insult, recvfrom )
	else:
		__main__.sendMessage( "There was a problem. Fix your shit.", recvfrom )
	return True
示例#6
0
def command( message, user, recvfrom ):
	message = message.split( " " )
	if len( message ) != 3 or not message[0].isdigit():
		__main__.sendMessage( "Usage: currency [amount] [from] [to]", recvfrom )
	else:
		txt = requests.get( "http://www.fxexchangerate.com/m/converter-" + message[0] + "-" + message[1] + "-to-" +  message[2] + ".html" ).text
		from1 = fixHTMLCharsAdvanced( strbetween( txt, "<title>Converter ", " To" ) )
		to1 = fixHTMLCharsAdvanced( strbetween( txt, "Bid Price: ", "</div>" ) )
		toname = fixHTMLCharsAdvanced( strbetween( txt, "To ", " - FX Exchange Rate</title>" ) )
		updated = fixHTMLCharsAdvanced( strbetween( txt, "Updated:: ",  "</div>" ) )
		if from1 != "" and " = 0 " not in from1:
			__main__.sendMessage( from1 + " = " + to1 + " " + toname + " :: Last Updated: " + updated , recvfrom )
			return True
		else:
			__main__.sendMessage( "Conversion unsuccessful! Make sure to use proper currency codes.", recvfrom )
	return False
示例#7
0
def normalLink( link ):
	try:
		ourdata = fetchLinkData( link )
		if ourdata == "":
			return ""
		# Some places have decided 'hey lets use whitespaces in titles for no particular reason'; f**k them
		urlstitle = strbetween( ourdata, "<title>", "</title>" ).strip()
		if urlstitle != "":
			return "\x02URL:\x02 " + fixHTMLCharsAdvanced( urlstitle )
		elif "<TITLE>" in ourdata:
			# f****n pricks using caps can f**k off, seriously f**k them too
			urlstitle = strbetween( ourdata, "<TITLE>", "</TITLE>" ).strip() # see above
			if urlstitle != "":
				return "\x02URL:\x02 " + fixHTMLCharsAdvanced( urlstitle )
		return ""
	except:
		return ""
示例#8
0
def command( message, user, recvfrom ):
	message = message.strip().lower()
	if message in signs:
		txt = requests.get( "http://www.astrology.com/horoscope/daily/" + message + ".html" ).text
		horoscope = fixHTMLCharsAdvanced( strbetween( strbetween( txt, "<div class=\"page-horoscope-text\" style=\"", "<div" ), ">", "</div>" ) )
		if horoscope != "":
			__main__.sendMessage( horoscope, recvfrom )
		else:
			__main__.sendMessage( message + "'s sign not found today. :(", recvfrom )
	elif message == "":
		__main__.sendMessage( "Usage: horoscope [sign]", recvfrom )
	else:
		__main__.sendMessage( "Invalid sign. Valid signs: " + (", ".join( signs )), recvfrom )
	return True
示例#9
0
def youtube( link ):
	try:
		# Preserve initial link var for BreakoutException
		youtubelink = link
		
		ourdata = fetchLinkData( youtubelink, 3.75 )
		
		title = fixHTMLCharsAdvanced( strbetween( ourdata, "<meta itemprop=\"name\" content=\"", "\">" ) )
		
		views = format( int( strbetween( ourdata, "<meta itemprop=\"interactionCount\" content=\"", "\">" ) ), "," )
		if views == "":
			raise BreakoutException
		
		return "\x02\x03" + "01,00You\x03" + "00,04Tube\x03:\x02 \"" + title + "\"" + " | Views: " + views
	except: # Handle it like a normal link?
		return normalLink( link )
示例#10
0
文件: google.py 项目: MWisBest/PyBot
def htmlCleaner( link ): # Remove or replace any special characters
	link = fixHTMLCharsAdvanced( link ) # Start with the usual stuff
	link = link.replace( "%3F", "?" )
	link = link.replace( "%3D", "=" )
	return link
示例#11
0
文件: haiku.py 项目: MWisBest/PyBot
def command( message, user, recvfrom ):
	thehaiku = fixHTMLCharsAdvanced( strbetween( requests.get( "http://prestopnik.com/emo_haiku/" ).text, "<div align=center><BR><BR>", "<BR><BR><BR><BR></div>" ) ).replace( "<BR>", " " )
	if thehaiku == "":
		thehaiku = "I should probably. Write an actual haiku. For error message."
	__main__.sendMessage( thehaiku, recvfrom )
	return True
示例#12
0
def imgur( link ):
	try:
		# chop off prefix for now, so we get "imgur.com" instead of "i.imgur.com"
		imgurlink = link[link.find( "imgur.com" ):]
		
		# If it's this short, assume plain imgur page
		if len( imgurlink ) < 11:
			raise BreakoutException
		
		# If the link ends with a file extension, remove it.
		linkpart = imgurlink.rpartition( "." )
		if not linkpart[2].startswith( "com" ):
			imgurlink = linkpart[0]
		
		# If it's not a gallery link, try to make it one so we can get stats.
		if not "gallery" in imgurlink:
			if "account/favorites/" in imgurlink:
				imgurlink = imgurlink.replace( "account/favorites/", "" )
			linkpart = imgurlink.partition( "/" )
			imgurlink = linkpart[0] + "/gallery/" + linkpart[2]
		
		# gotta start with http for urlopen
		imgurlink = "http://" + imgurlink
		# fetch page
		ourdata = fetchLinkData( imgurlink, 3.75 )
		if ourdata == "":
			return ""
		
		# chop off excess area so we only search the relevant parts of the page
		# this makes us kind of vulnerable to page changes, but it really speeds things up, so it's a worthy compromise.
		# don't check if this is empty, we'll find that out later; speed priority is for the majority case: a proper gallery post.
		tosearch = strbetween( ourdata, "widgetFactory.mergeConfig('gallery',", "</script>" )
		
		ups = strbetween( tosearch, ",\"ups\":", ",\"" )
		downs = strbetween( tosearch, ",\"downs\":", ",\"" )
		if ups == "" or downs == "":
			raise BreakoutException
		# try and get points.
		#points = format( int( strbetween( tosearch, ",\"points\":", ",\"" ) ), "," )
		# If we can't, just gtfo
		#if points == "":
		#	raise BreakoutException
		
		# try and get views.
		views = format( int( strbetween( tosearch, ",\"views\":", ",\"" ) ), "," )
		# again, if we can't, just gtfo
		if views == "":
			raise BreakoutException
		
		# NOTE: This gets something that does not match the actual title...
		## try and get 'official' title.
		##title = fixHTMLCharsAdvanced( strbetween( tosearch, ",\"title\":\"", "\",\"" ) )
		# INSTEAD: Grab what the browser uses.
		title = fixHTMLCharsAdvanced( strbetween( ourdata, "<meta property=\"og:title\" content=\"", "\"/>" ) )
		
		
		# and again, if we can't, just gtfo
		if title == "":
			raise BreakoutException
		
		
		return "\x02\x03" + "09,01Imgur\x03:\x02 \"" + title + "\" | " + ups + "U/" + downs + "D | " + views + " views"
	except: # Handle it like a normal link?
		return normalLink( link )