Пример #1
0
def prisjakt_search(query_string):
	# Build URLs
	url_product = 'http://www.prisjakt.nu/ajax/jsonajaxserver.php?' + \
			'm=super_search&p={"mode"%3A"' + 'prod_pj' + \
			'"%2C"search"%3A"' + query_string + '"%2C"limit"%3A1%2C"v4"%3A1}'
	url_book = 'http://www.prisjakt.nu/ajax/jsonajaxserver.php?' + \
			'm=super_search&p={"mode"%3A"' + 'bok' + \
			'"%2C"search"%3A"' + query_string + '"%2C"limit"%3A1%2C"v4"%3A1}'

	# Fetch the product result page
	response = utility.read_url(url_product)
	data = response["data"]

	# Look for info
	id_pattern = "\\{'mode': 'produkt', 'produkt_id': '(\\d+)'\\}"
	id_match = re.search(id_pattern, data)

	if not id_match:
		# Fetch the book result page
		response = utility.read_url(url_book)
		data = response["data"]

		# Look for info
		id_pattern = "\\{'mode': 'bok', 'produkt_id': '(\\d+)'\\}"
		id_match = re.search(id_pattern, data)

		url_type = "bok"
	else:
		url_type = "produkt"

	if id_match:
		# We seem to have found something
		product_id = id_match.group(1)

		# Get title
		if url_type == "bok":
			title_pattern = "class=\\\\\"ikon(14)?\\\\\"( alt=\\\\\"\\\\\")?\\> (.+?) \\\\n"
			encoded_title = re.search(title_pattern, data).group(3)
		else:
			title_pattern = "onmouseout=\\\\\"ajaxpopup_hide\(\);\\\\\"\>\\\\n  (.+?) \\\\n"
			encoded_title = re.search(title_pattern, data).group(1)
		
		# Remove HTML tags
		encoded_title = string.replace(
				encoded_title, '<span class=\\"search_hit\\">', '')
		encoded_title = string.replace(encoded_title, '<\\/span>', '')
		# Decode special characters
		product_title = decode_characters(encoded_title)

		# Get price
		data = data.replace("&nbsp;", "")
		price_pattern = "\\<span class=\\\\\"pris\\\\\"\>(\\d+:-)\\<\\\\\/span\\>"
		product_price = re.search(price_pattern, data).group(1)

		# Done, return info string (latin-1 to make IRCClient.send() happy)
		return product_title.encode('latin-1', 'replace') + ", " + product_price + \
				", http://www.prisjakt.nu/" + url_type + ".php?p=" + product_id + \
				" | All results: http://www.prisjakt.nu/search.php?query=" + query_string
	else:
		return "No product found."
Пример #2
0
    def trig_mc(self, bot, source, target, trigger, argument):
        term = argument.strip()

        if not term:
            return "usage: .metacritic <game title> or <game> <platform> (slower)"

        url = "http://apps.metacritic.com/search/process?ty=3&tfs=game_title&ts=" + utility.escape(term)
        data = utility.read_url(url)["data"]
        result = self.parse_result(data, term, url)

        if result:
            return result

        error_handler.output_message("<metacritic> title search failed.")
        url = "http://apps.metacritic.com/search/process?ty=3&ts=" + utility.escape(term)
        data = utility.read_url(url)["data"]
        result = self.parse_result(data, term, url)

        if result:
            return result

        return (
            "Found nothing. Try it yourself: "
            + "http://apps.metacritic.com/search/process?ty=3&ts="
            + utility.escape(term)
        )
Пример #3
0
    def trig_mc(self, bot, source, target, trigger, argument):
        term = argument.strip()

        if not term:
            return "usage: .metacritic <game title> or <game> <platform> (slower)"

        url = 'http://apps.metacritic.com/search/process?ty=3&tfs=game_title&ts=' + utility.escape(
            term)
        data = utility.read_url(url)["data"]
        result = self.parse_result(data, term, url)

        if result:
            return result

        error_handler.output_message("<metacritic> title search failed.")
        url = 'http://apps.metacritic.com/search/process?ty=3&ts=' + utility.escape(
            term)
        data = utility.read_url(url)["data"]
        result = self.parse_result(data, term, url)

        if result:
            return result

        return "Found nothing. Try it yourself: " + 'http://apps.metacritic.com/search/process?ty=3&ts=' + utility.escape(
            term)
Пример #4
0
def tw_get_info(): 
	counter = 0
	address = "master.teewars.com" 
	master_port = 8300
 
	sock = socket(AF_INET, SOCK_DGRAM) 
	sock.settimeout(5.0) 
	sock.sendto("\x20\x00\x00\x00\x00\x00\xff\xff\xff\xffreqt", (address, master_port)) 
 
	try:
		data, addr = sock.recvfrom(1024) 
		sock.close() 
		data = data[14:] 
		num_servers = len(data) / 6 
		num_players = 0 

		players_dic = {}

		for n in range(0, num_servers): 
			ip = ".".join(map(str, map(ord, data[n*6:n*6+4]))) 
			port = ord(data[n*6+5]) * 256 + ord(data[n*6+4]) 

			#print ip, port

			with list_lock:
				id = thread.start_new_thread(tw_get_num_players_proxy, (ip, port, players_dic))
				players_dic[id] = -2

		while True:
			has_items = False
			with list_lock:
				for slot in players_dic.keys():
					if players_dic[slot] == -2:
						has_items = True
						break

			if has_items:
				time.sleep(0.5)
			else:
				break

		players_list = []

		for slot in players_dic.keys():
			if players_dic[slot] != -1:
				players_list.append(players_dic[slot])

		num_servers = len(players_list)
		num_players = reduce(lambda x, y: x + y, players_list)

		with open("data/tw_stats.txt", "a") as file:
			file.write("%s %s %s\n" % (int(time.time()), num_servers, num_players))
			 
		utility.read_url("http://serp.starkast.net/berserker/gief_stats.php?timestamp=%s&servers=%s&players=%s" % (int(time.time()), num_servers, num_players));
		return (num_servers, num_players)
	except:
		print 'exception O.o', sys.exc_info(), traceback.extract_tb(sys.exc_info()[2])
		return None
Пример #5
0
    def trig_temp(self, bot, source, target, trigger, argument):
        """ Usage: .temp [City] Uses data from temperature.nu, please direct all complaints to www.temperatur.nu """
        argument = argument.strip()
        if argument:
            argument = argument.strip()
            self.places[source] = argument
            self.save()
        else:
            if source in self.places:
                argument = self.places[source]
            else:
                argument = "ryd"

        argument_text = argument
        argument = utility.asciilize(argument)
        argument = utility.escape(argument)

        # awesome hack to include avesta!
        if argument.lower() == "avesta":
            actual_argument = "fors"
        else:
            actual_argument = argument

        url = "http://www.temperatur.nu/termo/%s/temp.txt" % actual_argument.lower()
        response = utility.read_url(url)
        m = None

        if response:
            data = response["data"]
            m = _get_temp_re.match(data)

        if m and m.group(1) != "not found":
            return "Temperature in %s: %s." % (argument_text, m.group(1))
        else:
            return "Temperature in %s: invalid place, try using .yr instead." % (argument_text)
