Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error processing [video link] 'TikTokApi' object has no attribute 'num_sessions' #1190

Open
JohnWyatt077 opened this issue Sep 6, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@JohnWyatt077
Copy link

When scraping for video data with TikTokApi, it shows 'object has no attribute 'num_sessions' in my log display area on the program panel. In cmd, no error reported

#code#

import tkinter as tk
from tkinter import scrolledtext
from tkinter import messagebox
import pandas as pd
from TikTokApi import TikTokApi
import asyncio

Initialize TikTok API

try:
api = TikTokApi() # Initialize API without additional parameters
except Exception as e:
print(f"Failed to initialize TikTokApi: {str(e)}")
exit(1)

Asynchronous function to fetch video data and update Excel

async def fetch_video_data():
try:
# Load Excel file
df = pd.read_excel("VideoData.xlsx", engine='openpyxl')

    # Check if Column A has video links
    if 'Video Link' not in df.columns:
        log_area.insert(tk.END, "Error: 'Video Link' column not found.\n")
        return
    
    # Prepare columns for data if not present
    columns = ['Publish Date', 'Author Name', 'Views', 'Likes', 'Comments', 'Saves', 'Shares']
    for column in columns:
        if column not in df.columns:
            df[column] = None

    for index, row in df.iterrows():
        video_url = row['Video Link']
        try:
            # Fetch video details
            video = api.video(url=video_url)
            video_data = await video.info()  # Await if needed based on the library

            # Extract data
            publish_date = pd.to_datetime(video_data.get('createTime', None), unit='s')
            author_name = video_data.get('author', {}).get('uniqueId', '')
            views = video_data.get('stats', {}).get('playCount', 0)
            likes = video_data.get('stats', {}).get('diggCount', 0)
            comments = video_data.get('stats', {}).get('commentCount', 0)
            shares = video_data.get('stats', {}).get('shareCount', 0)

            # TikTok API does not provide saves
            saves = 'N/A'

            # Update DataFrame
            df.at[index, 'Publish Date'] = publish_date
            df.at[index, 'Author Name'] = author_name
            df.at[index, 'Views'] = views
            df.at[index, 'Likes'] = likes
            df.at[index, 'Comments'] = comments
            df.at[index, 'Saves'] = saves
            df.at[index, 'Shares'] = shares

            log_area.insert(tk.END, f"Processed: {video_url}\n")

        except Exception as e:
            log_area.insert(tk.END, f"Error processing {video_url}: {str(e)}\n")
    
    # Save the updated DataFrame back to Excel
    df.to_excel("VideoData.xlsx", index=False, engine='openpyxl')
    log_area.insert(tk.END, "Done updating Excel file.\n")
    messagebox.showinfo("Info", "Data fetch completed successfully!")

except Exception as e:
    log_area.insert(tk.END, f"Error: {str(e)}\n")

Function to start asynchronous fetch

def start_fetch():
asyncio.run(fetch_video_data()) # Run the data fetch coroutine

GUI Setup

root = tk.Tk()
root.title("TikTok Video Data Fetcher")

Start Button

start_button = tk.Button(root, text="Start", command=start_fetch)
start_button.pack(pady=10)

Log Display Area

log_area = scrolledtext.ScrolledText(root, width=80, height=20)
log_area.pack(padx=10, pady=10)

Run the GUI event loop

root.mainloop()

Expected behavior: Video data scraped and saved to Excel

  • OS: [Windows 11]
@JohnWyatt077 JohnWyatt077 added the bug Something isn't working label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant