def test_load_url_string(self): url1 = "https://httpbin.org/get" data1 = Commons.load_url_string(url1) data1split = data1.split("\n") assert data1split[0] == "{", "First line incorrect." assert data1split[1] == " \"args\": {}, ", "String response incorrect." url2 = "https://httpbin.org/headers" headers2 = [["User-Agent", "Example data"]] data2 = Commons.load_url_string(url2, headers2) data2split = data2.split("\n") assert data2split[0] == "{", "First line incorrect." assert data2split[1] == " \"headers\": {", "String response incorrect." assert "\"User-Agent\": \"Example data\"" in data2, "Headers missing from request."
def check_feed(self): """ Checks the feed for any updates :return: list of ElementTree XML elements """ rss_data = Commons.load_url_string(self.url) rss_elem = ElementTree.fromstring(rss_data) channel_elem = rss_elem.find("channel") new_items = [] # Update title title_elem = channel_elem.find("title") self.title = title_elem.text # Loop elements, seeing when any match the last item's hash latest_hash = None for item_elem in channel_elem.findall("item"): item_xml = ElementTree.tostring(item_elem) item_hash = hashlib.md5(item_xml).hexdigest() if latest_hash is None: latest_hash = item_hash if item_hash == self.last_item_hash: break new_items.append(item_elem) # Update last item hash self.last_item_hash = latest_hash self.last_check = datetime.now() # Return new items return new_items
def run(self, line, user_obj, destination_obj=None): api_key = user_obj.get_server().get_hallo().get_api_key("thecatapi") if api_key is None: return "No API key loaded for cat api." url = "http://thecatapi.com/api/images/get?format=xml&api_key=" + api_key + "&type=gif" xml_string = Commons.load_url_string(url) doc = minidom.parseString(xml_string) cat_url = doc.getElementsByTagName("url")[0].firstChild.data return cat_url
def run(self, line, user_obj, destination_obj): # Get input feed_url = line.split()[0] feed_period = "PT3600S" if len(line.split()) > 1: feed_period = line.split()[1] # Get current RSS feed list function_dispatcher = user_obj.server.hallo.function_dispatcher feed_check_class = function_dispatcher.get_function_by_name("rss check") feed_check_obj = function_dispatcher.get_function_object(feed_check_class) # type: FeedCheck feed_list = feed_check_obj.rss_feed_list # Check link works try: Commons.load_url_string(feed_url, []) except urllib.error.URLError: return "Error, could not load link." # Check period is valid try: feed_delta = Commons.load_time_delta(feed_period) except ISO8601ParseError: return "Error, invalid time period." # Create new rss feed rss_feed = RssFeed() rss_feed.server_name = user_obj.server.name rss_feed.url = feed_url rss_feed.update_frequency = feed_delta if destination_obj.is_channel(): rss_feed.channel_name = destination_obj.name else: rss_feed.user_name = destination_obj.name # Update feed try: rss_feed.check_feed() except ParseError: return "Error, RSS feed could not be parsed." # Add new rss feed to list feed_list.add_feed(rss_feed) # Save list feed_list.to_xml() # Return output return "I have added new RSS feed titled \"" + rss_feed.title + "\""
def get_rss_data(self): headers = None # Tumblr feeds need "GoogleBot" in the URL, or they'll give a GDPR notice if "tumblr.com" in self.url: headers = [["User-Agent", "Hallo IRCBot [email protected] (GoogleBot/4.5.1)"]] # Actually get the data rss_data = Commons.load_url_string(self.url, headers) # PHDComics doesn't always escape ampersands correctly if "phdcomics" in self.url: rss_data = rss_data.replace("& ", "& ") # Chainsaw suit has a blank first line if "chainsawsuit" in self.url and rss_data.startswith("\r\n"): rss_data = rss_data[2:] return rss_data
def run(self, line, user_obj, destination_obj=None): rgb_list = Commons.get_random_int(0, 256, 3) hex_code = (hex(rgb_list[0])[2:] + hex(rgb_list[1])[2:] + hex(rgb_list[2])[2:]).upper() url = "http://www.perbang.dk/rgb/" + hex_code + "/" url_data = Commons.load_url_string(url) colour_match = re.search('<meta name="Description" content="([A-Za-z ]+)#', url_data, re.M) if colour_match is None or colour_match.group(1) is None: output = "Randomly chosen colour is: #" + hex_code + " or rgb(" + str(rgb_list[0]) + "," + str( rgb_list[1]) + "," + str(rgb_list[2]) + ") " + url else: colour_name = colour_match.group(1) output = "Randomly chosen colour is: " + colour_name + " #" + hex_code + " or rgb(" + str( rgb_list[0]) + "," + str(rgb_list[1]) + "," + str(rgb_list[2]) + ") " + url return output