Ejemplo n.º 1
0
def fetch_history_location(data,
                           token=None,
                           fr=None,
                           to=None,
                           device_type=None):
    import requests
    import datetime
    import pytz

    eastern = pytz.timezone('US/Eastern')

    data['token'] = token
    data['test_fetch'] = True
    url = "https://campus.cornelltech.io/api/location/history/"
    headers = {'Authorization': "Bearer " + token}
    payload = {'from': fr, 'to': to, 'device_type': device_type}
    res = requests.get(url, headers=headers, params=payload)
    if res.status_code == 200:
        parsed_data = list()
        for entry in res.json()['data']:
            ts = datetime.datetime.fromtimestamp(entry['timestamp'],
                                                 tz=pytz.utc)
            loc_dt = ts.astimezone(eastern)
            entry['timestamp'] = loc_dt.strftime("%c")
            parsed_data.append(entry)
        data['location'] = sorted(parsed_data, key=lambda x: x['timestamp'])
    else:
        raise AncileException("Couldn't fetch location from the server.")

    return data
Ejemplo n.º 2
0
def fetch_location(user, device_type=None):
    import requests
    import datetime
    import pytz

    token = get_token(user)
    print(token)
    data = {'output': []}
    data['output'].append(token)

    eastern = pytz.timezone('US/Eastern')
    data['token'] = token
    data['test_fetch'] = True
    url = "https://campus.cornelltech.io/api/location/mostrecent/"
    payload = {'device_type': device_type}
    headers = {'Authorization': "Bearer " + token}
    res = requests.get(url, headers=headers, params=payload)
    if res.status_code == 200:
        data['location'] = res.json()
        ts = datetime.datetime.fromtimestamp(data['location']['timestamp'],
                                             tz=pytz.utc)
        loc_dt = ts.astimezone(eastern)
        data['location']['timestamp'] = loc_dt.strftime("%c")
    else:
        raise AncileException("Couldn't fetch location from the server.")

    return data
Ejemplo n.º 3
0
    def get_empty_data_pair(self,
                            data_source,
                            name=None,
                            sample_policy='ANYF*.return'):
        """
        Returns a new Data Policy Pair object that has no data.

        :param data_source:
        :param name:
        :param sample_policy:
        :return:
        """

        if data_source == 'test':
            dp_pair = DataPolicyPair(PolicyParser.parse_it(sample_policy),
                                     None, data_source, self._username, None)
            self._active_dps[data_source] = dp_pair
            return dp_pair
        if self._user_policies.get(data_source, False):
            policy = self._user_policies[data_source]
            token = self._user_tokens[data_source]
            dp_name = name if name else data_source
            dp_pair = DataPolicyPair(policy,
                                     token,
                                     dp_name,
                                     self._username,
                                     self._user_private_data,
                                     app_id=self._app_id)
            self._active_dps[dp_name] = dp_pair
            return dp_pair
        else:
            raise AncileException(
                f"No policies for provider {data_source}, for"
                f" this user. Please ask the user to add the policy.")
Ejemplo n.º 4
0
def rdl_fetch(user=None,
              url='https://localhost:9980',
              api_to_fetch=None,
              username=None,
              **kwargs):
    """
    Fetches Web Search Data From RDL
    :param username:
    :param user:
    :param url:
    :param kwargs:
    :param api_to_fetch
    :return:
    """
    data = {'output': []}
    token = get_token(user)
    params = {'username': username}
    r = requests.get(f'{url}/test/api/{api_to_fetch}',
                     headers={'Authorization': f'Bearer {token}'},
                     params=params)

    if r.status_code == 200:
        data.update(r.json())
    else:
        raise AncileException(f"Request error: {r.json()}")
    return data
Ejemplo n.º 5
0
def in_geofences(geofences, data=None):
    """
    Determine if the user is within any of a given list of circular geofences.
    EXPECTS:
        'latitude' 'longitude' keys


    :param list geofences: A list of dictionaries containing the following fields,
                          'latitude', 'longitude', 'radius', 'label'
    :return: ['in_geofences']. The label of the geofence the user is in,
             "Location Unknown" if they are in no fence.
    :raises: AncileException if the user is in multiple fences.
    """
    location_tuple = (data.get('latitude'), data.get('longitude'))
    filter_lambda = lambda fence: location._in_geofence((fence[
        'latitude'], fence['longitude']), location_tuple, fence['radius'])
    filtered = list(filter(filter_lambda, geofences))
    label = list(map(lambda res: res['label'], filtered))

    result = ''
    if len(label) == 0:
        result = "Location Unknown"
    elif len(label) == 1:
        result = label[0]
    else:
        raise AncileException("Specified Geofences must not overlap")

    data['in_geofences'] = result
    return data