Пример #6
0
Файл: imdb.py Проект: plux/pynik
def imdb_info(url):
    response = utility.read_url(url)
    data = response["data"]

    m = re.search("<title>(.*?) \((\d+\/?I?)\)<\/title>", data)
    if m:
        title = m.group(1)
        year = m.group(2)
    else:
        title = ""
        year = ""

    m = re.search("<b>([0-9\.]+)\/10<\/b>", data)
    if m:
        rating = m.group(1)
    else:
        rating = 0

    m = re.search("<h5>Plot Summary:</h5> \n(.*?)\n<a", data)
    if m:
        tagline = '"%s" -' % m.group(1)
    else:
        m = re.search("<h5>Plot Outline:</h5> \n(.*?) <a", data)
        if m:
            tagline = '"%s" -' % m.group(1)
        else:
            tagline = ""

    m = re.findall('"\/Sections\/Genres\/(.*?)\/', data)
    if m:
        genres = ", ".join(set(m))
    else:
        genres = ""

    return "%s (%s) - Rating: %s/10 - Genre: %s - %s %s" % (title, year, rating, genres, tagline, url)
Пример #7
0
def get_title(url):
	import urllib
	if not re.search('[^:]+:\/\/', url):
		url = 'http://' + url

	response = utility.read_url(url)
	if response == None:
		return None

	# workaround ISO-8859-1 charset fallback in requests
	# https://github.com/kennethreitz/requests/issues/2086
	if "charset" in response["headers"]["Content-Type"]:
		possible_encoding = response["encoding"]
	else:
		possible_encoding = False

	markup = response["raw_content"]
	soup = BeautifulSoup(markup, "html5lib", from_encoding=possible_encoding)
	title_tag = soup.title

	if title_tag:
		title = title_tag.text.strip()

		if title.lower() in url:
			return None
		else:
			return title
	else:
		return None
Пример #8
0
def icq_lookup(icqid):
    url = 'http://www.icq.com/people/about_me.php?uin=' + utility.escape(icqid)
    response = utility.read_url(url)
    data = response["data"].replace("\n", "")

    m = re.search(
        '<div class="uinf-2-2-2-1">(.*?)<\/div>.*?<div class="uinf-2-2-2-2">(.*?)<\/div>.*?<div class="uinf-2-2-2-4">(.*?)<\/div>.*?<div class="uinf-2-2-2-4">(.*?)<\/div>',
        data)

    if m:
        nick = m.group(1)
        info = m.group(2)
        if info:
            info = re.sub("\n|\r\n|\n\n", ", ", info)
        city = m.group(3)
        country = m.group(4)

        if nick:
            result = nick
        if info or city or country:
            result = result + ": "

        if info:
            result = result + info
        if city:
            result = result + ", " + city
        if country:
            result = result + ", " + country
        return result
    else:
        return None
Пример #9
0
    def trig_schema(self, bot, source, target, trigger, argument):
        if not argument:
            argument = self.id_presets.get(source.lower(), source.lower())
        else:
            argument = argument.strip().lower()
            self.id_presets[source.lower()] = argument
            self.save()

        if argument in self.id_directory:
            url = self.id_directory[argument]
            if isinstance(url, int):
                url = "http://timeedit.liu.se/4DACTION/iCal_downloadReservations/timeedit.ics?branch=5&id1=%d&lang=1" % url

            response = utility.read_url(url)

            parser = iCalParser()
            parser.process(response["data"])
            parser.events.sort()

            relevant_events = parser.events[0:7]
            event_outputs = []
            last_event = None
            for event in relevant_events:
                if last_event and last_event.start.day == event.start.day:
                    event_outputs.append(event.short_description())
                else:
                    event_outputs.append(event.long_description())

                last_event = event

            return "%s: %s" % (argument, " | ".join(event_outputs))
        else:
            return "I don't have the ID '" + argument + "' in my directory. Feel free to add it by typing .addschemaid <name> <url or timeedit id>."
Пример #10
0
    def fetchFood(self, restaurant, day=None):
        if datetime.now() < datetime(2011, 8, 7):
            return "Nien nien nien! http://www.youtube.com/watch?v=fkmv2eAESfM"
        if day == "today":
            return "Wiseass aren't you?"
        elif day != None:
            return "I'm sorry Dave Preston can only handle 'today'."

        response = utility.read_url(self.url)
        data = response["data"]

        lunches = re.findall("</b>\s*<BR>([^<]*)<", data)
        if lunches[0][0:6] == "Vi har":
            lunches = lunches[1:]

        cnt = 1
        result = "Lunch Collegium "

        for lunch in lunches:
            result += str(cnt) + ": " + lunch + " "
            cnt += 1

        if result[-1:] == " ":
            result = result[:-1]

        if len(result) == 0 or len(lunches) == 0:
            return "No lunch available at %s ):" % restaurant
        else:
            return result
Пример #11
0
    def trig_tv(self, bot, source, target, trigger, argument):
        response = utility.read_url("http://www.tv.nu/")
        data = response["data"]

        if len(argument):
            channel = argument

            s = self.extract_channel_info(data, channel)
            if s:
                return "Currently on %s: %s." % (channel, s)
            else:
                return "Could not find that channel. Try http://tvguide.swedb.se/tv?=NU"
        else:
            channels = [
                'SVT 1', 'SVT 2', 'TV3', 'TV4', 'TV4+', 'Kanal 5', 'TV6',
                'Discovery Mix', 'MTV'
            ]
            descriptions = []

            for channel in channels:
                s = self.extract_channel_info(data, channel)
                if s:
                    descriptions.append(channel + ': ' + s)
            descriptions.append('http://tvguide.swedb.se/tv?=NU')

            return " | ".join(descriptions)
Пример #12
0
 def fetch_show_info(self, show):
     info = {}
     query_result = utility.read_url(self.URL_API % show)
     raw_data = utility.unescape(query_result['data'].decode('utf-8'), True)
     for m in self.PATTERN_DATA_ENTRY.finditer(raw_data):
         info[m.group('key')] = m.group('value').replace('^', u", ")
     return info
Пример #13
0
    def trig_addschemacourse(self, bot, source, target, trigger, argument):
        argument = argument.replace(" ", "")

        if argument:
            url = "http://timeedit.liu.se/4DACTION/WebShowSearch/5/2-0?wv_type=6&wv_search=" + argument
            #print url
            response = utility.read_url(url)
            data = response["data"].replace("\n", "")
            #print data

            m = re.search(
                '\<a href=\'javascript:addObject\((\d+)\)\'\>\<img src=\'\/img\/plus\.gif\' width=\'12\' height=\'12\' border=\'0\' alt=\'\'\>\<\/a\>',
                data)

            if not m:
                return "Course not found :("

            #print m.group(1)
            self.id_directory[argument.lower()] = int(m.group(1))
            self.save()
            return "Added %s: http://timeedit.liu.se/4DACTION/WebShowSearch/5/2-0?wv_obj1=%s&wv_graphic=Grafiskt+format If this is wrong, just re-add it." % (
                argument.lower(), m.group(1))

        else:
            return "Try .addschemacourse <course code>"
