Ejemplo n.º 1
0
def wizard_send_email():
    # add new request to database
    router_db = current_app.config['ROUTER_DB']
    r = IPRequest(session['hostname'], session['email'], session['router_id'])
    db.session.add(r)
    db.session.commit()

    # allocate mesh IPs
    router = router_db_get_entry(router_db, session['router_id'])
    ip_mesh_num = 2 if router['dualband'] else 1
    get_api().allocate_ips(current_app.config['API_POOL_MESH'], r.id, r.email,
        r.hostname, ip_mesh_num)

    # allocate HNA network
    get_api().allocate_ips(current_app.config['API_POOL_HNA'], r.id, r.email,
        r.hostname, prefix_len = session['prefix_len'])

    url = url_for(".wizard_activate", request_id=r.id,
                  signed_token=r.gen_signed_token(), _external=True)
    send_email(session['email'], r.hostname, router, url)

    for k in ('email', 'router_id'):
        del session[k]

    return render_template('waiting_for_confirmation.html')
Ejemplo n.º 2
0
def wizard_activate(request_id, signed_token):
    r = IPRequest.query.get(request_id)
    if not r.verified:
        if not r.verify_signed_token(signed_token, timeout = 3600):
            raise Exception("Invalid Token")

        get_api().activate_ips(r.id)
        r.verified = True

        db.session.add(r)
        db.session.commit()

    return redirect(url_for('.wizard_get_config', token = r.token))
Ejemplo n.º 3
0
def train_algorithm(request):
    module_id = request.GET.get('module_id')
    scene_id = request.GET.get('scene_id')
    limit  = request.GET.get('limit')

    if scene_id and module_id:
        tmp = utils.get_scene_record(module_id,scene_id)
        for i in tmp:
            i['data_length'] = range(len(i['data'][i['data'].keys()[0]]))
            i['resources'] = []
            i['apis'] = []
            i['api_info'] = []
            api_dict = {}
            for k in i['data'].keys():
                if k != 'total' and k.find('#api#') != 0:
                    i['resources'].append(k)
                if k != 'total' and k.find('#api#') == 0:
                    api_dict[k[5:]] = i['data'][k]
                    #this_api_id = utils.get_api_by_name(k[5:])
                    i['api_info'].append(k) # TODO

            for j in i['data_length']:
                current_api_dict = {}
                for k,v  in api_dict.iteritems():
                    current_api_dict[k] = v[j]
                i['apis'].append(current_api_dict)
        if limit and int(limit) > 0:
            ret = {'scene_records' : tmp[:int(limit)]}
        else:
            ret = {'scene_records': tmp}

        ret['module_id'] = module_id
        ret['scene_id'] = scene_id
        scene_api = utils.get_scene_api(module_id, scene_id)

        for s in scene_api:
            s['api_info'] = utils.get_api(s.get('api_id'))
            # ge threhold
            if s['api_info']:
                s['api_info']['threholds'] = utils.get_api_resource(s.get('api_id'))
                for th in s['api_info']['threholds'].get('resource_list'):
                    th['name'] = utils.get_resource(th.get('resource_id')).get('name')

        ret['scene_info'] = utils.get_scene(scene_id)
        ret['module_info'] = utils.get_module(module_id)
        ret['scene_api'] = scene_api
        ret['all_resource'] = []

        all_resource_ids = []
        # get all resource need
        for s in scene_api:
            for id in s.get('api_info').get('threholds').get('resource_id'):
                if not id in all_resource_ids:
                    all_resource_ids.append(id)
                    ret['all_resource'].append(utils.get_resource(id))

        ret["public"] = utils.get_public(request)
        return render(request, 'assess/train_algorithm.html', {'data': ret})
    else:
        return render(request, 'error.html')
Ejemplo n.º 4
0
def get_org_policies(resource, body={}):
    api = get_api('cloudresourcemanager').organizations()
    return policy_wrap(
        # api.getIamPolicy doesn't apper to have paging
        api.getIamPolicy(resource=resource, body=body).execute()['bindings'],
        resource,
        'organization')
Ejemplo n.º 5
0
def unauth(request):
    """
    logout and remove all session data
    """
    if check_key(request):
        api = get_api(request)
        request.session.clear()
        logout(request)
    return HttpResponseRedirect(reverse('main'))