Ejemplo n.º 6
0
    def _store(self, obj, key):
        if not isinstance(obj, DataPolicyPair) and not isinstance(obj, Collection):
            raise AncileException("Cannot store this object.")

        if isinstance(obj, DataPolicyPair):
            self.redis.set(key, pickle.dumps(obj), ex=600)
        else:
            self.redis.set(key, pickle.dumps(obj))

        logger.info(f'Stored object {obj} under id \'{key}\'')
Ejemplo n.º 7
0
 def call(self):
     """
     Call the function
     """
     try:
         result = self.function(**self.params)
         return result
     except:
         error = f"Error executing function: {self.function_name}(). Log: {traceback.format_exc()}"
         logger.error(error)
         raise AncileException(error)
Ejemplo n.º 8
0
    def _load(self, key):
        value = self.redis.get(key)
        if value is None:
            raise AncileException("Nothing stored under this ID.")
        obj = pickle.loads(value)
        if isinstance(obj, DataPolicyPair):
            obj._was_loaded = True
            obj._load_key = key

        logger.info(f'Loaded object {obj} from id \'{key}\'')
        return obj
Ejemplo n.º 9
0
    def _store_encrypted(self, obj, key):
        if not isinstance(obj, DataPolicyPair):
            raise AncileException('Encrypted storage only applies to DataPolicyPairs')

        keys, crypt = encrypt(obj._data)
        obj._data.clear()
        obj._encryption_keys.update(**keys)

        self.redis.set(key, pickle.dumps(obj))
        logger.info(f'Stored encrypted object {obj} under id \'{key}\'')
        return crypt
Ejemplo n.º 10
0
def _get_primary_metadata(token):
    """
    Retrieve the metadata for a given user's primary google calendar.

    :param token: A Google access token with calendar scopes.
    :return: Primary calendar metadata as a dictionary.
    """
    import requests
    target_url = "https://www.googleapis.com/calendar/v3/users/me/calendarList"
    r = requests.get(target_url, headers={'Authorization': "Bearer " + token})

    if r.status_code != 200:
        raise AncileException("Request Error")

    calendar_list = r.json()
    primary = [x for x in calendar_list['items'] if x.get("primary")]

    if not primary:
        raise AncileException("No Calendar")

    return primary[0]
Ejemplo n.º 11
0
def get_latest_reddit_data(session):
    import requests

    url = "https://127.0.0.1/app-ancile/ui/tsblob/latest"
    payload = {"data_source_id": "redditSimulatorData"}
    headers = {"session": session}
    res = requests.get(url, cookies=headers, params=payload, verify=False)
    if res.status_code == 200:
        data = res.json()
    else:
        raise AncileException("Couldn't fetch data from databox.")

    return data
Ejemplo n.º 12
0
def retrieve_compiled(program, redis):
    redis_response = redis.get(program) if ENABLE_CACHE else None

    if redis_response is None:
        compile_results = compile_restricted_exec(program)
        if compile_results.errors:
            raise AncileException(compile_results.errors)
        if ENABLE_CACHE:
            redis.set(program, dill.dumps(compile_results.code), ex=600)
            logger.debug("Cache miss on submitted program")
        return compile_results.code
    logger.debug("Used cached program")
    return dill.loads(redis_response)
Ejemplo n.º 13
0
def aggregate_or(data):
    """
    Produce an aggregate value with the OR of the specified boolean fields
    for each input object.

    Note, this depends on the VALUE_KEYS parameter used by the decorator.

    :param data: A list of boolean values
    :return: The OR of the list values
    """
    if all(list(map(lambda x: isinstance(x, bool), data['aggregated']))):
        data['aggregate_or'] = any(data.pop('aggregated'))
    else:
        raise AncileException("All values to \"aggregate_and()\" must be booleans")

    return data
Ejemplo n.º 14
0
def get_last_location(user):
    """
    Make a request to the last location API with the user's access token.

    :param user: A UserSpecific data structure
    :return: The last location information given by the server.
    """
    token = get_token(user)
    data = {'output': []}
    r = requests.get('https://campusdataservices.cs.vassar.edu/api/last_known',
                     headers={'Authorization': f'Bearer {token}'})
    if r.status_code == 200:
        data.update(r.json())
    else:
        raise AncileException(f"Request error: {r.json()}")

    return data