Пример #14
0
def icq_lookup(icqid):
	url = 'http://www.icq.com/people/about_me.php?uin=' + utility.escape(icqid)
	response = utility.read_url(url)
	data = response["data"].replace("\n", "")

	m = re.search('<div class="uinf-2-2-2-1">(.*?)<\/div>.*?<div class="uinf-2-2-2-2">(.*?)<\/div>.*?<div class="uinf-2-2-2-4">(.*?)<\/div>.*?<div class="uinf-2-2-2-4">(.*?)<\/div>', data)

	if m:
		nick = m.group(1)
		info = m.group(2)
		if info:
			info = re.sub("\n|\r\n|\n\n",", ",info)
		city = m.group(3)
		country = m.group(4)
		
		if nick:
			result = nick
		if info or city or country:
			result = result + ": "
			
		if info:
			result = result + info
		if city:
			result = result + ", " + city
		if country:
			result = result + ", " + country
		return result
	else:
		return None
Пример #15
0
	def lookup_direct(self, reference):
		if reference.type == u"album":
			endpoint = u"v1/albums/{id}"
		elif reference.type == u"artist":
			endpoint = u"v1/artists/{id}"
		elif reference.type == u"playlist":
			endpoint = u"v1/users/{user_id}/playlists/{playlist_id}"
			return None  # Unsupported by this plugin
		elif reference.type == u"track":
			endpoint = u"v1/tracks/{id}"
		else:
			return None  # Unsupported by this plugin

		api_url = self.api_base_url + endpoint.format(id=reference.hash)
		response = utility.read_url(api_url)

		if not response:
			return None

		try:
			data = JSONDecoder().decode(response['data'])
		except ValueError:
			return None

		if data.get(u"status"):
			return None
		else:
			return self._format_result(reference.type, data)
Пример #16
0
def posten_kolli_query(kolli_id):
    url = (
        "http://posten.se/tracktrace/TrackConsignments_do.jsp?trackntraceAction=saveSearch&consignmentId="
        + utility.escape(kolli_id)
    )
    response = utility.read_url(url)
    data = response["data"]

    search = re.search(
        "(?ims)<dt>Fr&aring;n:</dt><dd>(.*?)</dd>.*?rightcol.*h2>.*<h3>(.*?)</h3>\s*?(.*?)(<br/>|<div).*?<dt>Vikt:</dt><dd>(.*?)</dd>",
        data,
    )

    if search:
        sender = search.group(1)
        date = search.group(2)
        status = search.group(3)
        weight = search.group(5)

        if date and status:
            result = "%s fr\xe5n %s | %s: %s | %s" % (weight, sender, date, re.sub("<.+?>", "", status), url)
            return result
        else:
            return None
    else:
        return None
Пример #17
0
	def spot_lookup_direct(self, theSpot):
		url = "http://spotify.url.fi/%s/%s" % (theSpot.type, theSpot.hash)
		response = utility.read_url(url)
		data = response["data"]

		# Commence data mining

		artist = re.search(r"<span>Artist</span>\s*<a.*?>(?P<artist>.+?)</a>", data, re.DOTALL)
		if artist: artist = artist.group(1)

		album = re.search(r"<span>Album</span>\s*<a.+?>(?P<album>.+?)</a>", data, re.DOTALL)
		if album: album = album.group(1)

		year = re.search(r"<span>Year</span>\s*(?P<year>.+?)\s*</p>", data, re.DOTALL)
		if year: year = year.group(1)

		track = re.search(r"<span>Track</span>\s*<a.+?>(?P<track>.+?)</a>", data, re.DOTALL)
		if track: track = track.group(1)

		length = re.search("<span>Length</span>\s*(?P<length>.+?)\s*</p>", data, re.DOTALL)
		if length: length = length.group(1)

		output = "%s: %s | %s (%s)" % (artist, track, album, year)

		if not track:
			output = "%s: %s (%s)" % (artist, album, year)

		if not album:
			output = "%s" % artist

		if not artist:
			return "couldn't find shit, captain!"

		return output
Пример #18
0
    def posten_postnr_query(self, Address, Postort):
        url = 'http://www.posten.se/soktjanst/postnummersok/resultat.jspv?gatunamn=' + utility.escape(
            Address) + '&po=' + utility.escape(Postort)

        response = utility.read_url(url)
        data = response["data"]

        postnrs = {}
        for line in data.split("\n"):
            search = re.search(
                '<TD class="firstcol">([^<]*)</TD><TD>([^<]*)</TD><TD>([^<]*)',
                line)
            if search:
                if postnrs.has_key(search.group(3)):
                    postnrs[search.group(
                        3)] += " & " + search.group(1) + " " + search.group(2)
                else:
                    postnrs[search.group(
                        3)] = search.group(1) + " " + search.group(2)

        result = ""
        for postnr in postnrs.iterkeys():
            if len(result) != 0:
                result += ", "
            result += "%s: %s" % (postnr, postnrs[postnr])
            # print postnrs[postnr]

        if len(result) == 0:
            return "no result :<"
        else:
            # print result
            return result
Пример #19
0
	def posten_postnr_query(self, Address, Postort):
		url = 'http://www.posten.se/soktjanst/postnummersok/resultat.jspv?gatunamn=' + utility.escape(Address) + '&po=' + utility.escape(Postort)
	 
		response = utility.read_url(url)
		data = response["data"]
	 
		postnrs = {}
		for line in data.split("\n"):
			search = re.search('<TD class="firstcol">([^<]*)</TD><TD>([^<]*)</TD><TD>([^<]*)', line)
			if search:
				if postnrs.has_key(search.group(3)):
					postnrs[search.group(3)] += " & " + search.group(1) + " " + search.group(2)
				else:
					postnrs[search.group(3)] = search.group(1) + " " + search.group(2)
	 
		result = ""
		for postnr in postnrs.iterkeys():
			if len(result) != 0:
				result += ", "
			result += "%s: %s" % (postnr, postnrs[postnr])
			# print postnrs[postnr]
	 
		if len(result) == 0:
			return "no result :<"
		else:
			# print result
			return result
Пример #20
0
	def query(self, argument):
		decoder = JSONDecoder()
		argument = utility.escape(argument)
		api_url = u"http://www.imdbapi.com/?t=%(search_term)s&r=json&plot=short" % \
				{"search_term": argument}
		site_search_url = u"http://akas.imdb.com/find?s=all&q=" + argument
		response = utility.read_url(api_url)

		if not response:
			return u"Couldn't connect to the API :( | Manual search: " + site_search_url

		try:
			data = decoder.decode(response['data'])
		except Exception:
			return u"Couldn't parse the API output :( | Manual search: " + site_search_url

		if data.get(u"Response") != u"True":
			return u"No results found! Maybe you should try searching manually: " + \
					site_search_url

		return \
				(u"%(title)s (%(year)s) - Rating: %(rating)s out of 10 - Genre: %(genre)s - " + \
				u"http://akas.imdb.com/title/%(id)s/ | More results: %(site_search_url)s") % \
				{u"title": data.get(u"Title", u"Missing title :S"),
					u"year": data.get(u"Year", u"Unknown year"),
					u"rating": data.get(u"Rating", u"N/A"),
					u"genre": data.get(u"Genre", u"Unknown"),
					u"id": data.get(u"ID", u"tt0107838"),
					u"site_search_url": site_search_url}
