Example #1
0
def url_updater():
    #
    updatestatus = False
    #
    while True:
        #
        try:
            eml_lists = eml_list()
            #
            if len(eml_lists) > 0:
                #
                eml_ids = get_ids(eml_lists)
                #
                if len(eml_ids) > 0:
                    #
                    emls = get_emails(eml_ids)
                    updatestatus = process_emls(emls)
            #
            if updatestatus:
                msg_success = 'the url stored in config.json has been updated'
            else:
                msg_success = 'no new urls received'
            log_general(
                'Axiscare URL updater process completed - {msg_success}'.
                format(msg_success=msg_success))
            #
        except Exception as e:
            log_error(
                'Could not process emails to check for new URL notification - {error}'
                .format(error=e))
        #
        time.sleep(300)  #5mins
Example #2
0
def getlisting(channel_id, day):
    #
    # Use of bleb.org data requires API calls to be 2 seconds apart
    time.sleep(2)
    #
    headers = {
        'User-Agent':
        'TV::Fetch::XML, {app_name} - {email}'.format(
            app_name=cfg.app_name, email=cfg.developer_email),
        'Connection':
        'close',
        'content-type':
        'text/xml; charset=utf-8'
    }
    #
    r = requests.get(
        'http://www.bleb.org/tv/data/listings/{day}/{channel_id}.xml'.format(
            day=day, channel_id=channel_id),
        headers=headers)
    #
    if r.status_code == requests.codes.ok:
        return r.content
    else:
        log_error('Request to bleb.org failed - {status}'.format(
            status=r.status_code))
        return None
Example #3
0
def get_accesstoken(client_id, client_secret, pincode):
    url_access = 'https://api.home.nest.com/oauth2/access_token'
    #
    headers = {
        'Connection': 'close',
        'User-Agent': 'Linux/2.6.18 UDAP/2.0 CentOS/5.8',
        'Content-type': 'application/x-www-form-urlencoded'
    }
    #
    payload = {
        'client_id': client_id,
        'client_secret': client_secret,
        'code': pincode,
        'grant_type': 'authorization_code'
    }
    #
    r = requests.post(url_access, headers=headers, data=payload)
    #
    if r.status_code != requests.codes.ok:
        log_error('Access Token not returned by Nest server')
        raise Exception()
    #
    try:
        response = json.loads(r.text)
    except Exception as e:
        log_error('Access Token not returned by Nest server .')
        raise Exception()
    #
    token = response['access_token']
    tokenexpiry = (datetime.datetime.now() + datetime.timedelta(
        seconds=response['expires_in'])).strftime(date_format)
    #
    return {'token': token, 'tokenexpiry': tokenexpiry}
Example #4
0
 def sendCmd(self, request):
     #
     command = request['command']
     #
     try:
         #
         if not self._tokencheck():
             log_error('Nest command could not be sent - error encountered with retrieving new authorisation code', dvc_id=self.dvc_id())
             return False
         #
         nest_model = request['nest_model']
         nest_device_id = request['nest_device_id']
         nest_device = request['nest_device']
         value = request['value']
         #
         if nest_device == 'thermostats':
             #
             if command == 'temp':
                 json_cmd = {'target_temperature_' + self._temp_unit : float(value)}
                 if self._send_nest_json(json_cmd, nest_model, nest_device, nest_device_id):
                     return True
             #
         return False
         #
     except Exception as e:
         log_error('Exception encountered sending ' + command + ' - ' + str(e), dvc_id=self.dvc_id())
         return False
Example #5
0
def send_ip(ip):
    try:
        msg = compile_email(ip)
        return send_email(msg)
    except Exception as e:
        log_error(e)
        return False
Example #6
0
 def getData(self, request):
     try:
         if request['data'] == 'articles':
             return json.dumps(self.getNews(request['sources'].split(' '), request['sortby']))
     except Exception as e:
         log_error('Failed to return requested data {request} - {error}'.format(request=request['data'],
                                                                                  error=e))
         return False