Ejemplo n.º 6
0
def api_view(request):
    data = {}
    data["public"] = utils.get_public(request)
    data["resource"] = utils.get_resource()
    id = request.GET.get("id")
    if id:
        data["api"] = utils.get_api(id)
        data["resource_list"] = utils.get_api_resource(id)
    return render(request, 'assess/api.html', {"data":data})
Ejemplo n.º 7
0
def login_init():
    data = {}
    url = ''
    is_init = r.get('is_init')
    if request.method == 'POST':
        '''
        is_init = BooleanField(default=False)
        language = StringField(default='Chinese')
        normal_pin = IntField(default=9999)
        study_pin  = IntField(default=1234)
        api_key = StringField()
        server_address = StringField()
        media_server = StringField()
        q_a = StringField()
        
        '''
        if is_init == 'True':
            data = request.form['q_a']
            # add_obj = Seetings.objects.first()
            if str(data) == r.get('q_a'):
                r.set('is_init', '')
                flash(_('Certification successful, please reinitialize'))
            else:
                flash(_('Authentication failed'))
            return redirect('/login/init')

        else:
            data = request.form
            # #logging.info(str(data.to_dict()))
            data = data.to_dict()
            data['api_key'] = get_api()

            # logging.info(data)
            '''
            {'language': 'Chinese', 'server_address': 'http://127.0.0.1:5000', 'media_server': 'http://127.0.0.1:5000', 'normal_pin': '9999', 'study_pin': '1234', 'q_a': 'Alpha', 'api_key': 'e4b93eb4-0915-11ea-98cf-8c859072f2dc'}
            '''
            for key, value in data.items():
                r.set(key, value)
            with open('config/api_server.txt', 'w') as f:
                f.write(data['server_address'])
            r.set('is_init', 'True')
            flash(_('Parameter setup was successful'))
            return redirect('/login')

    else:
        url = request.base_url.replace('/login/init', '')

    templateData = {
        'title': 'Initial',
        'url': url,
        'is_init': is_init,
        # 'api_key': api_key,
        # 'pagenation_replace': utils.pagenation_replace
    }
    return render_template('login_init.html', **templateData)
Ejemplo n.º 8
0
def info(request):
    """
    display some user info to show we have authenticated successfully
    """
    if check_key(request):
        api = get_api(request)
        user = api.users(id='self')
        print dir(user)
        return render_to_response('djfoursquare/info.html', {'user': user})
    else:
        return HttpResponseRedirect(reverse('main'))
Ejemplo n.º 9
0
def invite_code():
    api_key = get_api()
    url = 'http://www.wulibobo.com/invite_code?api_key=' + api_key
    c = requests.get(url)
    code = c.text
    templateData = {
        'title': 'Invite code',
        'code': code,
        # 'api_key': api_key,
        # 'pagenation_replace': utils.pagenation_replace
    }
    return render_template('invite_code.html', **templateData)
Ejemplo n.º 10
0
def iter_uids(limit, ind_cred):
    API_BLOCKED_STATUS = 429
    API_BAD_REQUEST = 400
    NOT_FOUND_STATUS = 404
    gen_api = get_api(credentials[ind_cred])
    api = next(gen_api)

    def get_fwings(pk):
        try:
            return api.getTotalFollowings(pk)
        except:
            api.logout()
            api.login()
            return api.getTotalFollowings(pk)

    with DB('instadata') as idb:
        uids = idb.execute_get(
            'SELECT pk FROM uids where is_proceed=FALSE AND is_private=FALSE LIMIT %d OFFSET %d'
            % (limit, limit * ind_cred))
        print(uids)
        for i, urow in enumerate(uids):
            pk = urow[0]
            fwngs = get_fwings(pk)
            if not fwngs or len(fwngs) == 0:
                print("Followings Empty", pk)
                if api.LastResponse.status_code == NOT_FOUND_STATUS:
                    print('NOT_FOUND fwings %s' % pk)
                    continue
                while api.LastResponse.status_code == API_BLOCKED_STATUS or api.LastResponse.status_code == API_BAD_REQUEST:
                    api.logout()
                    if attempt > 2:
                        print("API stop working on pk=%s" % pk)
                        time.sleep(60)
                    api = next(gen_api)
                    print('Change instagram user')
                    attempt += 1
                    fwngs = get_fwings(pk)

            attempt = 0

            print('%d %s -> len=%d' % (i, pk, len(fwngs)))
            try:
                if fwngs:
                    fwngs_filt = filter_advert(fwngs)
                    if fwngs_filt:
                        proceed_followings(idb, fwngs_filt, pk)
                set_proceed(idb, pk)
            except:
                print('pk = ', pk)
                raise
