async def get_friends(self): ids = await self._epic_client.get_friends_list() account_ids = [] friends = [] prev_slice = 0 for index, entry in enumerate(ids): account_ids.append(entry["accountId"]) ''' Send request for friends information in batches of 50 so the request isn't too large, 50 is an arbitrary number, to be tailored if need be ''' if index + 1 % 50 == 0 or index == len(ids) - 1: friends.extend(await self._epic_client.get_users_info( account_ids[prev_slice:])) prev_slice = index friend_infos = [] for friend in friends: if "id" in friend and "displayName" in friend: friend_infos.append( FriendInfo(user_id=friend["id"], user_name=friend["displayName"])) elif "id" in friend: friend_infos.append( FriendInfo(user_id=friend["id"], user_name="")) return friend_infos
async def test_get_friends_success(plugin, read, write): request = { "jsonrpc": "2.0", "id": "3", "method": "import_friends" } read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)] plugin.get_friends.return_value = async_return_value([ FriendInfo("3", "Jan"), FriendInfo("5", "Ola") ]) await plugin.run() plugin.get_friends.assert_called_with() assert get_messages(write) == [ { "jsonrpc": "2.0", "id": "3", "result": { "friend_info_list": [ {"user_id": "3", "user_name": "Jan"}, {"user_id": "5", "user_name": "Ola"} ] } } ]
async def test_multiple_friends(authenticated_plugin, backend_client, steam_id): backend_client.get_friends.return_value = { "76561198040630463": "crak", "76561198053830887": "Danpire" } result = await authenticated_plugin.get_friends() assert result == [ FriendInfo("76561198040630463", "crak"), FriendInfo("76561198053830887", "Danpire") ] backend_client.get_friends.assert_called_once_with(steam_id)
async def get_friends(self): friends = await self.client.get_friends() return [ FriendInfo(user_id=friend["pid"], user_name=friend["nameOnPlatform"]) for friend in friends["friends"] ]
async def get_friends(self): self._check_authenticated() return [ FriendInfo(user_id=str(user_id), user_name=str(user_name)) for user_id, user_name in ( await self._backend_client.get_friends(self._user_id)).items() ]
async def get_friends(self): if not self.authentication_client.is_authenticated(): raise AuthenticationRequired() friends_list = await self.social_features.get_friends() return [ FriendInfo(user_id=friend.id.low, user_name='') for friend in friends_list ]
async def get_friends(self): if self._steam_id is None: raise AuthenticationRequired() return [ FriendInfo(user_id=user_id, user_name=user_name) for user_id, user_name in (await self._client.get_friends(self._steam_id)).items() ]
async def get_friends(self): if not self._http_client.is_authenticated(): raise AuthenticationRequired() return [ FriendInfo(user_id=str(user_id), user_name=str(user_name)) for user_id, user_name in ( await self._backend_client.get_friends(self._pid)).items() ]
async def test_add_friend(plugin, write): friend = FriendInfo("7", "Kuba") plugin.add_friend(friend) assert get_messages(write) == [ { "jsonrpc": "2.0", "method": "friend_added", "params": { "friend_info": {"user_id": "7", "user_name": "Kuba"} } } ]
async def get_friends(self): # This method is currently causing the plugin to crash after its second call. I am unsure of why this is # happening, but it needs to be fixed. # NOTE: This will return a list of type FriendInfo. # The Social Club website returns a list of the current user's friends through the url # https://scapi.rockstargames.com/friends/getFriendsFiltered?onlineService=sc&nickname=&pageIndex=0&pageSize=30. # The nickname URL parameter is left blank because the website instead uses the bearer token to get the correct # information. The last two parameters are of great importance, however. The parameter pageSize determines the # number of friends given on that page's list, while pageIndex keeps track of the page that the information is # on. The maximum number for pageSize is 30, so that is what we will use to cut down the number of HTTP # requests. # We first need to get the number of friends. url = ( "https://scapi.rockstargames.com/friends/getFriendsFiltered?onlineService=sc&nickname=&" "pageIndex=0&pageSize=30") current_page = await self._http_client.get_json_from_request_strict(url ) log.debug("ROCKSTAR_FRIENDS_REQUEST: " + str(current_page)) num_friends = current_page['rockstarAccountList']['totalFriends'] num_pages_required = num_friends / 30 if num_friends % 30 != 0 else ( num_friends / 30) - 1 # Now, we need to get the information about the friends. friends_list = current_page['rockstarAccountList']['rockstarAccounts'] return_list = [] for i in range(0, len(friends_list)): friend = FriendInfo(friends_list[i]['rockstarId'], friends_list[i]['displayName']) return_list.append(FriendInfo) for cached_friend in self.friends_cache: if cached_friend.user_id == friend.user_id: break else: self.friends_cache.append(friend) self.add_friend(friend) log.debug("ROCKSTAR_FRIEND: Found " + friend.user_name + " (Rockstar ID: " + str(friend.user_id) + ")") # The first page is finished, but now we need to work on any remaining pages. if num_pages_required > 0: for i in range(1, int(num_pages_required + 1)): url = ( "https://scapi.rockstargames.com/friends/getFriendsFiltered?onlineService=sc&nickname=&" "pageIndex=" + str(i) + "&pageSize=30") return_list.append(friend for friend in await self._get_friends(url)) return return_list
def test_get_friends_success(plugin, read, write): request = {"jsonrpc": "2.0", "id": "3", "method": "import_friends"} read.side_effect = [json.dumps(request).encode() + b"\n", b""] plugin.get_friends.coro.return_value = [ FriendInfo("3", "Jan"), FriendInfo("5", "Ola") ] asyncio.run(plugin.run()) plugin.get_friends.assert_called_with() response = json.loads(write.call_args[0][0]) assert response == { "jsonrpc": "2.0", "id": "3", "result": { "friend_info_list": [{ "user_id": "3", "user_name": "Jan" }, { "user_id": "5", "user_name": "Ola" }] } }
async def _get_friends(self, url): current_page = await self._http_client.get_json_from_request_strict(url) friends_list = current_page['rockstarAccountList']['rockstarAccounts'] return_list = [] for i in range(0, len(friends_list)): friend = FriendInfo(friends_list[i]['rockstarId'], friends_list[i]['displayName']) return_list.append(FriendInfo) for cached_friend in self.friends_cache: if cached_friend.user_id == friend.user_id: break else: # An else-statement occurs after a for-statement if the latter finishes WITHOUT breaking. self.friends_cache.append(friend) self.add_friend(friend) log.debug("ROCKSTAR_FRIEND: Found " + friend.user_name + " (Rockstar ID: " + str(friend.user_id) + ")") return return_list
async def get_friends(self): friends = list() xmpp_client = self.__xmpp_get_client() while len(xmpp_client.client_roster) == 0: try: await asyncio.sleep(1) except asyncio.CancelledError: break for jid in xmpp_client.client_roster: userid = jid.split('@', 1)[0] if userid != str(self._wgc.account_id()): username = '******' % (self._wgc.account_realm(), xmpp_client.client_roster[jid]['name']) friends.append(FriendInfo(userid, username)) return friends
def test_add_friend(plugin, write): friend = FriendInfo("7", "Kuba") async def couritine(): plugin.add_friend(friend) asyncio.run(couritine()) response = json.loads(write.call_args[0][0]) assert response == { "jsonrpc": "2.0", "method": "friend_added", "params": { "friend_info": { "user_id": "7", "user_name": "Kuba" } } }
async def get_friends(ws: websocket.WebSocket): log.debug( "DISCORD_SCRAPE_FRIENDS: Scraping the user's friends from the Discord client..." ) await open_friends_page(ws) msg = create_ws_json("DOM.getDocument") ws.send(msg) root_node = ws.recv() root_node_id = json.loads(root_node)['result']['root']['nodeId'] msg = create_ws_json( "DOM.querySelectorAll", rf''' {{ "nodeId": {root_node_id}, "selector": "div[class^='friendsRow']" }} ''') ws.send(msg) while True: resp = ws.recv() if 'result' in json.loads(resp): friend_node_ids = json.loads(resp)['result']['nodeIds'] break friends = [] for friend_node_id in friend_node_ids: msg = create_ws_json( "DOM.querySelector", rf''' {{ "nodeId": {friend_node_id}, "selector": "span[class^='username-']" }} ''') ws.send(msg) while True: resp = ws.recv() if 'result' in json.loads(resp): username_node_id = json.loads(resp)['result']['nodeId'] break msg = create_ws_json( "DOM.getOuterHTML", rf''' {{ "nodeId": {username_node_id} }} ''') ws.send(msg) while True: resp = ws.recv() if 'result' in json.loads(resp): username = json.loads(resp)['result']['outerHTML'] break username = re.search(r'<span class=".+">(.+)</span>', str(username))[1] msg = create_ws_json( "DOM.querySelector", rf''' {{ "nodeId": {friend_node_id}, "selector": "span[class^='discriminator-']" }} ''') ws.send(msg) while True: resp = ws.recv() if 'result' in json.loads(resp): discriminator_node_id = json.loads(resp)['result']['nodeId'] break msg = create_ws_json( "DOM.getOuterHTML", rf''' {{ "nodeId": {discriminator_node_id} }} ''') ws.send(msg) while True: resp = ws.recv() if 'result' in json.loads(resp): discriminator = json.loads(resp)['result']['outerHTML'] break discriminator = re.search(r'<span class=".+">#(.+)</span>', str(discriminator))[1] if LOG_SENSITIVE_DATA: log.debug( f"DISCORD_FRIEND: Found {username} (Discriminator: {discriminator})" ) else: log.debug( f"DISCORD_FRIEND: Found {username[:1]}*** (Discriminator: ***)" ) friends.append(FriendInfo(f"{username}#{discriminator}", username)) log.debug( "DISCORD_SCRAPE_FRIENDS_FINISHED: The user's list of friends was successfully found from the Discord " "client!") return friends
from backend import OriginBackendClient from galaxy.api.types import FriendInfo from galaxy.api.errors import AuthenticationRequired import pytest FRIEND_LIST = [ FriendInfo("1003118773678", "crak"), FriendInfo("1008880909879", "Danpire") ] PARSED_FRIEND_LIST_RESPONSE = { "1003118773678": "crak", "1008880909879": "Danpire" } BACKEND_FRIENDS_RESPONSE = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <users> <user> <userId>1003118773678</userId> <personaId>1781965055</personaId> <EAID>crak</EAID> </user> <user> <userId>1008880909879</userId> <personaId>1004303509879</personaId> <EAID>Danpire</EAID> </user> </users> """
def friend_info_parser(profile): return FriendInfo( user_id=str(profile["accountId"]), user_name=str(profile["onlineId"]) )
def result_friends(): data = json.loads('''{"jsonrpc": "2.0", "id": "1", "result": {"friend_info_list": [{"user_id": "420", "user_name": "mocker1"}, {"user_id": "123", "user_name": "mocker2"}, {"user_id": "321", "user_name": "mocker3"}]}}''') return [ FriendInfo(user_id=friend["user_id"], user_name=friend["user_name"]) for friend in data["result"]["friend_info_list"] ]