Example #7
0
def cache_tvchannels():
    log = log_msg(request, uri_cache_tvchannels)
    try:
        log_general(log)
        return HTTPResponse(body=compile_tvchannels(), status=200)
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        return HTTPResponse(status=404)
Example #8
0
 def updateData(self):
     try:
         self.data_cache = self.getForecast()
         self.data_lastupdate = datetime.now()
     except Exception as e:
         log_error(
             'Failed to return requested weather data - {error}'.format(
                 error=e))
         return False
Example #9
0
 def add_pages(self, responses, parent_dict):
     for response in responses:
         # To avoid add exists url, we check and add returned url
         try:
             if not self.is_contain(response.url):
                 self.add_page(response, response.url,
                               parent_dict[response.url])
         except KeyError:
             log.log_error("{} doesn't exist".format(response.url))
Example #10
0
 def getData(self, request):
     try:
         if request['data'] == 'alllistings':
             return json.dumps(self._listings.copy())
     except Exception as e:
         log_error(
             'Failed to return requested data {request} - {error}'.format(
                 request=request['data'], error=e))
         return False
Example #11
0
def send_favicon():
    log = log_msg(request, uri_favicon)
    try:
        root = os.path.join(os.path.dirname(__file__), 'imgs/logo')
        log_general(log)
        return static_file('favicon.ico', root=root)
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        raise HTTPError(500)
Example #12
0
 def add_pages(self, responses, parent_dict):
     for response in responses:
         # To avoid add exists url, we check and add returned url
         try:
             if not self.is_contain(response.url):
                 self.add_page(response,
                               response.url,
                               parent_dict[response.url])
         except KeyError:
             log.log_error("{} doesn't exist".format(response.url))
Example #13
0
def _carers_nownext():
    try:
        data = carer_info()
        if data:
            return HTTPResponse(data, status=200)
        else:
            return HTTPResponse(status=400)
    except Exception as e:
        log_error('Could not return current/next carer details to client')
        raise HTTPError(500)
Example #14
0
 def getNews(self, request_sources, sortby):
     #
     sortby = str(sortby)
     #
     sources_details = get_sources()
     news = {'news_articles': {}}
     #
     for request_src in request_sources:
         #
         request_src = str(request_src)
         #
         src_details = {'name': '',
                        'description': '',
                        'url': '',
                        'category': '',
                        'logos': {'small': '',
                                  'medium': '',
                                  'large': ''}}
         #
         try:
             if sources_details['status'] == 'ok':
                 #
                 for src_detail_item in sources_details['sources']:
                     if str(src_detail_item['id']) == request_src:
                         #
                         # if the requested sortby is not available for the source,
                         # default to first in list for the source
                         if not sortby in src_detail_item['sortBysAvailable']:
                             sortby = src_detail_item['sortBysAvailable'][0]
                         #
                         src_details = {'name': src_detail_item['name'],
                                        'description': src_detail_item['description'],
                                        'url': src_detail_item['url'],
                                        'category': src_detail_item['category'],
                                        'logos': {'small': src_detail_item['urlsToLogos']['small'],
                                                  'medium': src_detail_item['urlsToLogos']['medium'],
                                                  'large': src_detail_item['urlsToLogos']['large']}}
                         break
         except Exception as e:
             pass
         #
         try:
             tmp_articles = get_articles(request_src, sortby)
             #
             if tmp_articles['status'] == 'ok':
                 #
                 news['news_articles'][request_src] = {}
                 news['news_articles'][request_src]['source_details'] = src_details
                 news['news_articles'][request_src]['articles'] = tmp_articles['articles']
         except Exception as e:
             log_error('Unable to retrieve news articles from newsapi.org - {source}:{sortby}'.format(source=request_src,
                                                                                                        sortby=sortby))
         #
     #
     return news