Ejemplo n.º 11
0
def sync_admin_logs(app_name, resource_id, log_id):
    scope = ['https://www.googleapis.com/auth/admin.reports.audit.readonly']
    api = get_api('admin', 'reports_v1', scope).activities()
    req = api.list(
        applicationName=app_name,
        userKey='all',
        startTime = last_log_time(resource_id, log_id)
    )
    paging_batch(
        lambda batch: log_write(
            map(lambda i: convert_admin_log(i), batch),
            resource_id, log_id
        ),
        api, req
    )
Ejemplo n.º 12
0
def module_view(request):
    module_id = request.GET.get("module_id")
    data = {}
    data["module_id"] = module_id
    data["public"] = utils.get_public(request)
    scenes = utils.get_scene()
    data["scene"] = scenes
    for scene in scenes:
        scene_id = scene["id"]
        api_list = utils.get_scene_api(module_id, scene_id)
        scene["api_list"] = []
        for api in api_list:
            api["info"] = utils.get_api(api["api_id"])
            scene["api_list"].append(api)

    return render(request, 'assess/module.html', {"data":data})
Ejemplo n.º 13
0
def send_random_tweet():
    tweet = generate_random_tweet()
    with open(config.RECENT_DB) as f:
        recent = json.load(f)
    while tweet in recent:
        tweet = generate_random_tweet()

    api = utils.get_api()
    try:
        api.update_status(status=tweet)
    except:
        pass

    recent.append(tweet)
    with open(config.RECENT_DB, 'w') as f:
        json.dump(recent, f)
    return tweet
Ejemplo n.º 14
0
def init_personal_from_file(start_from):
    API_BLOCKED_STATUS = 429
    API_BAD_REQUEST = 400
    NOT_FOUND_STATUS = 404
    with open('inst_uids_init.csv', 'r') as f:
        lines = f.readlines()
    gen_api = get_api(credentials)
    api = next(gen_api)
    attempt = 0
    with DB('instadata') as idb:
        for i, pk, _, uname in (l.strip().split() for l in lines[start_from:]):
            if not pk.isdigit():
                print('%s FAIL %s %s' % (i, pk, uname))
                continue
            if not api.getUsernameInfo(pk):
                if api.LastResponse.status_code == NOT_FOUND_STATUS:
                    print('%s NOT_FOUND %s %s' % (i, pk, uname))
                    continue
                while api.LastResponse.status_code == API_BLOCKED_STATUS or api.LastResponse.status_code == API_BAD_REQUEST:
                    api.logout()
                    if attempt > 5:
                        print("API stop working on pk=%s uname=%s (%s)" %
                              (pk, attempt, uname))
                        time.sleep(60)
                    api = next(gen_api)
                    print('Change instagram user')
                    attempt += 1
                    api.getUsernameInfo(pk)

            attemp = 0

            fname = api.LastJson['user'].get('full_name', '').strip()
            fname = fname if fname else None

            print('%s %s' % (i, pk))

            idb.execute("INSERT INTO personal (pk, uname, fname) VALUES " +
                        idb.cur.mogrify("(%s, %s, %s)", (pk, uname,
                                                         fname)).decode() +
                        "ON CONFLICT DO NOTHING")
            time.sleep(0.2)
Ejemplo n.º 15
0
def fetch_tweets():
    api = utils.get_api()

    statuses = api.home_timeline(count=150, since_id=latest_tweet_id())

    db_connection = sqlite3.connect(config.SQLITE_DB)
    db_cursor = db_connection.cursor()

    for status in filter(utils.is_clean, statuses):
        print(status.author.screen_name)
        print(status.created_at)
        print()
        db_cursor.execute('INSERT INTO tweets VALUES (?,?,?,?)', (
            status.author.screen_name,
            status.text,
            status.id,
            status.created_at.timestamp(),
        ))

    db_connection.commit()
    db_connection.close()
Ejemplo n.º 16
0
def log_network(project, resource, log_id):
    try:
        zones = get_zones(project)
    except HttpError as e:
        code, reason = error_info(e)
        # skip projects for which compute API is not enabled
        if reason == 'accessNotConfigured': return
        else: raise
    rules = get_fw_rules(project)
    routes = get_routes(project)
    api = get_api('compute').instances()
    for zone in zones:
        req = api.list(project=project, zone=zone)
        paging_batch(
            lambda batch: log_write(
                map(
                    lambda i: to_network_entry(
                        i, filter(lambda r: rule_applies(i, r), rules),
                        filter(
                            lambda r: instance_ip_in_range(
                                i, IpRangeList(r['destRange'])), routes)),
                    batch), resource, log_id), api, req)
        break