Пример #21
0
    def fetchFood(self, restaurant, day=None):
        if day == "today":
            return "Wiseass aren't you?"
        elif day != None:
            return "I'm sorry Dave Preston can only handle 'today'."

        response = utility.read_url(self.url)
        data = response["data"]

        day = None
        week = None
        found_restaurant = None
        lunches = []
        ofset = 0

        # find week and day
        search = re.search(
            '<h2><span class="[^"]*">([^<]*)</span>  <span class="[^"]*">vecka ([^<]*)</span></h2>',
            data)
        if search:
            day = search.group(1)
            week = search.group(2)

        # find lunch at restaurant
        search = re.search('id="(' + restaurant + ')"', data, re.IGNORECASE)
        end = 0
        if search:
            found_restaurant = search.group(1)
            ofset += search.end()

            search = re.search('</div>', data[ofset:])
            if search:
                end = ofset + search.start()

                for cnt in range(1, 20):
                    search = re.search("<li>([^<]*)</li>", data[ofset:])
                    if search and ofset + search.end() < end:
                        lunch = search.group(1).replace("&amp; ", "")
                        lunches.append(lunch)
                        ofset += search.end()
                    else:
                        break

        # create result
        result = ""
        cnt = 1
        if day != None and week != None and found_restaurant != None:
            result = "Lunch " + found_restaurant + " " + day + " v" + week + " "

        for lunch in lunches:
            result += str(cnt) + ": " + lunch + " "
            cnt += 1

        if result[-1:] == " ":
            result = result[:-1]

        if len(result) == 0 or len(lunches) == 0:
            return "No lunch available at %s ):" % restaurant
        else:
            return result
Пример #22
0
	def trig_schema(self, bot, source, target, trigger, argument):
		if not argument:
			argument = self.id_presets.get(source.lower(), source.lower())
		else:
			argument = argument.strip().lower()
			self.id_presets[source.lower()] = argument
			self.save()

		if argument in self.id_directory:
			url = self.id_directory[argument]
			if isinstance(url, int):
				url = "http://timeedit.liu.se/4DACTION/iCal_downloadReservations/timeedit.ics?branch=5&id1=%d&lang=1" % url
			
			response = utility.read_url(url)

			parser = iCalParser()
			parser.process(response["data"])
			parser.events.sort()

			relevant_events = parser.events[0:7]
			event_outputs = []
			last_event = None
			for event in relevant_events:
				if last_event and last_event.start.day == event.start.day:
					event_outputs.append(event.short_description())
				else:
					event_outputs.append(event.long_description())
					
				last_event = event

			return "%s: %s" % (argument, " | ".join(event_outputs))
		else:
			return "I don't have the ID '" + argument + "' in my directory. Feel free to add it by typing .addschemaid <name> <url or timeedit id>."
Пример #23
0
	def fetchFood(self, restaurant, day=None):
                if datetime.now() < datetime(2011, 8, 7):
                        return "Nien nien nien! http://www.youtube.com/watch?v=fkmv2eAESfM"
		if day == "today":
			return "Wiseass aren't you?"
		elif day != None:
			return "I'm sorry Dave Preston can only handle 'today'."
		

		response = utility.read_url(self.url)
		data = response["data"]

                lunches = re.findall("</b>\s*<BR>([^<]*)<", data)
                if lunches[0][0:6] == "Vi har":
                        lunches = lunches[1:]

                cnt = 1
		result = "Lunch Collegium "
			
		for lunch in lunches:
			result += str(cnt) + ": " + lunch + " "
			cnt += 1

		if result[-1:] == " ":
			result = result[:-1]

		if len(result) == 0 or len(lunches) == 0:
			return "No lunch available at %s ):" % restaurant
		else:
			return result
Пример #24
0
	def fetchFood(self, restaurant, day=None):
		if day == "today":
			return "Wiseass aren't you?"		
		elif day != None:
			return "I'm sorry Dave Preston can only handle 'today'."
		

		response = utility.read_url(self.url)
		data = response["data"]

		day = None
		week = None
		found_restaurant = None
		lunches = []
		ofset = 0

		# find week and day
		search = re.search('<h2><span class="[^"]*">([^<]*)</span>  <span class="[^"]*">vecka ([^<]*)</span></h2>', data)
		if search:
			day = search.group(1)
			week = search.group(2)

		# find lunch at restaurant
		search = re.search('id="(' + restaurant + ')"', data, re.IGNORECASE)
		end = 0
		if search:
			found_restaurant = search.group(1)
			ofset += search.end()
			
			search = re.search('</div>', data[ofset:])
			if search:
				end = ofset + search.start()

				for cnt in range(1,20):
					search = re.search("<li>([^<]*)</li>", data[ofset:])
					if search and ofset+search.end() < end:
						lunch = search.group(1).replace("&amp; ", "")
						lunches.append(lunch)
						ofset += search.end()
					else:
						break

		# create result
		result = ""
		cnt = 1
		if day != None and week != None and found_restaurant != None:
			result = "Lunch " + found_restaurant + " " + day + " v" + week + " "
			
		for lunch in lunches:
			result += str(cnt) + ": " + lunch + " "
			cnt += 1

		if result[-1:] == " ":
			result = result[:-1]

		if len(result) == 0 or len(lunches) == 0:
			return "No lunch available at %s ):" % restaurant
		else:
			return result
Пример #25
0
	def fetchFood(self, restaurant, today=None):
		response = utility.read_url(self.url)
		data = response["data"]

		day = None
		week = None
		lunches = []
		ofset = 0
		result = ""

                found_restaurant = "Monark"
		ldays = ["m\xc3\xa5ndag", "tisdag", "onsdag", "torsdag", "fredag","l\xc3\xb6rdag", "s\xc3\xb6ndag"]
		days = {"m\xc3\xa5ndag": "gfx/meny_day_1.gif",
                        "m\xe5ndag": "gfx/meny_day_1.gif", 
                        "tisdag": "gfx/meny_day_2.gif",
                        "onsdag": "gfx/meny_day_3.gif",
                        "torsdag": "gfx/meny_day_4.gif",
                        "fredag": "gfx/meny_day_5.gif",
                        }
		if not today:
                        day = ldays[datetime.now().isoweekday()-1]
			today = days[day]
                else:
                        day = today
                        today = days[today]

                # find week
		search = re.search('<FONT CLASS="rubrik"><B>Vecka ([0-9]+)</B></FONT></TD>', data)
		if search:
			week = search.group(1)

		# find day
		start = data.lower().find(today)
                end = data[start+1:].find("gfx/meny_day")
                if end == 0:
                        end = len(data)

                lunches = re.findall('<FONT CLASS="rubrik"><B>&nbsp;([^:]*)[^>]*>([^<]*)</FONT>', data[start:start+end])
		if lunches:
                        #print lunches

			# create result
			cnt = 1
			if day != None and week != None and found_restaurant != None:
				result = "Lunch " + found_restaurant + " " + day + " v" + week + " "

			for lunch in lunches:
				result += lunch[0].strip() + ": " + lunch[1].strip() + " "
				cnt += 1

			if result[-1:] == " ":
				result = result[:-1]

		if len(result) == 0 or len(lunches) == 0:
			return "No lunch available at %s ):" % restaurant
		else:
			return result