Example #15
0
 def sendCmd(self, request):
     #
     try:
         msg_command = ''
         code = False
         response = False
         #
         if request['command'] == 'enterpin':
             msg_command = 'enterpin'
             try:
                 rsp = []
                 for num in self._pin():
                     code = self.commands[num]
                     rsp.append(
                         self._send_telnet(self._ipaddress(),
                                           self._port(),
                                           data=code))
                     log_msg = create_device_log_message(
                         code, self._type, self._ipaddress(), response)
                     log_general(log_msg, dvc_id=self.dvc_id())
                 response = not (False in rsp)
             except Exception as e:
                 response = False
         elif request['command'] == 'channel':
             msg_command = request['chan']
             response = self._send_telnet(ipaddress=self._ipaddress(),
                                          port=self._port(),
                                          data=("SETCH {}\r").format(
                                              request['chan']),
                                          response=True)
             if response.startswith('CH_FAILED'):
                 log_msg = create_device_log_message(
                     'channel', self._type, self._ipaddress(), response)
                 log_general(log_msg, dvc_id=self.dvc_id())
                 return False
         elif request['command'] == 'command':
             msg_command = request['code']
             code = self.commands[request['code']]
             try:
                 response = self._send_telnet(self._ipaddress(),
                                              self._port(),
                                              data=code)
             except:
                 response = False
         #
         log_msg = create_device_log_message(msg_command, self._type,
                                             self._ipaddress(), response)
         log_general(log_msg, dvc_id=self.dvc_id())
         return response
     except Exception as e:
         log_msg = create_device_log_message(request['command'], self._type,
                                             self._ipaddress(), e)
         log_error(log_msg, dvc_id=self.dvc_id())
         return False
Example #16
0
def _messages_current():
    try:
        data = messages_current()
        if data:
            return HTTPResponse(data, status=200)
        else:
            return HTTPResponse(status=400)
    except Exception as e:
        log_error(
            'Could not return messages to client - {error}'.format(error=e))
        raise HTTPError(500)
Example #17
0
 def getData(self, request):
     try:
         if request['data'] == 'recordings':
             self._check_recordings()
             return self.recordings
         elif request['data'] == 'channel':
             return self._getChan()
     except Exception as e:
         log_error(
             'Failed to return requested data {request} - {error}'.format(
                 request=request['data'], error=e))
         return False
Example #18
0
def get_image(category, filename):
    log = log_msg(request, uri_image)
    try:
        root = os.path.join(os.path.dirname(__file__),
                            'imgs/{img_cat}'.format(img_cat=category))
        mimetype = filename.split('.')[1]
        log_general(log)
        return static_file(
            filename,
            root=root,
            mimetype='image/{mimetype}'.format(mimetype=mimetype))
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        raise HTTPError(500)
Example #19
0
def cache_subscribe():
    log = log_msg(request, uri_cache_subscribe)
    try:
        #
        r = request
        #
        categories = request.json['categories']
        ipaddress = request.json['ipaddress']
        port = request.json['port']
        #
        log_general(log)
        return HTTPResponse(status=200)
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        return HTTPResponse(status=404)
def getForcast(frequency):
    # frequency = '3hourly' or 'daily'
    url = '{url}{uri}?res={frequency}&key={key}'.format(url=BASE_URL,
                                                        uri=URI_FORECAST_SITE.format(datatype='json',
                                                                                     locationId=LOCATION_id),
                                                        frequency=frequency,
                                                        key=get_config_weather_metoffice_appkey())
    r = requests.get(url)
    #
    if r.status_code == requests.codes.ok:
        log_general('Met Office weather forecast data retrieved successfully for frequency {frequency}'.format(frequency=frequency))
        return r.json()
    else:
        log_error('Could not retrieve Met Office weather forecast for frequency {frequency}'.format(frequency=frequency))
        return False
Example #21
0
 def getData(self, request):
     #
     try:
         #
         if request['data'] == 'data':
             nest_data = self._read_json_all()
             nest_data.pop('metadata')
             return nest_data
         else:
             return False
         #
     except Exception as e:
         log_error('Failed to return requested data {request} - {error}'.format(request=request['data'],
                                                                                error=e))
         return False
