def get_info_by_name(self, show_name): """<Results> <show> <showid>2445</showid> <name>24</name> <link>http://www.tvrage.com/24</link> <country>US</country> <started>2001</started> <ended>0</ended> <seasons>8</seasons> <status>Returning Series</status> <classification>Scripted</classification> <genres><genre01>Action</genre01><genre02>Adventure</genre02><genre03>Drama</genre03></genres> </show> <show>""" if show_name.endswith(", The"): show_name = show_name.replace(", The", "") show_name = "The " + show_name show_xml = http_get(self.search_info_url % urllib.urlencode({"show": show_name})) dom = parseString(show_xml) shows = dom.getElementsByTagName("show") show_id = None for show in shows: if normalize(unescape(show.getElementsByTagName("name")[0].firstChild.data)) == normalize(show_name): show_id = int(show.getElementsByTagName("showid")[0].firstChild.data) break if show_id is None: logging.warn("Did not really find %s" % show_name) if len(shows): logging.warn("Taking first") return self.get_info(int(shows[0].getElementsByTagName("showid")[0].firstChild.data)) return None return self.get_info(show_id)
def _incident_app_reported(self, incident): app_reported = None if not incident['app']: return app_reported elif not self._app_service_name: return app_reported since_time = self._timestamp( incident['time_created'] - datetime.timedelta(minutes=self._app_service_grace_period)) until_time = self._timestamp(incident['time_occurred']) key = '{0}-{1}'.format(incident['time_created'], incident['time_occurred']) if not self._app_processed.has_key(key): qs = { 'fields': 'incident_number,created_on,status,service,trigger_summary_data', 'service': self._app_service_name, 'since': since_time, 'utill': until_time, 'limit': 100, 'offset': 0, } app_incidents = [] try: while True: resp = http_get( self._get_request_url + urllib.urlencode(qs), **self._http_base_params()) if resp: _incidents = resp.get('incidents', []) app_incidents += _incidents if len(_incidents) + resp['offset'] >= resp['total']: break else: qs['offset'] = len(_incidents) + resp['offset'] else: break except Exception as e: self._warn("failed to poll app status: {0}".format(e)) self._app_processed[key] = app_incidents self._debug( "total # of alerts found between {0} and {1}: {2}".format( since_time, until_time, len(app_incidents))) for _incident in self._app_processed.get(key, []): if incident['app'] in _incident['trigger_summary_data'][ 'description']: self._debug( "found a potential parent app hit: {0}".format(_incident)) app_reported = _incident break return app_reported
def _report_incident(self, incident, only_if_not_acknowledged): if not self._reportable(incident): self._summary['grace_period_applied'] += 1 return self._debug("reporting incident: {0}".format(incident)) if only_if_not_acknowledged: ## grab incident status and not spam incidents already acknowleged try: ## whether this incident has been reported or not qs = { 'fields': 'incident_number,status', 'incident_key': incident['incident_key'], 'status': 'acknowledged' } resp = http_get(self._get_request_url + urllib.urlencode(qs), **self._http_base_params()) if resp and resp['incidents']: self._info("incident: {1} ALREADY ACKnowledged: {0}!!!".format(resp['incidents'][0], incident['incident_key'])) incident.update(resp['incidents'][0]) self._summary['incidents_not_triggered_ins_processed'] += 1 return except Exception as e: self._warn("failed to grab incident status: {0}".format(e)) app_incident = self._incident_app_reported(incident) if app_incident: incident['time_created'] = incident['time_occurred'] self._info("NOT reporting incident since app '{0}' already done so: {1}".format(incident['app'], app_incident)) self._summary['incidents_not_triggered_app_processed'] += 1 return ## fire this event regardless def _reporter(count): msgs = [] for alert in incident['details']: msgs.append(alert.summary()) data = json.dumps({ 'service_key': self._service_api_key, 'incident_key': incident['incident_key'], 'event_type':'trigger', 'description':incident['description'], 'details': msgs }) resp = http_post('https://events.pagerduty.com/generic/2010-04-15/create_event.json', body=data, **self._http_base_params()) self._info("reported incident: {0} on try #{2} resp: {1}".format(incident['incident_key'], resp, count+1)) self._summary['incidents_triggered'] += 1 incident['reported'] = True ok, exceptions = do_request(_reporter, self._retry_total, self._retry_interval) for i, e in enumerate(exceptions): self._warn("try #{1}: failed to report incident: {0}".format(e, i+1)) return ok
def _incident_app_reported(self, incident): app_reported = None if not incident['app']: return app_reported elif not self._app_service_name: return app_reported since_time = self._timestamp(incident['time_created'] - datetime.timedelta(minutes=self._app_service_grace_period)) until_time = self._timestamp(incident['time_occurred']) key = '{0}-{1}'.format(incident['time_created'], incident['time_occurred']) if not self._app_processed.has_key(key): qs = { 'fields': 'incident_number,created_on,status,service,trigger_summary_data', 'service': self._app_service_name, 'since': since_time, 'utill': until_time, 'limit': 100, 'offset': 0, } app_incidents = [] try: while True: resp = http_get(self._get_request_url + urllib.urlencode(qs), **self._http_base_params()) if resp: _incidents = resp.get('incidents', []) app_incidents += _incidents if len(_incidents) + resp['offset'] >= resp['total']: break else: qs['offset'] = len(_incidents) + resp['offset'] else: break except Exception as e: self._warn("failed to poll app status: {0}".format(e)) self._app_processed[key] = app_incidents self._debug("total # of alerts found between {0} and {1}: {2}".format(since_time, until_time, len(app_incidents))) for _incident in self._app_processed.get(key, []): if incident['app'] in _incident['trigger_summary_data']['description']: self._debug("found a potential parent app hit: {0}".format(_incident)) app_reported = _incident break return app_reported
def get_info(self, show_id): """<Show> <name>Scrubs</name> <totalseasons>9</totalseasons> <showid>5118</showid> <showlink>http://tvrage.com/Scrubs</showlink> <started>Oct/02/2001</started> <ended></ended> <image>http://images.tvrage.com/shows/6/5118.jpg</image> <origin_country>US</origin_country> <status>Returning Series</status> <classification>Scripted</classification> <genres><genre>Comedy</genre></genres> <runtime>30</runtime> <network country="US">ABC</network> <airtime>21:00</airtime> <airday>Tuesday</airday> <timezone>GMT-5 -DST</timezone> <akas><aka country="LV">Dakterīši</aka><aka country="HU">Dokik</aka><aka country="SE">Första hjälpen</aka><aka country="NO">Helt sykt</aka><aka country="PL">Hoży doktorzy</aka><aka attr="Second Season" country="RU">Klinika</aka><aka attr="First Season" country="RU">Meditsinskaya akademiya</aka><aka country="DE">Scrubs: Die Anfänger</aka><aka country="RO">Stagiarii</aka><aka attr="French Title" country="BE">Toubib or not toubib</aka><aka country="FI">Tuho Osasto</aka><aka country="IL">סקרבס</aka></akas> <Episodelist> <Season no="1"> <episode><epnum>1</epnum><seasonnum>01</seasonnum> <prodnum>535G</prodnum> <airdate>2001-10-02</airdate> <link>http://www.tvrage.com/Scrubs/episodes/149685</link> <title>My First Day</title> <screencap>http://images.tvrage.com/screencaps/26/5118/149685.jpg</screencap></episode>""" logging.debug("Start downloading...") show_xml = http_get(self.show_info_url % show_id) logging.debug("Start parsing...") dom = parseString(show_xml) logging.debug("Start walking...") show_doc = dom.getElementsByTagName("Show")[0] seasons = show_doc.getElementsByTagName("Season") special = show_doc.getElementsByTagName("Special") seasons.extend(special) timezone = show_doc.getElementsByTagName("timezone")[0].firstChild.data tz = get_timezone_for_gmt_offset(timezone) last_show_date = None delta_params = show_doc.getElementsByTagName("airtime")[0].firstChild.data.split(":") delta = datetime.timedelta(hours=int(delta_params[0]), minutes=int(delta_params[1])) season_list = [] for season in seasons: try: season_nr = int(season.attributes["no"].value) except Exception: season_nr = False episode_list = [] for episode in season.getElementsByTagName("episode"): if season_nr is False: season_nr = int(episode.getElementsByTagName("season")[0].firstChild.data) try: title = unescape(episode.getElementsByTagName("title")[0].firstChild.data) except AttributeError: title = "" date_str = episode.getElementsByTagName("airdate")[0].firstChild.data try: date = datetime.datetime(*map(int, date_str.split("-"))) date = date + delta date = tz.localize(date) except ValueError: date = None if date is not None: if last_show_date is None or last_show_date < date: last_show_date = date try: epnum = int(episode.getElementsByTagName("seasonnum")[0].firstChild.data) except IndexError: epnum = 0 ep_info = TVEpisodeInfo(date=date, title=title, nr=epnum, season_nr=season_nr) episode_list.append(ep_info) season = TVSeasonInfo(season_nr=season_nr, episodes=episode_list) season_list.append(season) try: runtime = int(show_doc.getElementsByTagName("runtime")[0].firstChild.data) except IndexError: runtime = 30 name = unescape(show_doc.getElementsByTagName("name")[0].firstChild.data) country = show_doc.getElementsByTagName("origin_country")[0].firstChild.data network = unescape(show_doc.getElementsByTagName("network")[0].firstChild.data) genres = show_doc.getElementsByTagName("genre") genre_list = [] for genre in genres: if genre and genre.firstChild and genre.firstChild.data: genre_list.append(genre.firstChild.data) genre_str = "|".join(genre_list) active = show_doc.getElementsByTagName("ended")[0].firstChild if active is None or active.data == "0": active = True elif "/" in active.data: parts = active.data.split('/') if len(parts) == 3: try: month = monthsToNumber[parts[0]] day = int(parts[1]) year = int(parts[2]) if datetime.datetime.now() > datetime.datetime(year, month, day): active = False else: active = True except (ValueError, KeyError): active = True else: active = True else: active = False logging.debug("Return TVShowInfo...") return TVShowInfo(name=name, seasons=season_list, tvrage_id=show_id, country=country, runtime=runtime, network=network, timezone=timezone, active=active, genres=genre_str)
def get_info(self, show_id): """<Show> <name>Scrubs</name> <totalseasons>9</totalseasons> <showid>5118</showid> <showlink>http://tvrage.com/Scrubs</showlink> <started>Oct/02/2001</started> <ended></ended> <image>http://images.tvrage.com/shows/6/5118.jpg</image> <origin_country>US</origin_country> <status>Returning Series</status> <classification>Scripted</classification> <genres><genre>Comedy</genre></genres> <runtime>30</runtime> <network country="US">ABC</network> <airtime>21:00</airtime> <airday>Tuesday</airday> <timezone>GMT-5 -DST</timezone> <akas><aka country="LV">Dakterīši</aka><aka country="HU">Dokik</aka><aka country="SE">Första hjälpen</aka><aka country="NO">Helt sykt</aka><aka country="PL">Hoży doktorzy</aka><aka attr="Second Season" country="RU">Klinika</aka><aka attr="First Season" country="RU">Meditsinskaya akademiya</aka><aka country="DE">Scrubs: Die Anfänger</aka><aka country="RO">Stagiarii</aka><aka attr="French Title" country="BE">Toubib or not toubib</aka><aka country="FI">Tuho Osasto</aka><aka country="IL">סקרבס</aka></akas> <Episodelist> <Season no="1"> <episode><epnum>1</epnum><seasonnum>01</seasonnum> <prodnum>535G</prodnum> <airdate>2001-10-02</airdate> <link>http://www.tvrage.com/Scrubs/episodes/149685</link> <title>My First Day</title> <screencap>http://images.tvrage.com/screencaps/26/5118/149685.jpg</screencap></episode>""" logging.debug("Start downloading...") show_xml = http_get(self.show_info_url % show_id) logging.debug("Start parsing...") dom = parseString(show_xml) logging.debug("Start walking...") show_doc = dom.getElementsByTagName("Show")[0] seasons = show_doc.getElementsByTagName("Season") special = show_doc.getElementsByTagName("Special") seasons.extend(special) timezone = show_doc.getElementsByTagName("timezone")[0].firstChild.data tz = get_timezone_for_gmt_offset(timezone) last_show_date = None delta_params = show_doc.getElementsByTagName("airtime")[0].firstChild.data.split(":") delta = datetime.timedelta(hours=int(delta_params[0]), minutes=int(delta_params[1])) season_list = [] for season in seasons: try: season_nr = int(season.attributes["no"].value) except Exception: season_nr = False episode_list = [] for episode in season.getElementsByTagName("episode"): if season_nr is False: season_nr = int(episode.getElementsByTagName("season")[0].firstChild.data) try: title = unescape(episode.getElementsByTagName("title")[0].firstChild.data) except AttributeError: title = "" date_str = episode.getElementsByTagName("airdate")[0].firstChild.data try: date = datetime.datetime(*map(int, date_str.split("-"))) date = date + delta date = tz.localize(date) except ValueError, e: date = None if date is not None: if last_show_date is None or last_show_date < date: last_show_date = date try: epnum = int(episode.getElementsByTagName("seasonnum")[0].firstChild.data) except IndexError: epnum = 0 ep_info = TVEpisodeInfo(date=date, title=title, nr=epnum, season_nr=season_nr) episode_list.append(ep_info) season = TVSeasonInfo(season_nr=season_nr, episodes=episode_list) season_list.append(season)
def _report_incident(self, incident, only_if_not_acknowledged): if not self._reportable(incident): self._summary['grace_period_applied'] += 1 return self._debug("reporting incident: {0}".format(incident)) if only_if_not_acknowledged: ## grab incident status and not spam incidents already acknowleged try: ## whether this incident has been reported or not qs = { 'fields': 'incident_number,status', 'incident_key': incident['incident_key'], 'status': 'acknowledged' } resp = http_get(self._get_request_url + urllib.urlencode(qs), **self._http_base_params()) if resp and resp['incidents']: self._info( "incident: {1} ALREADY ACKnowledged: {0}!!!".format( resp['incidents'][0], incident['incident_key'])) incident.update(resp['incidents'][0]) self._summary['incidents_not_triggered_ins_processed'] += 1 return except Exception as e: self._warn("failed to grab incident status: {0}".format(e)) app_incident = self._incident_app_reported(incident) if app_incident: incident['time_created'] = incident['time_occurred'] self._info( "NOT reporting incident since app '{0}' already done so: {1}". format(incident['app'], app_incident)) self._summary['incidents_not_triggered_app_processed'] += 1 return ## fire this event regardless def _reporter(count): msgs = [] for alert in incident['details']: msgs.append(alert.summary()) data = json.dumps({ 'service_key': self._service_api_key, 'incident_key': incident['incident_key'], 'event_type': 'trigger', 'description': incident['description'], 'details': msgs }) resp = http_post( 'https://events.pagerduty.com/generic/2010-04-15/create_event.json', body=data, **self._http_base_params()) self._info("reported incident: {0} on try #{2} resp: {1}".format( incident['incident_key'], resp, count + 1)) self._summary['incidents_triggered'] += 1 incident['reported'] = True ok, exceptions = do_request(_reporter, self._retry_total, self._retry_interval) for i, e in enumerate(exceptions): self._warn("try #{1}: failed to report incident: {0}".format( e, i + 1)) return ok