Пример #26
0
    def lastcommit(self, repourl):
        fullurl = 'https://api.github.com/repos/%s/commits' % repourl
        response = utility.read_url(fullurl)

        json = loads(response['data'])
        message = json[0]['commit']['message']
        author = json[0]['author']['login']
        time = json[0]['commit']['author']['date']
        return "'%s' by %s, %s" % (message, author, self.prettify_date(time))
Пример #27
0
 def run_item_search_query(self, query_string):
     """Runs a search query, returns the JSON search result."""
     response = utility.read_url(self.URL_API % query_string)
     response_lines = response['data'].split('\n')
     
     # The JSON payload is on the second line
     if len(response_lines) >= 2:
         return response_lines[1]
     else:
         return None
Пример #28
0
    def trig_erekebabfredag(self, bot, source, target, trigger, argument):
        url = 'http://ere.kebabfredag.nu/api/erekebabfredag?format=json'
        response = utility.read_url(url)
        data = response["data"]
        jsondata = json.loads(data)

        if jsondata['isIt']:
            return u'Japp!'
        else:
            return u'Nepp :('
Пример #29
0
 def fetchFood(self, restaurant, today=None):
         response = utility.read_url(self.url)
         data = response["data"]
         start = data.lower().find("<p class=\"big\">")
         if start != -1:
                 start += 15
                 end = data[start:].lower().find("</p>")
                 if end != -1:
                         end = start + end
                         return "Donken lunch: " + data[start:end]
         else:
                 return "Donken lunch: dunno :("
Пример #30
0
    def fetchFood(self, restaurant, today=None):
        response = utility.read_url(self.url)
        data = response["data"]

        day = None
        week = None
        lunches = []
        ofset = 0
        found_restaurant = "JB"
        result = ""

        days = [
            "M\xc3\xa5ndag", "Tisdag", "Onsdag", "Torsdag", "Fredag",
            "L\xc3\xb6rdag", "S\xc3\xb6ndag"
        ]
        if not today:
            today = days[datetime.now().isoweekday() - 1]

        # find week
        search = re.search('kudden v ([0-9]+)', data)
        if search:
            week = search.group(1)

        # find day
        start = data.lower().find(today.lower())
        search = re.search("</p>", data[start:])
        if search:
            day = today

            lines = data[start:start + search.end()].split("\n")
            for line in lines:
                print line
                search = re.search("</strong>[ ]?([^<]*)<br />", line)
                if search:
                    print search.group(1)
                    lunches.append(search.group(1))

            # create result
            cnt = 1
            if day != None and week != None and found_restaurant != None:
                result = "Lunch " + found_restaurant + " " + day + " v" + week + " "

            for lunch in lunches:
                result += str(cnt) + ": " + lunch + " "
                cnt += 1

            if result[-1:] == " ":
                result = result[:-1]

        if len(result) == 0 or len(lunches) == 0:
            return "No lunch available at %s ):" % restaurant
        else:
            return result
Пример #31
0
    def trig_quote(self, bot, source, target, trigger, argument):
        queried_stock = argument.strip()
        if not queried_stock:
            return "usage: .quote AAPL"

        url = 'http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=nxl1p4' % queried_stock
        response = utility.read_url(url)

        if response:
            name, stock_exchange, value_price = response['data'].split("\",")
            value, price = value_price.split(",")
            return ("%s (%s) %s %s" % (name, stock_exchange, value, price)).replace("\"", "")
Пример #32
0
 def fetchFood(self, restaurant, today=None):
     response = utility.read_url(self.url)
     data = response["data"]
     start = data.lower().find("<p class=\"big\">")
     if start != -1:
         start += 15
         end = data[start:].lower().find("</p>")
         if end != -1:
             end = start + end
             return "Donken lunch: " + data[start:end]
     else:
         return "Donken lunch: dunno :("
Пример #33
0
    def fetchFood(self, restaurant, today=None):
        response = utility.read_url(self.url)
        data = response["data"]

        day = None
        week = None
        lunches = []
        ofset = 0
        found_restaurant = "JB"
        result = ""

        days = [
            "M\xc3\xa5ndag", "Tisdag", "Onsdag", "Torsdag", "Fredag",
            "L\xc3\xb6rdag", "S\xc3\xb6ndag"
        ]
        if not today:
            today = days[datetime.now().isoweekday() - 1]

        # find week
        search = re.search('kudden v ([0-9]+)', data)
        if search:
            week = search.group(1)

        # find day
        start = data.lower().find(today.lower())
        search = re.search("</p>", data[start:])
        if search:
            day = today

            lines = data[start:start + search.end()].split("\n")
            for line in lines:
                #print line
                search = re.search("</strong>[ ]?([^<]*)<br />", line)
                if search:
                    print search.group(1)
                    lunches.append(search.group(1))

            # create result
            cnt = 1
            if day != None and week != None and found_restaurant != None:
                result = "Lunch " + found_restaurant + " " + day + " v" + week + " "

            for lunch in lunches:
                result += str(cnt) + ": " + lunch + " "
                cnt += 1

            if result[-1:] == " ":
                result = result[:-1]

        if len(result) == 0 or len(lunches) == 0:
            return "No lunch available at %s ):" % restaurant
        else:
            return result
Пример #34
0
    def calculate(self, query):
        request = self.URL_API % query

        lazy_json_response = utility.read_url(request)['data'].decode('utf-8')
        strict_json_response = self.lazy_to_strict_json(lazy_json_response)
        unescaped_json_response = self.unescape_response(strict_json_response)
        response = json.loads(unescaped_json_response)
        
        if response['error'] == '0':
            response['error'] = ''
        
        return response
Пример #35
0
def random_product_list_dealextreme():
	# Fetch the web page
	response = utility.read_url("http://www.dealextreme.com/products.dx/random.gadgets")
	data = response["data"].replace("\r\n", "")
	
	result = []
	
	product_pattern = "\<a href='\/details.dx\/sku.(\d+)' style=\" font-family: Verdana; font-size: 9pt;\"\>\s+(.+?)\s+\<\/a\>" + \
			".+?" + "style=\"font-size: 11pt;\"\>\s+\$(\d+\.\d\d)\s+\<\/font\>"
	product_iterator = re.finditer(product_pattern, data)
	
	for match in product_iterator:
		result.append(match.groups([1, 2, 3])) # [sku, title, price]
	
	return result
