File size: 4,114 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import asyncio
from TikTok.Statistic.tiktok import tiktokUserCountVideoViews, SameMsTokenException
from TikTok.Statistic.SingleUser import users_videos_with_hashtag
from TikTok.Cookies.cookie import getMsToken, readOldMsToken, saveMsToken, get_tiktok_cookies_from_file,getCookiesFromFile
from TikTok.Server.users import get_user_list
import time
import os
import json

def getNewMsToken():
    try:
        
        
        ms_token = get_tiktok_cookies_from_file("cookies.txt")
        print(f" ms_token:\t {ms_token} \n")
        
        
        return ms_token
    
    except Exception as e:
        print("Exception" + e)
    except SameMsTokenException as e:
        print(e.message)
    except ValueError as e:
        print(e)
        print("Please check your ms_token")
    

def getUserList(userlistLink: str):
    userlist = get_user_list(userlistLink)
    if not userlist:
        raise Exception("No users found in the user list.")
    return userlist

async def divide_list(userlist: list, num_parts: int, selectedPart: int) -> list:
    userlist = userlist[selectedPart::num_parts]
    return userlist

def saveIndex(index: dict):
    with open("Data/JSON/index.json", "w") as f:
        json.dump(index, f)
def openIndex() -> tuple:
    with open("Data/JSON/index.json", "r") as f:
        index = f.read()
    index = json.loads(index)
    
    return index["parts"], index["selectedPart"]
            
async def getInfo(hashtag: str, userlistLink: str) -> dict:
    
    # ms_token = get_tiktok_cookies_from_file("Data/JSON/cookies.json")
    userlist = getUserList(userlistLink)
    {
    # length = len(userlist)
    # try:
    #     num_parts, selectedPart = openIndex()
    #     print(f"num_parts: {num_parts}, selectedPart: {selectedPart}")
    # except:   
    #     print("No index.json")
    #     num_parts = 1
    #     selectedPart = 0
        
    # if selectedPart >= num_parts -1:
    #     selectedPart = 0
    # else:
    #     selectedPart += 1
    
    # maxusersinrow = 16
    # num_parts = length // maxusersinrow
    
    # if num_parts == 0:
    #     num_parts = 1
    # print(f"num_parts: {num_parts}, selectedPart: {selectedPart}")
    
    # userSmallLists = await divide_list(userlist, num_parts, selectedPart)
    # print(f"userSmallLists: {userSmallLists}")
    # userlist = userSmallLists
    
    # saveIndex({"parts": num_parts, "selectedPart": selectedPart})
    }
    blackList=getBlackList("Data/JSON/blackList.json")
    
    result = await users_videos_with_hashtag(
        usernameList=userlist,
        hashtag=hashtag,
        blackList=blackList
        )
    
    
    return result #result
    
def getBlackList(blackListFile: str) -> dict:
    try:
        with open(blackListFile, "r") as f:
            blackList = f.read()
        if not blackList:
            return {}
        json_blackList = json.loads(blackList)
        return json_blackList
    except Exception as e:
        print(e)
        return {}
#if __name__ == "__main__":
    # ms_token= get_tiktok_cookies_from_file("cookies.txt")
    # userlistLink = "tiktok_stats/tiktokNames.txt"
    # userlistLink = "tiktok_stats/names.txt"
    # userlist = getUserList(userlistLink)
    # hashtag = "костиккакто"
    # result = 0
    # blackList=getBlackList("blackList.json")
    # print(f"userlist = {blackList}, users = {blackList.get('usernames')}, videos = {blackList.get('videos')}")
    
    # try:
    #     result = asyncio.run(tiktokUserCountVideoViews(
    #         userlist=userlist,
    #         ms_token="pLSi7qEbF7imuiF0_ySIDEJe_Ew97wEpGvTZL5Icr8WmcazmH8qwiGigUt7HwWbk6sNffDl6KqnK5Ll1WfqRawl3f-zVNtcSD6iAfRL86GzR5z2A7k5O1BrGtsumNbKFy2XuzYca1SAotXiHd16_",
    #         hashtag=hashtag,
    #         blackList=blackList
    #         ))
    # except SameMsTokenException as e:
    #     print(e.message)
        
    # print(f"returnValue = {result}")
    #asyncio.run(getInfo("костиккакто", "tiktok_stats/tiktokNames.txt"))