Example #22
0
 def build_listing_dict(self):
     #
     data = read_list_channels()
     #
     for cat in data['channels']:
         _temp_chan = {}
         for chan in data['channels'][cat]['channels']:
             try:
                 _temp_chan[str(chan)] = self._getlisting(
                     data['channels'][cat]['channels'][chan])
             except Exception as e:
                 log_error(
                     'Could not retrieve listings for {channel} - {error}'.
                     format(channel=data['channels'][cat]['channels'][chan]
                            ['name'],
                            error=e))
             self._listings[str(cat)] = _temp_chan
def getSunRiseSet(date, latitude, longitude):
    url = '{url}{uri}'.format(url=BASE_URL,
                              uri=QUERY_REQUEST.format(lat=latitude,
                                                       lng=longitude,
                                                       date=date))
    r = requests.get(url)
    #
    if r.status_code == requests.codes.ok:
        log_general(
            'Sunset-Sunrise weather forecast data retrieved successfully for date {date}'
            .format(date=date))
        return r.json()
    else:
        log_error(
            'Could not retrieve Sunset-Sunrise data for date {date}'.format(
                date=date))
        return {'status': 'ERROR'}
Example #24
0
 def start_crawling(self):
     response = request.get_request(self.domain)
     if response is None:
         quit(log_error("Cannot start script."))
     return pages.PageCollection(
         self.domain,
         response,
         self.domain
     )
Example #25
0
def update_cache():
    #
    try:
        #
        url = get_config_axiscare_url()
        #
        if not url == '':
            data = getData(url)
            #
            cache.carers = getCarerDetails(data)
            #
        else:
            cache.carers = {}
        #
    except Exception as e:
        log_error('Error when updating Axiscare cache data - {error}'.format(
            error=e))
        raise Exception
def create_bindings(_things, _infoservices):
    #
    data = get_cfg_bindings_json()
    #
    for group in data['bindings']['groups']:
        #
        _things[group['sequence']] = {}
        group_things = {}
        #
        for thing in group['things']:
            #
            try:
                group_things[thing['sequence']] = _create_thing(
                    thing['type'], group['sequence'], thing['sequence'])
                log_general('Thing created: {type}: {group}:{thing}'.format(
                    type=thing['type'],
                    group=group['name'],
                    thing=thing['name']))
            except Exception as e:
                log_error(
                    'Coud not create Thing: {type}: {group}:{thing}'.format(
                        type=thing['type'],
                        group=group['name'],
                        thing=thing['name']))
            #
        #
        _things[group['sequence']] = group_things
        #
    #
    for info in data['bindings']['info_services']:
        #
        try:
            _infoservices[info['sequence']] = _create_info(
                info['type'], info['sequence'])
            log_general('Info_service created: {type}: {info}'.format(
                type=info['type'], info=info['name']))
        except Exception as e:
            log_error('Coud not create Info_service: {type}: {info}'.format(
                type=info['type'], info=info['name']))
        #
    #
    log_general('All instances of Things and Info_services created')
def set_binding_detail(type, key, value):
    #
    data = json.load(
        open(os.path.join(os.path.dirname(__file__), 'list_bindings.json'),
             'r'))
    #
    data[type][key] = value
    try:
        #
        with open(
                os.path.join(os.path.dirname(__file__), 'list_bindings.json'),
                'w+') as output_file:
            output_file.write(
                json.dumps(data, indent=4, separators=(',', ': ')))
            output_file.close()
        #
        return True
    except Exception as e:
        log_error('Could not update "list_bindings.json" with new value - ' +
                  str(e))
        return False
Example #28
0
def _info_today():
    #
    data = {}
    #carers
    try:
        data['carers'] = carers_today()
    except Exception as e:
        log_error(
            'Failed to add carer data to response - {error}'.format(error=e))
        data['carers'] = {}
    #weather
    try:
        data['weather'] = weather.weather_today()
    except Exception as e:
        log_error(
            'Failed to add weather data to response - {error}'.format(error=e))
        data['weather'] = {}
    #
    if data:
        return HTTPResponse(data, status=200)
    else:
        return HTTPResponse(status=400)