Пример #36
0
    def look_up_item(self, url):
        response = utility.read_url(url)
        data = response['data'].decode('latin-1').replace('&nbsp;', u"")
        name_match = self.PATTERN_ITEM_NAME.search(data)
        if not name_match:
            return u"Could not extract product info :("

        name = utility.unescape(name_match.group('name'), True)
        price_match = self.PATTERN_ITEM_PRICE.search(data)
        if price_match:
            price = price_match.group('price')
        else:
            price = u"???:-"

        return u"%s, %s" % (name, price)
Пример #37
0
def ted_query(rawurl):
    response = utility.read_url(rawurl)
    data = response["data"]
    search = re.search('(?ims)hs:"talks/dynamic/(.+?)".+?vu=(http://[^&]+)/.+?\.flv&', data)

    if search:
        hqname = search.group(1)
        baseurl = search.group(2)

        if hqname and baseurl:
            result = "%s/%s" % (baseurl, hqname)
            return result
        else:
            return "Read the source, but couldn't capture all the groups."
    else:
        return "Read the source, but regex failed."
Пример #38
0
def random_product_list_dealextreme():
    # Fetch the web page
    response = utility.read_url(
        "http://www.dealextreme.com/products.dx/random.gadgets")
    data = response["data"].replace("\r\n", "")

    result = []

    product_pattern = "\<a href='\/details.dx\/sku.(\d+)' style=\" font-family: Verdana; font-size: 9pt;\"\>\s+(.+?)\s+\<\/a\>" + \
      ".+?" + "style=\"font-size: 11pt;\"\>\s+\$(\d+\.\d\d)\s+\<\/font\>"
    product_iterator = re.finditer(product_pattern, data)

    for match in product_iterator:
        result.append(match.groups([1, 2, 3]))  # [sku, title, price]

    return result
Пример #39
0
Файл: imdb.py Проект: plux/pynik
def imdb_search(name):
    # 	url = "http://akas.imdb.com/find?s=tt&q=%s" % name.replace(" ","+")
    url = "http://www.imdb.com/find?s=tt&q=%s" % utility.escape(name)

    response = utility.read_url(url)
    data = response["data"]

    # 	print url

    m = re.search("<title>(.*?) \((\d+)\)<\/title>", data)
    if m:
        return imdb_info(url)

    m = re.search('<a href="(\/(title)\/.*?)"', data)
    if m:
        url = "http://www.imdb.com%s" % m.group(1)
        return imdb_info(url)
Пример #40
0
def google_pages(string):
	url = 'http://www.google.se/search?q=' + utility.escape(string) + '&ie=UTF-8&oe=UTF-8'

	response = utility.read_url(url)
	data = response["data"]

	search = re.search('swrnum=(\d+)">', data)

	if search:
		result = search.group(1)

		if result:
			return int(result, 10)
		else:
			return None
	else:
		return None
Пример #41
0
def ted_query(rawurl):
    response = utility.read_url(rawurl)
    data = response["data"]
    search = re.search(
        '(?ims)hs:"talks/dynamic/(.+?)".+?vu=(http://[^&]+)/.+?\.flv&', data)

    if search:
        hqname = search.group(1)
        baseurl = search.group(2)

        if hqname and baseurl:
            result = "%s/%s" % (baseurl, hqname)
            return result
        else:
            return "Read the source, but couldn't capture all the groups."
    else:
        return "Read the source, but regex failed."
Пример #42
0
def get_tweet_text(idno):
    decoder = JSONDecoder()
    url = "https://api.twitter.com/1/statuses/show/" + idno + ".json"
    response = utility.read_url(url)

    if not response:
        # Couldn't connect to Twitter API
        return False

    try:
        data = decoder.decode(response['data'])
    except Exception:
        # Couldn't parse the API output
        return False

    # Use latin-1 to make IRCClient.send() happy
    return data.get(u"text").encode('latin-1', 'replace')
Пример #43
0
def get_tweet_text(idno):
	decoder = JSONDecoder()
	url = "https://api.twitter.com/1/statuses/show/" + idno + ".json"
	response = utility.read_url(url)

	if not response:
		# Couldn't connect to Twitter API
		return False

	try:
		data = decoder.decode(response['data'])
	except Exception:
		# Couldn't parse the API output
		return False

	# Use latin-1 to make IRCClient.send() happy
	return data.get(u"text").encode('latin-1', 'replace')
Пример #44
0
def tyda_lookup(word, lang):
    # Assemble URL
    url = "http://tyda.se/search?w=" + utility.escape(word) + "&source_lang=" + \
     utility.escape(lang)

    # Fetch result
    response = utility.read_url(url)
    if response:
        data = response["data"].replace("\n", "")
    else:
        return "Ohnoes, nothing found."

    # Look for word
    pattern = "\<span class=\"tyda_entry_base\"( title=\"[^\"]+\")?\>([^\<]+)\<\/span\>(.*?)\<\/td\>(.+?)\<\/table\>(\<table cellpadding=\"0\" cellspacing=\"0\" class=\"tyda_entry\"\>|\<script type=\"text\/javascript\"\>)"
    match = re.search(pattern, data)

    if not match:
        return "No result found, maybe you should try searching manually: " + url

    base_word = match.group(2).replace(" (", ", ").replace(")", "")
    inflected_word_data = match.group(3)
    inflected_words = []
    translation_data = match.group(4)
    translated_words = []

    pattern = "\<span class=\"tyda_entry_inflected\" title=\"[^\"]+\"\>([^\<]+)\<\/span\>"
    iterator = re.finditer(pattern, inflected_word_data)

    for match in iterator:
        inflected_words.append(
            match.group(1).replace(" (", ", ").replace(")", ""))

    if inflected_words:
        inflected_words = " (" + ", ".join(inflected_words) + ")"
    else:
        inflected_words = ""

    pattern = "\<a id=\"tyda_transR\d+\" href=\"\/search\/[^\"]+\"\>([^\<]+)\<\/a\>"
    iterator = re.finditer(pattern, translation_data)

    for match in iterator:
        translated_words.append(match.group(1))

    return base_word + inflected_words + ": " + ", ".join(
        translated_words) + " | " + url
Пример #45
0
    def trig_rss(self, bot, source, target, trigger, argument):
        url = argument

        response = utility.read_url(url)

        if not response:
            return "Couldn't fetch feed."

        data = response["data"]

        self.reader.parse(data)

        articles = self.reader.get_articles()

        if articles:
            return 'Newest: ' + ' | '.join(
                map(lambda x: "%s - %s" % (x[1], x[2]), articles[0:3]))
        else:
            return 'I couldn\'t find any articles there. :-('
Пример #46
0
def get_title(url):
    import urllib
    if not re.search('[^:]+:\/\/', url):
        url = 'http://' + url

    response = utility.read_url(url)
    if response == None:
        return None

    data = response["data"]
    data = data.replace("\r", "").replace("\n", "")

    m = re.search('<title[^>]*>\s*(.+?)\s*<\/title>', data,
                  re.IGNORECASE | re.MULTILINE)

    if m:
        title = m.group(1)
        title = re.sub('\s+', ' ', title)
        return utility.unescape(re.sub('<.+?>', '', title))
    else:
        return None