Ejemplo n.º 17
0
def fetch_tweets():
    api = utils.get_api()

    statuses = api.home_timeline(count=150, since_id=latest_tweet_id())

    db_connection = sqlite3.connect(config.SQLITE_DB)
    db_cursor = db_connection.cursor()

    for status in filter(utils.is_clean, statuses):
        print(status.author.screen_name)
        print(status.created_at)
        print()
        db_cursor.execute(
            'INSERT INTO tweets VALUES (?,?,?,?)',
            (
                status.author.screen_name,
                status.text,
                status.id,
                status.created_at.timestamp(),
            )
        )

    db_connection.commit()
    db_connection.close()
Ejemplo n.º 18
0
    return ''.join(convert_valid(one_char) for one_char in fname)


def convert_valid(one_char):
    """Convert a character into '_' if invalid.
    Arguments:
        one_char -- the char to convert
    Return:
        Character -- converted char
    """
    valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits)
    if one_char in valid_chars:
        return one_char
    else:
        return '_'

@classmethod
def parse(cls, api, raw):
    status = cls.first_parse(api, raw)
    setattr(status, 'json', json.dumps(raw))
    return status

if __name__ == '__main__':
    # Arguments
    parser = get_parser()
    args = parser.parse_args()
    # Twitter's API and Auth
    api, auth = get_api()
    # Streaming tweets
    twitter_stream = Stream(auth, MyListener(args.query))
    twitter_stream.filter(track=[args.query])
Ejemplo n.º 19
0
def actualize_api():
    # create file and fill with api
    with open("api", "w+") as f:
        data = json.dumps(get_api())
        f.write(data)
Ejemplo n.º 20
0
def fetch_tweets():
    api = utils.get_api()

    timeline_listener = TimelineListener()
    timeline_stream = tweepy.Stream(auth=api.auth, listener=timeline_listener)
    timeline_stream.userstream()
Ejemplo n.º 21
0
def get_project_policies(resource, body={}):
    api = get_api('cloudresourcemanager').projects()
    return policy_wrap(
        pagingc(api,
                api.getIamPolicy(resource=resource, body=body),
                item_key='bindings'), resource, 'project')
Ejemplo n.º 22
0
def doit(ctx, email, config_path, fresh, debug):
    if debug:
        logging.basicConfig(level=logging.INFO)
    api = utils.get_api(email, config_path, fresh)
    ctx.obj['api'] = api
Ejemplo n.º 23
0
def get_users(domain):
    api = get_api('admin', 'directory_v1', USER_SCOPES).users()
    return pagingc(api, api.list(domain=domain), item_key='users')
Ejemplo n.º 24
0
def get_members(group):
    api = get_api('admin', 'directory_v1', USER_SCOPES).members()
    return pagingc(api, api.list(groupKey=group), item_key='members')
Ejemplo n.º 25
0
def get_folder_policies(resource, body={}):
    api = get_api('cloudresourcemanager', 'v2').folders()
    return policy_wrap(
        pagingc(api,
                api.getIamPolicy(resource=resource, body=body),
                item_key='bindings'), resource, 'folder')
Ejemplo n.º 26
0
        dct = self.call_api(url)
        obj = resource.model_class.get_instance(_atts=dct)

        return obj


if __name__ == '__main__':
    import sys
    sys.path.append('.')
    from utils import get_api

    import logging
    from logutils import get_logger

    if '-v' in sys.argv:
        logger = get_logger(__file__)
        logger.setLevel(logging.DEBUG)

    api = get_api()
    t1 = api.get_tenant(name="supertenant")
    print t1
    g1 = api.get_group(tenant=t1, name="admins")
    #print api.all_user(tenant=t1)
    #u1.description = u'a'
    #api.update_user(u1)
    #print api.create_user(initial={'name': 'h', 'surname': 'a', 'username': '******', 'tenant': t1})

    data = api.get_user_privs(g1)
    api.put_user_privs(g1, data=data)
    print data
Ejemplo n.º 27
0
def go(recipient):
    monzo = get_api()
    current_account = get_current_account(monzo)
    todays_transactions = get_todays_transactions(monzo, current_account)
    change = change_to_save(todays_transactions)
    send_sms(change, recipient)