Ejemplo n.º 15
0
def get_calendar_events_in_relative_window(user=None,
                                           min_time=0,
                                           max_time=1,
                                           **kwargs):
    """
    Retrieve Google calendar events for the primary calendar that are occurring
    within min_time minutes before now and max_time minutes after now.

    :param user: A UserSpecific data structure.
    :param min_time: The lower bound of the event window, given in minutes
                     before the current moment.
    :param max_time: The upper bound of the event window, given in minutes
                     after the current moment.
    :return: ['events']. Data for all events occurring in the window
                         (now - min, now + max) listed in the user's primary
                         google calendar.
    """
    from datetime import datetime, timedelta, timezone
    import requests

    data = {'output': []}
    token = get_token(user)

    def format_time(unformatted):
        return unformatted.isoformat()

    cal_id = _get_primary_metadata(token)['id']

    now = datetime.now(timezone.utc).astimezone()
    lower = format_time(now - timedelta(minutes=min_time))
    upper = format_time(now + timedelta(minutes=max_time))

    target_url = "https://www.googleapis.com/calendar/v3/calendars/" + \
                 cal_id + "/events?singleEvents=true&timeMin=" + \
                 lower + "&timeMax=" + upper

    r = requests.get(target_url, headers={'Authorization': "Bearer " + token})

    if r.status_code != 200:
        raise AncileException("Request Error")

    data['events'] = r.json()['items']
    return data
Ejemplo n.º 16
0
    def decorator_preamble(params):
        dp_pairs = dict()

        for name, param in params.items():
            if isinstance(param, DataPolicyPair):
                logger.info(f'Found DataPolicyPair for param: {name}')
                dp_pairs[name] = param
            elif isinstance(param, list):
                # check all items in list are DPPs
                if len(param) == len(
                    [True for i in param if isinstance(i, DataPolicyPair)]):
                    dp_pairs[name] = DataPolicyPair.combine_dpps_list(param)

        if len(dp_pairs) == 0:
            logger.info(f'Calling function without DPPs')
            raise AncileException(
                "Passed no DataPolicyPairs. Not Implemented yet.")

        return dp_pairs
Ejemplo n.º 17
0
    def process_call(self, command):

        logger.debug(f'Calling Reduction "{command.function_name}"')

        collection = command.params.get('collection', None)
        if not isinstance(collection, Collection):
            raise AncileException(
                'Please provide a Collection object as `collection` argument.')
        policy = Policy(
            collection.get_collection_policy().advance_policy(command))
        if not policy:
            raise PolicyError(
                f'Collection policy prevented execution of \'{f.__name__}\'')

        command.params['collection'] = [
            x._data for x in collection._data_points
        ]
        data = command.call()
        dp = DataPolicyPair(policy, None, 'reduce', None, None)
        dp._data = data

        return dp
Ejemplo n.º 18
0
    def process_call(self, command):
        logger.debug(f'Calling Aggregate "{command.function_name}"')

        new_data = dict() if not self.reduce else list()
        new_policy = None
        dp_pairs = command.params.get('data', [])
        set_users = set()
        app_id = None

        keys = command.params.pop('value_keys', None)
        if self.reduce:
            if isinstance(keys, str):
                keys = [keys] * len(dp_pairs)

            if len(keys) != len(dp_pairs):
                raise AncileException(
                    "value_keys must either be a single value or a list of the same length as data"
                )

        for index, dp_pair in enumerate(dp_pairs):
            if not (isinstance(dp_pair, DataPolicyPair)
                    or isinstance(dp_pair, Collection)):
                raise ValueError(
                    "You need to provide a Data object. Use get_data to get it."
                )
            if isinstance(dp_pair, Collection):
                collection_data = list()
                for dps in dp_pair._data_points:
                    collection_data.append(dps._data)
                if self.reduce:
                    new_data.append(collection_data)
                else:
                    new_data['collection'] = collection_data
                continue

            app_id = dp_pair._app_id if app_id is None else app_id

            if self.reduce:
                new_data.append(dp_pair._data[keys[index]])
            else:
                new_data[
                    f'{dp_pair._username}.{dp_pair._name}'] = dp_pair._data

            set_users.add(dp_pair._username)
            if new_policy is None:
                new_policy = Policy(dp_pair._policy)
            else:
                new_policy = new_policy.intersect(dp_pair._policy)

        new_dp = DataPolicyPair(policy=new_policy,
                                token=None,
                                name='Aggregate',
                                username='******',
                                private_data=dict(),
                                app_id=app_id)
        new_dp._data['aggregated'] = new_data
        if command.params.get('user_specific', False):
            user_specific_dict = command.params['user_specific']
            new_us = UserSecrets(policies=None,
                                 tokens=None,
                                 private_data=None,
                                 username='******',
                                 app_id=app_id)
            new_us._active_dps['aggregated'] = new_dp
            user_specific_dict['aggregated'] = new_us

        logger.info(f'aggregate function: {command}, app: {app_id}')
        new_dp._data = new_dp._call_transform(command)

        return new_dp
Ejemplo n.º 19
0
def check_is_func(func):
    if not callable(func):
        raise AncileException(
            "You can't call operation on the DataPair object. Use only allowed functions."
        )