Пример #47
0
    def wp_get(self, language, item):
        url = "http://%s.wikipedia.org/wiki/%s" % (
            language, utility.escape(item.replace(" ", "_")))

        response = utility.read_url(url)

        if not response:
            return (None, None)

        data = response["data"]
        url = response["url"]

        # sometimes there is a nasty table containing the first <p>. we can't allow this to happen!
        pattern = re.compile("<table.*?>.+?<\/table>", re.MULTILINE)

        data = re.sub(pattern, "", data)

        m = re.search("<p>(.+?)<\/p>", data)
        if m:
            data = utility.unescape(m.group(1))
            data = re.sub("<.+?>", "", data)
            data = re.sub("\[\d+\]", "", data)

            index = data.rfind(".", 0, 300)

            if index == -1:
                index = 300

            if index + 1 < len(data) and data[index + 1] == '"':
                index += 1

            data = data[0:index + 1]

            if "Wikipedia does not have an article with this exact name." in data:
                data = None
        else:
            data = None

        return (url, data)
Пример #48
0
    def trig_down(self, bot, source, target, trigger, argument):

        queriedUrl = argument.strip()
        if not queriedUrl:
            return "usage: .down http://example.url"

        url = 'http://downforeveryoneorjustme.com/' + utility.escape(
            queriedUrl)

        response = utility.read_url(url)
        data = response["data"]

        search = re.search(
            r'<div id\=\"container\">\s+(.+)<p>.+?<\/p>.+<\/div>', data, re.S)

        if search:
            message = search.group(1)
            message = re.sub(r'<[^>]*?>', '', message)
            return message

        else:
            return "No result. downforeveryoneorjustme.com might be down. Oh, the irony."
Пример #49
0
    def trig_temp(self, bot, source, target, trigger, argument):
        """ Usage: .temp [City] Uses data from temperature.nu, please direct all complaints to www.temperatur.nu """
        argument = argument.strip()
        if argument:
            argument = argument.strip()
            self.places[source] = argument
            self.save()
        else:
            if source in self.places:
                argument = self.places[source]
            else:
                argument = 'ryd'

        argument_text = argument
        argument = utility.asciilize(argument)
        argument = utility.escape(argument)

        # awesome hack to include avesta!
        if argument.lower() == "avesta":
            actual_argument = "fors"
        else:
            actual_argument = argument

        url = "http://www.temperatur.nu/termo/%s/temp.txt" % actual_argument.lower(
        )
        response = utility.read_url(url)
        m = None

        if response:
            data = response["data"]
            m = _get_temp_re.match(data)

        if m and m.group(1) != "not found":
            return "Temperature in %s: %s." % (argument_text, m.group(1))
        else:
            return "Temperature in %s: invalid place, try using .yr instead." % (
                argument_text)
Пример #50
0
def posten_kolli_query(kolli_id):
    url = 'http://posten.se/tracktrace/TrackConsignments_do.jsp?trackntraceAction=saveSearch&consignmentId=' + utility.escape(
        kolli_id)
    response = utility.read_url(url)
    data = response["data"]

    search = re.search(
        '(?ims)<dt>Fr&aring;n:</dt><dd>(.*?)</dd>.*?rightcol.*h2>.*<h3>(.*?)</h3>\s*?(.*?)(<br/>|<div).*?<dt>Vikt:</dt><dd>(.*?)</dd>',
        data)

    if search:
        sender = search.group(1)
        date = search.group(2)
        status = search.group(3)
        weight = search.group(5)

        if date and status:
            result = "%s fr\xe5n %s | %s: %s | %s" % (
                weight, sender, date, re.sub("<.+?>", "", status), url)
            return result
        else:
            return None
    else:
        return None
Пример #51
0
def prisjakt_product(url):
	# Fetch the web page
	response = utility.read_url(url)
	data = response["data"]
	data = data.replace("&nbsp;", "")

	# Look for title
	title_pattern = "\<h1.*?\>(\<a href=\".+?\"\>)?(.+?)(\<\/a\>)?\<\/h1\>"
	title_match = re.search(title_pattern, data)

	if not title_match:
		# Failure
		return "Could not extract product info :("
	
	# Success
	title = utility.unescape(title_match.group(2))
	
	# Look for price
	price_pattern = "&auml;gsta: \<span class=\"pris\">(.|\n)(\d+:-)\<\/span\>"
	price_match = re.search(price_pattern, data)
	price = price_match.group(2)

	# Done, return info string
	return title + ", " + price + ", " + url
Пример #52
0
def get_fml_dom(identifier, lang):
    url = "http://api.betacie.com/view/" + identifier + \
      "/nocomment?key=readonly&language=" + lang
    response = utility.read_url(url)
    data = response["data"]
    return minidom.parseString(data)
Пример #53
0
    def lookup_direct(self, reference):
        decoder = JSONDecoder()
        api_url = self.api_base_url + u"lookup/1/.json?uri=" + reference.URI()
        response = utility.read_url(api_url)

        if not response:
            return None

        try:
            data = decoder.decode(response['data'])
        except StandardError:
            return None

        if not data.get(u"info"):
            return None

        # Album reference
        if reference.type == u"album":
            metadata = data.get(u"album", {})
            album = metadata.get(u"name", u"Unknown album")
            artist = metadata.get(u"artist", u"Unknown artist")
            year = metadata.get(u"released", u"Unknown year")
            return u"%s: %s (%s)" % (artist, album, year)

        # Artist reference
        elif reference.type == u"artist":
            metadata = data.get(u"artist", {})
            artist = metadata.get(u"name", u"Unknown artist")
            return u"%s" % artist

        # Track reference
        elif reference.type == u"track":
            #return u"track"
            # Extract some dicts from the data
            metadata = data.get(u"track", {})
            metadata_album = metadata.get(u"album", {})
            metadata_artists = metadata.get(u"artists", [{}])

            # Extract info from the dicts
            album = metadata_album.get(u"name", u"Unknown album")
            artists = map(
                lambda artist: artist.get(u"name", u"Unknown artist"),
                metadata_artists)
            artist = ", ".join(artists)
            duration = metadata.get(u"length", u"0.0")
            popularity = metadata.get(u"popularity", u"0.0")
            track = metadata.get(u"name", u"Unknown track")
            year = metadata_album.get(u"released", u"Unknown year")

            # Convert strings to floats
            try:
                duration = float(duration)
            except ValueError:
                duration = 0.0
            try:
                popularity = float(popularity)
            except ValueError:
                popularity = 0.0

            # Construct result
            return u"%s: %s | %s (%s) | Track popularity %d%%, Track duration %d:%02d" % \
              (artist, track, album, year, int(round(popularity*100)), duration / 60, duration % 60)

        # Unsupported reference
        else:
            return None
