summaryrefslogtreecommitdiff
path: root/fortniteTest.py
blob: a00283f290a898afd404df94cc5454cec08e0637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3

import fortnitepy
import json
import os

email = 'dmitry-morozov.rjkv73@yandex.ru'
password = 'Dimka2407985$'
filename = 'device_auths.json'

class MyClient(fortnitepy.Client):
    def __init__(self):
        if self._device_auth_file_exists():
            device_auth_details = self.get_device_auth_details().get(email, {})
            super().__init__(
                auth=fortnitepy.DeviceAuth(
                    **device_auth_details
                )
            )
        else:
            code = input("Enter authorization code (https://www.epicgames.com/id/api/redirect?clientId=3446cd72694c4a4485d81b77adbb2141&responseType=code):") 
            print(code) 
            super().__init__(
                auth=fortnitepy.AuthorizationCodeAuth(
                    code = code
                )
            )
    
    def _device_auth_file_exists(self):
        return os.path.isfile(filename)

    def get_device_auth_details(self):
        if os.path.isfile(filename):
            with open(filename, 'r') as fp:
                return json.load(fp)
        return {}

    def store_device_auth_details(self, email, details):
        existing = self.get_device_auth_details()
        existing[email] = details

        with open(filename, 'w') as fp:
            json.dump(existing, fp)

    async def event_device_auth_generate(self, details, email):
        self.store_device_auth_details(email, details)

    async def event_ready(self):
        print('----------------')
        print('Client ready as')
        print(self.user.display_name)
        print(self.user.id)
        print('----------------')

        if not self._device_auth_file_exists():
            device_auth_data = await self.auth.generate_device_auth()
            details = {
                'device_id': device_auth_data['deviceId'],
                'account_id': device_auth_data['accountId'],
                'secret': device_auth_data['secret'],
            }
            self.auth.__dict__.update(details)
            self.dispatch_event(
                'device_auth_generate',
                details,
                email
            )

        for incomingPedingFriend in self.incoming_pending_friends:
            print('Accepting friend request')
            await incomingPedingFriend.accept()

    async def event_party_invite(self, invitation: fortnitepy.ReceivedPartyInvitation):
        clientParty = await invitation.accept()
        for partyMember in clientParty.members:
            if not self.get_friend(partyMember.id) and self.user.id != partyMember.id:
                print('Adding {} as friend'.format(partyMember.display_name))
                await partyMember.add()
    
    async def event_friend_request(self, request: fortnitepy.friend.IncomingPendingFriend):
        print('Accepting friend request')
        await request.accept()

    async def event_friend_presence(self, before, after: fortnitepy.Presence):
        if before is not None and after is not None:
            print('event_friend_presence for user {}'.format(after.friend.display_name))
            print("Before available {}, joinable {}, playing {}, lfg {}".format(before.available, before.joinable, before.playing, before.lfg))
            print("After available {}, joinable {}, playing {}, lfg {}".format(after.available, after.joinable, after.playing, after.lfg))

client = MyClient()
client.run()