Ejemplo n.º 28
0
        # {u'body': u'140\u0440', u'uid': 19255756, u'mid': 877596, u'date': 1448724424,
        # u'out': 0, u'read_state': 1, u'from_id': 19255756}
        i = 0
        total_messages = len(messages)
        while i < total_messages:
            user_id = messages[i]['from_id']
            f.write(str(user_id) + ':\n')

            j = i
            while j < total_messages and messages[j]['from_id'] == user_id:
                f.write(messages[j]['body'] + '\n')
                j += 1
            i = j
            f.write('\n')


if __name__ == '__main__':
    args = parser.parse_args()

    # Reading credentials and authenticating
    login = str(raw_input("VK Login: "******"Password: "******"Authenticated"

    # Downloading all messages in dialog
    messages = get_messages_with(api, args.user_id)

    print "Writing messages to file '{0}'".format(args.out)
    write_messages(messages, args.out)
Ejemplo n.º 29
0
def train_algorithm(request):
    module_id = request.GET.get('module_id')
    scene_id = request.GET.get('scene_id')
    limit = request.GET.get('limit')

    if scene_id and module_id:
        tmp = utils.get_scene_record(module_id, scene_id)
        for i in tmp:
            i['data_length'] = range(len(i['data'][i['data'].keys()[0]]))
            i['resources'] = []
            i['apis'] = []
            i['api_info'] = []
            api_dict = {}
            for k in i['data'].keys():
                if k != 'total' and k.find('#api#') != 0:
                    i['resources'].append(k)
                if k != 'total' and k.find('#api#') == 0:
                    api_dict[k[5:]] = i['data'][k]
                    #this_api_id = utils.get_api_by_name(k[5:])
                    i['api_info'].append(k)  # TODO

            for j in i['data_length']:
                current_api_dict = {}
                for k, v in api_dict.iteritems():
                    current_api_dict[k] = v[j]
                i['apis'].append(current_api_dict)
        if limit and int(limit) > 0:
            ret = {'scene_records': tmp[:int(limit)]}
        else:
            ret = {'scene_records': tmp}

        ret['module_id'] = module_id
        ret['scene_id'] = scene_id
        scene_api = utils.get_scene_api(module_id, scene_id)

        for s in scene_api:
            s['api_info'] = utils.get_api(s.get('api_id'))
            # ge threhold
            if s['api_info']:
                s['api_info']['threholds'] = utils.get_api_resource(
                    s.get('api_id'))
                for th in s['api_info']['threholds'].get('resource_list'):
                    th['name'] = utils.get_resource(
                        th.get('resource_id')).get('name')

        ret['scene_info'] = utils.get_scene(scene_id)
        ret['module_info'] = utils.get_module(module_id)
        ret['scene_api'] = scene_api
        ret['all_resource'] = []

        all_resource_ids = []
        # get all resource need
        for s in scene_api:
            for id in s.get('api_info').get('threholds').get('resource_id'):
                if not id in all_resource_ids:
                    all_resource_ids.append(id)
                    ret['all_resource'].append(utils.get_resource(id))

        ret["public"] = utils.get_public(request)
        return render(request, 'assess/train_algorithm.html', {'data': ret})
    else:
        return render(request, 'error.html')
Ejemplo n.º 30
0
def test_get_api():
    assert isinstance(get_api(), MonzoAPI)
Ejemplo n.º 31
0
def get_zones(project):
    api = get_api('compute').zones()
    return map(lambda z: z['name'], pagingc(api, api.list(project=project)))
Ejemplo n.º 32
0
def get_routes(project):
    api = get_api('compute').routes()
    return pagingc(api, api.list(project=project))
Ejemplo n.º 33
0
from vk import types
from vk.bot_framework.dispatcher import Blueprint
from neuron import send_and_gen_sentence
from utils import get_api

bp = Blueprint()

api = get_api()


@bp.message_handler(commands=["g", "gen", "generate"])
async def generate(message: types.Message, _):
    await send_and_gen_sentence(f"dialogs/dialogs{message.peer_id}.txt",
                                message.peer_id)
Ejemplo n.º 34
0
def get_fw_rules(project):
    api = get_api('compute').firewalls()
    return pagingc(api, api.list(project=project))
Ejemplo n.º 35
0
def get_api_view(request):
    id = request.GET.get("id")
    ret = utils.get_api(id)
    return public.success_result_http(ret)