Пример #54
0
def system_status(product_id, store_id):
    # Fetch the web page
    url = "http://systembolaget.se/SokDrycker/Produkt?VaruNr=" + product_id + \
      "&Butik=" + store_id
    response = utility.read_url(url)
    if response:
        data = response["data"].replace("\r", "")
    else:
        return "Ingen produkt med det artikelnumret hittades."

    # Look for title
    title_pattern = "class=\"rubrikstor\"\>(.+)\n"
    title_match = re.search(title_pattern, data)

    if not title_match:
        # Failure
        return "Hittade inte produktinfon :("

    # Set product info and store name variables
    title_text = title_match.group(1)

    origin_pattern = "class=\"text_tabell_rubrik\"\>Land\<\/td\>[\n\s]+\<td class=\"text_tabell\"\>(\<B\>\<A.+?\>)?([\w\s]+)(\<\/A\>\<\/B\>)?\<\/td\>"
    origin_text = re.search(origin_pattern, data).group(2)

    percentage_pattern = "class=\"text_tabell_rubrik\"\>Alkoholhalt\<\/td\>[\n\s]+\<td class=\"text_tabell\"\>(.+?)\<\/td\>"
    percentage_text = re.search(percentage_pattern,
                                data).group(1).replace(",", ".")

    store_pattern = "\<option selected=\"selected\" value=\"" + store_id + "\"\>(.+?)\<\/option\>"
    store_text = re.search(store_pattern, data).group(1)

    # Look for available packaging options for this product
    product_pattern = "\<img src=\"\/images\/button_plus\.gif\" class=\"LaggTillMinaVaror\" " + \
      "align=\"absmiddle\" onMouseover=\"ddrivetip\(\'L.gg till i \<b\>Mina varor\<\/b\>\'\)\" " + \
      "onMouseout=\"hideddrivetip\(\)\" onClick=\"LaggTillEnArtikel\(\'\d+\'\);\"\>" + \
      "(.+?)" + "\<\/td\>\<td class=\"text_tabell\" valign=\"Top\" " + \
      "background=\"\/images\/tab_bg_blueline\.gif\" style=\"padding-top:5px;\"\>" + \
      "([\w\s]+)" + "\<\/td\>\<td class=\"text_tabell\" align=\"Right\" valign=\"Top\" " + \
      "background=\"\/images\/tab_bg_blueline\.gif\" style=\"padding-top:5px;\"\>" + \
      "\((.+?)\)" + "\<\/td\>\<td class=\"text10pxfet\" align=\"Right\" valign=\"Top\" " + \
      "background=\"\/images\/tab_bg_blueline\.gif\" width=\"87\" style=\"padding-top:5px;\"\>" + \
      "(.+?)" + "\<\/td\>\<td class=\"text_tabell\" align=\"Left\" valign=\"Top\" " + \
      "background=\"\/images\/tab_bg_blueline\.gif\" width=\"183\" " + \
      "bgcolor=\"#FFFFFF\" style=\"padding-top:5px;\"\>.*?\<\/td\>" + \
      "\<td class=\"text_tabell\" valign=\"Top\" background=\"\/images\/tab_bg_blueline\.gif\" " + \
      "style=\"padding-top:5px;\"\>" + \
      "\<strong\>Lagersaldo: \<\/strong\>(\d+) st&nbsp;&nbsp;&nbsp;\<strong\>" + \
      "Plats i butiken: \<\/strong\>(.+?)\<\/td\>"
    product_iterator = re.finditer(product_pattern, data)
    product_list = []

    for match in product_iterator:
        # An available packaging option has been found, let's calculate the APK value.
        apk_value = float(percentage_text[:-2]) / 100  # He
        apk_value *= float(match.group(2)[:-3])  # V (expected to be in ml)
        apk_value /= float(match.group(4))  # P

        # Add it to the list...
        format_string = "%s %s: %s kr (%s kr/l, APK " + str(round(apk_value, 2)) + \
          "), %s st, hylla %s"
        product_list.append(format_string % match.group(1, 2, 4, 3, 5, 6))

    if not product_list:
        # No available packaging options found
        product_list.append("Varan finns inte i denna butik.")

    # Assemble string
    result_string = "#%s: %s, %s, %s | %s | %s | %s" % \
      (product_id, title_text, origin_text, percentage_text, store_text, " | ".join(product_list), url)

    # Unescape things like &nbsp;
    return utility.unescape(result_string)
Пример #55
0
    def trig_google(self, bot, source, target, trigger, argument):
        url = 'http://www.google.com/search?rls=en&q=' + utility.escape(
            argument) + '&ie=UTF-8&oe=UTF-8'

        response = utility.read_url(url)

        data = response["data"]

        data = re.sub(r"\n|\r|\r\n", "", data)
        data = re.sub(r" +", " ", data)

        print data

        # try to extract video result
        m = re.search(
            r'Video results for <em>.*?<\/em>.*?<td valign=top style="padding-right:10px"><a href="(.*?)" class=l.*?>(.*?)</a><br>',
            data)
        if m:
            text = utility.unescape(m.group(2))
            text = re.sub('<.+?>', '', text)
            link = m.group(1)
            return "%s - %s | %s" % (text, link, url)

        # try to extract calculator result
        #m = re.search('<td><img src="\/images\/icons\/onebox\/calculator-40\.gif" ?width=40 height=40 alt=""><td>&nbsp;<td style="vertical-align:top" >(<h2 class=r( style="font-size:\d+%")?>)?<b>(.*?)<\/b>', data)
        m = re.search('.*?font-size:138%">(.*?)<', data)
        if m:
            answer = m.group(1)
            answer = answer.replace(' &#215;', '×').replace('<sup>', '^')
            answer = re.sub('<.+?>', '', answer)
            return answer

        # try to extract definition
        m = re.search(
            '<img src="\/images\/dictblue\.gif" width=40 height=30 alt=""><td valign=top.*?>(.*?)<br>',
            data)
        if m:
            definition = utility.unescape(m.group(1))
            definition = re.sub('<.+?>', '', definition)
            return definition

        # try to extract weather
        m = re.search(
            '<b>Weather<\/b> for <b>(.+?)<\/b>.+?<b>(-?\d+).*C<\/b>.+?Current: <b>(.+?)<\/b>',
            data)

        if m:
            location = m.group(1)
            temperature = m.group(2)
            weather = m.group(3)
            return "%s: %s - %s" % (location, temperature, weather)

        # try to extract time
        m = re.search(
            'alt=""><td valign=middle><b>(.*?)<\/b> .+?day \((.*?)\) - <b>Time</b> in (.*?)<\/table>',
            data)

        if m:
            time = m.group(1)
            timezone = m.group(2)
            location = m.group(3)
            location = re.sub('<.+?>', '', location)

            return "Time in %s: %s (%s)" % (location, time, timezone)

        # try to extract first hit
        m = re.search(
            '<li class=g><h3 class=r><a href="(.*?)".*?>(.*?)<\/a>(.*?)</div>',
            data)
        if m:
            text = utility.unescape(m.group(2))
            text = re.sub('<.+?>', '', text)

            link = m.group(1)

            return "%s - %s | %s" % (text, link, url)
        else:
            return url