Example #29
0
 def _getlisting(self, data):
     #
     listing_srcs = data['listingsrc']
     #
     if not listing_srcs:
         log_warning(
             'No sources available to retrieve listings for {channel}'.
             format(channel=data['name']))
         return {}
     #
     if len(listing_srcs) > 0:
         for src, code in listing_srcs.items():
             try:
                 if src == 'bleb':
                     log_general(
                         'TV listing for {channel} being retrieved from {src}'
                         .format(channel=data['name'], src=src))
                     return data_source_bleb.get(code)
             except Exception as e:
                 log_error(
                     'TV listing for {channel} failed to be retrieved from {src} - {error}'
                     .format(channel=data['name'], src=src, error=e))
     return {}
def get_messages(all=False):
    # Default will only retrieve current messages
    #
    values = get_data(get_config_google_googlesheetId(),
                      get_config_google_googlesheetRange())
    #
    msgs = []
    #
    if not values:
        log_error(
            'Problem encountered when processing messages from Google Sheet - No data found'
        )
    else:
        for row in values:
            try:
                msg = row[0]
                start = datetime.strptime(row[2], '%d/%m/%Y')
                end = datetime.strptime(row[3], '%d/%m/%Y')
                #
                if not row[1] == '':
                    countdown_target = datetime.strptime(row[1], '%d/%m/%Y')
                    msgObj = messageDetails(msg,
                                            start,
                                            end,
                                            countdown_target=countdown_target)
                else:
                    msgObj = messageDetails(msg, start, end)
                #
                if all or msgObj.is_current():
                    msgs.append(msgObj)
                #
            except Exception as e:
                log_error(
                    'Problem encountered when processing message from Google Sheet - {error}'
                    .format(error=e))
        return msgs
Example #31
0
def get_data_infoservice(service=False, resource_requested=False):
    #
    global infoservices
    #
    log = log_msg(request, uri_data_infoservice)
    #
    try:
        #
        if (not service) or (not resource_requested):
            log_error('{log} - {error}'.format(log=log, error='URI invalid'))
            raise HTTPError(404)
        #
        try:
            info_seq = get_cfg_info_seq(service)
        except Exception as e:
            log_error('{log} - {error}'.format(log=log, error=e))
            raise HTTPError(404)
        #
        data_dict = {'data': resource_requested}
        #
        if len(request.query.decode()) > 0:
            data_dict.update(request.query.decode())
        #
        #
        rsp = infoservices[info_seq].getData(data_dict)
        #
        response = HTTPResponse()
        enable_cors(response)
        #
        if isinstance(rsp, bool):
            if rsp:
                response.status = 200
            else:
                response.status = 400
        else:
            if bool(rsp):
                response.body = str(rsp)
                response.status = 200
            else:
                response.status = 400
        #
        log_general(log)
        return response
        #
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        raise HTTPError(500)
Example #32
0
def send_command_device(group=False, thing=False):
    #
    global devices
    #
    log = log_msg(request, uri_command_device)
    #
    try:
        #
        if (not group) or (not thing):
            log_error('{log} - {error}'.format(log=log, error='URI invalid'))
            raise HTTPError(404)
        #
        try:
            group_seq = get_cfg_group_seq(group)
            thing_seq = get_cfg_thing_seq(group, thing)
        except Exception as e:
            log_error('{log} - {error}'.format(log=log, error=e))
            raise HTTPError(404)
        #
        cmd_dict = request.json
        #
        rsp = devices[group_seq][thing_seq].sendCmd(cmd_dict)
        #
        log_general(log)
        #
        if isinstance(rsp, bool):
            return HTTPResponse(status=200) if rsp else HTTPResponse(
                status=400)
        else:
            return HTTPResponse(body=str(rsp),
                                status=200) if bool(rsp) else HTTPResponse(
                                    status=400)
        #
    except Exception as e:
        log_error('{log} - {error}'.format(log=log, error=e))
        raise HTTPError(500)