import requests,re,time,json,random

def fetch_random_device():
    url = "https://raw.githubusercontent.com/Z4usXcode/-/main/devices.txt"
    response = requests.get(url)
    response.raise_for_status()
    
    lines = response.text.strip().splitlines()
    if not lines:
        raise ValueError("Dosya boş.")
    line = random.choice(lines)    
    parts = line.strip().split(":")
    if len(parts) != 7:
        raise ValueError("Satır beklenen formatta değil.")    
    return {
        "iid": parts[0],
        "did": parts[1],
        "type": parts[2],
        "brand": parts[3],
        "os_version": parts[4],
        "cdid": parts[5],
        "odid": parts[6],
    }
def info(username):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Android 10; Pixel 3 Build/QKQ1.200308.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/125.0.6394.70 Mobile Safari/537.36 trill_350402 JsSdk/1.0 NetType/MOBILE Channel/googleplay AppName/trill app_version/35.3.1 ByteLocale/en ByteFullLocale/en Region/IN AppId/1180 Spark/1.5.9.1 AppVersion/35.3.1 BytedanceWebview/d8a21c6",
    }
    try:
        tikinfo = requests.get(f'https://www.tiktok.com/@{username}', headers=headers).text
        data = json.loads(re.search(r'<script id="__UNIVERSAL_DATA_FOR_REHYDRATION__" type="application/json">(.*?)</script>', tikinfo).group(1))
        user = data['__DEFAULT_SCOPE__']['webapp.user-detail']['userInfo']['user']
        stats = data['__DEFAULT_SCOPE__']['webapp.user-detail']['userInfo']['stats']
        
        return {
            'id': user['id'],
            'username': user['uniqueId'],
            'name': user['nickname'],
            'verified': user['verified'],
            'private': user['privateAccount'],
            'create_time': time.strftime('%Y-%m-%d', time.localtime(user['createTime'])),
            'videos': stats['videoCount'],
            'followers': stats['followerCount'],
            'following': stats['followingCount'],
            'likes': stats['heartCount']
        }
    except Exception as e:
        print(f"Error getting user info: {e}")
        return None
