def get_group_users(group): users = [] new_users = call_api('groups.getMembers', group_id=group)['users'] offset = 1000 while len(new_users) > 0: users = users + new_users new_users = call_api('groups.getMembers', group_id=group, offset=offset)['users'] offset += 1000 return users
def __init__(self): global builder, window if api.call_api("execute.auth", [], db.getOne('token')) == True: self.token, self.user_id = db.getOne('token'), db.getOne('user_id') else: self.gui() self.token, self.user_id = db.getOne('token'), db.getOne('user_id') if api.call_api("execute.auth", [], self.token) != True: self.__init__()
def test_call_api(self): with mock.patch('api.requests.get') as mocked_get: mocked_get.return_value.ok = True mocked_get.return_value.text = 'Success' response = call_api('http://www.dnd5eapi.co/api/equipment/') # mocked_get.assert_called_with('http://www.dnd5eapi.co/api/equipment/') self.assertEqual(response, 'Success')
def test_item_info(self): url = api.get_item_url('Club', self.list_of_dics) item_info = api.get_api_all(api.call_api(url)) item_id = api.get_nested_api_dict(item_info, '_id') item_cost = api.get_nested_api_dict(item_info, 'cost') self.assertEqual(item_id, '5bce91275b7768e792017da3') self.assertEqual(item_cost['quantity'], 1) self.assertEqual(item_cost['unit'], 'sp')
def stock_stores(conn, some_bar, window, some_label, some_queue): # TODO: This is a seam canceled = False count = 0 time_to_install = time.time() store_dict = stores.stores() # {'some_store':[1,2,3,4,5]} Used to tell which items in which stores - ints are ids url = api.construct_api_url('equipment') s = api.create_session() response = api.call_api(url, s) response_dict = api.get_api_all(response) usable_dict = api.get_nested_api_dict(response_dict, 'results') # [{'name': 'some_name', 'url': 'some_url'}] # TODO: This is a seam for dic in usable_dict: temp = {} # TODO: Make sure to do with conn: around phases instead of each sql statement. Will require need execute_sql. for key, value in dic.items(): # TODO: First thing will always be to slice ID off url via regex and add to dictionary as 'index' # TODO: Then add item name, url and index to temp dictionary for DB insertion if not temp: # If temp dictionary is empty temp['item'] = value # add value with key 'item' else: temp['api'] = value # add value with key 'api' # TODO: This will be a pool of API calls, post phase1 DB insertion item_value = api.get_item_value(temp['item'], character.Character.list_of_item_dicts, s) temp['currency'] = item_value # TODO: This should be done prior to phase1 DB insertion. if value[0:37] == url: # if one of those values beings with a url like string num = api.regex(value, 'equipment/') # slices number off url and captures as variable # This logic compares the captured number to the numbers in the dict imported earlier # Then it adds a 'store':'some_store' key:value to the temp dictionary if num in store_dict['GS']: temp['store'] = 'General Store' elif num in store_dict['BS']: temp['store'] = 'Blacksmith' elif num in store_dict['Ship']: temp['store'] = 'Shipyard' elif num in store_dict['Stables']: temp['store'] = 'Stables' else: temp['store'] = 'No Store' # TODO: This will be phase2 DB insertion. Inserting the item values where index # TODO: add_store_item will need to be reworked, and, most likely, split into multiple functions # adds an item to a store table based on information stored in dictionary database.add_store_item(conn, sql.add_store_item(), temp) count += 1 print(count) # TODO: Change this to a Daemon thread to avoid having to do this cancel workaround if not some_queue.empty(): canceled = some_queue.get() update_mainloop(some_bar, count, some_label, window, canceled) print('done in: ', time.time() - time_to_install) print('Done with everything.')
def test_main_dictionaries(self): equip_count = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('equipment'))), 'count') spells_count = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('spells'))), 'count') classes_count = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('classes'))), 'count') features_count = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('features'))), 'count') monsters_count = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('monsters'))), 'count') self.assertEqual(equip_count, 256) self.assertEqual(spells_count, 319) self.assertEqual(classes_count, 12) self.assertEqual(features_count, 414) self.assertEqual(monsters_count, 325)
def buy_sell(self, item, action=None, conn=None): url = api.get_item_url(item, Character.list_of_item_dicts) item_info = api.get_api_all(api.call_api(url)) item_cost = api.get_nested_api_dict(item_info, 'cost') item_value = api.convert_price_info(item_cost) if action == 'buy': if item_value > self.currency: return False else: print('---item bought---') # TODO remove later self.currency -= item_value self.update_currency_db(conn) return True elif action == 'sell': print('---item sold---') # TODO remove later self.currency += item_value self.update_currency_db(conn)
def retrieve(task): if MOCK_DATA_HEAP.get(task): return MOCK_DATA_HEAP[task].pop(0) try: constraints = request.get_json(silent=True) constraints = convert_comparators(constraints) except Exception as e: raise InvalidUsage("Bad constraints string") try: item, num_other_items = api.call_api( task, constraints=constraints, ) return jsonify({ "Message": "Ok", "Item": item, "NumOtherItems": num_other_items }) except Exception as e: raise InvalidUsage("API code failed")
users = [] new_users = call_api('groups.getMembers', group_id=group)['users'] offset = 1000 while len(new_users) > 0: users = users + new_users new_users = call_api('groups.getMembers', group_id=group, offset=offset)['users'] offset += 1000 return users users = get_group_users('tinthelp') print len(users) user_info = [] ch = 300 chunks = [users[x*ch:(x+1)*ch] for x in xrange(len(users) / ch)] + [users[ch * (len(users) / ch):]] for chunk in chunks: chunk_info = call_api('users.get', user_ids=','.join(map(str, chunk)), fields='sex') user_info += chunk_info girls = [] for user in user_info: if user['sex'] is 1: girls.append(user['uid']) with open('girls.txt', 'w') as out: for girl in girls: out.write(str(girl) + '\n') pickle.dump(girls, open('girls', 'w'))
def eigenvec(A): ans = call_api('eigenvectors of %s' % repr(A), "Corresponding eigenvalues") res = trans(Matrix([list(eval(val)) for val in ans[0]], A.steps)) res.eigenval = [float(val) for val in ans[1]] res.steps.append('Eigenvectors of ' + str(A) + ' = ' + str(res)) return res
def eigenval(A): ans = call_api('eigenvalues of %s' % str(A))[0] return result([[float(k) for k in ans]], r'Eigenvalues of ' + str(A) + ' are = %s', A)
class Character: # TODO: unit_integration_test: FIXTURE REQUIRED # Main equipment dictionary returned from DnD 5e API # [{'name': 'some name', 'url': 'some url'}, {'name': 'some name', 'url': 'some url'}] list_of_item_dicts = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('equipment'))), 'results') def __init__(self, char_id, name, currency, inventories_list): self.id = char_id self.currency = currency # All currency held in cp. cp is then formatted via convert_currency() before view. self.name = name self.inventories = inventories_list # List of player inventory names in string format. Not currently used. # TODO: Not tested # TODO: This makes more sense as an inventory method. Change it once multiple inventories. # Adds an item under character id. Can specify a specific inventory via inv_id def add_item_db(self, conn, item, acc_id, inv_id): url = sql.execute_fetchone_sql(conn, sql.query_store_item_url(), item) item_value = sql.execute_fetchone_sql(conn, sql.query_store_item_value(), item) item_info = { 'acc_id': acc_id, 'char_id': self.id, 'inv_id': inv_id, 'item': item, 'api': url[0], 'value': item_value[0], 'quantity': 1 } database.add_item_row(conn, sql.add_item_row(), item_info) # TODO: Test # Updates currency column in DB based on character ID and currency. def update_currency_db(self, conn): sql.execute_sql(conn, sql.update_currency(), self.currency, self.id) # TODO: action= and conn= aren't exactly optional and should not be treated as such. # Gets item price info and adds/subtracts it to/from currency based on [unit] key. # See convert_price_info() in api.py for more information on conversion. # Also updates currency in DB via self.update_currency_db # Returns True if item is affordable. Else returns False. def buy_sell(self, item, action=None, conn=None): url = api.get_item_url(item, Character.list_of_item_dicts) item_info = api.get_api_all(api.call_api(url)) item_cost = api.get_nested_api_dict(item_info, 'cost') item_value = api.convert_price_info(item_cost) if action == 'buy': if item_value > self.currency: return False else: print('---item bought---') # TODO remove later self.currency -= item_value self.update_currency_db(conn) return True elif action == 'sell': print('---item sold---') # TODO remove later self.currency += item_value self.update_currency_db(conn)
def setUp(self): self.list_of_dics = api.get_nested_api_dict( api.get_api_all(api.call_api(api.construct_api_url('equipment'))), 'results')
from api import call_api import pickle, time import warnings warnings.filterwarnings("ignore") girls = pickle.load(open('girls', 'r')) print len(girls) #girls=[46849] photos={} for i, girl in enumerate(girls): print str(i) + '\r', #albums = call_api('photos.getAlbums', owner_id=girl) albums=['profile'] photos[girl]=[] for album in albums: album_photos = call_api('photos.get', owner_id=girl, album_id=album) #time.sleep(.1) if album_photos: for photo in album_photos: try: photos[girl].append(photo['src_xbig']) except: pass print len(photos) pickle.dump(photos, open('photos', 'w'))
import api if __name__ == "__main__": print() print("Search for a trivia question") item, num_other_items = api.call_api( "trivia", constraints=[{ "QuestionNum": 2 }], ) print(item) print(num_other_items) print() print("Search for an apartment") item, num_other_items = api.call_api( "apartment_search", constraints=[{ "HasBalcony": True }, { "NumRooms": api.is_greater_than(2) }, { "NumRooms": api.is_less_than(5) }], ) print(item) print(num_other_items)