File size: 2,753 Bytes
7e4b742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
import browser_cookie3
import json

def getMsToken():
    cookie_keys = ["msToken"]
    json_cookies = get_tiktok_cookies(cookie_keys)
    if json_cookies["found"]:
        ms_token = json_cookies["cookies"]["msToken"]
        #print(ms_token)
    else:
        raise Exception("Missing cookie msToken. Login to your tiktok account and retry")
    saveMsToken(ms_token)
    return ms_token

def saveMsToken(ms_token):
    with open("Data/TXT/Data/ms_token.txt", "w") as f:
        f.write(ms_token)
def readOldMsToken():
    with open("Data/TXT/Data/ms_token.txt", "r") as f:
        ms_token = f.read()
    return ms_token

def get_tiktok_cookies(cookie_keys):
    # Try to get cookie from browser
    ref = ["chromium", "opera", "edge", "firefox", "chrome", "brave"]
    index = 0
    json_cookie = {}
    found = False
    for cookie_fn in [
        
        browser_cookie3.firefox,
        browser_cookie3.chrome,
        browser_cookie3.brave,
    ]:
        try:
            for cookie in cookie_fn(domain_name="tiktok.com"):
                
                if ('tiktok.com' in cookie.domain):

                    # print(f"COOKIE - {ref[index]}: {cookie}")
                    if (cookie.name in cookie_keys):
                        json_cookie['browser'] = ref[index]
                        json_cookie[cookie.name] = cookie.value
                        json_cookie[cookie.name + '_expires'] = cookie.expires

                # Check
                found = True
                for key in cookie_keys:
                    if (json_cookie.get(key, "") == ""):
                        found = False
                        break

        except Exception as e:
            print(e)

        index += 1

        if (found):
            break
    #print("found " + str(found))
    return {"found": found, "cookies": json_cookie}



def get_tiktok_cookies_from_file(filepath: str):
    msToken = ""
    cookies = {}
    with open(filepath, "r") as f:
        cookies = f.read()
    
    cookies = json.loads(cookies)
    
    for cookie in cookies:
        cookie: dict
        if cookie.get("name", "") == "msToken" and cookie.get("domain", "") == ".tiktok.com":
            msToken = cookie.get("value", "")
            break
    
        
    if msToken is None:
        raise Exception("Missing cookie msToken. Login to your tiktok account and retry")
    saveMsToken(msToken)
    return msToken
    
def getCookiesFromFile(filepath: str):
    cookies = {}
    with open(filepath, "r") as f:
        cookies = f.read()

    cookies = json.loads(cookies)
    return cookies
if __name__ == "__main__":
    print(get_tiktok_cookies_from_file())