Analyze TikTok comment sentiment by extracting comments via Scavio API ($0.005/page) and classifying them with an LLM ($0.01-0.05 per 50 comments). Total cost per video analysis: $0.02-0.10 depending on comment volume.
Prerequisites
- Scavio API key
- OpenAI API key (or any LLM)
- Python 3.8+ with requests and openai libraries
Walkthrough
Step 1: Extract comments from a video
Pull comments using the video/comments endpoint.
import requests, os
SCAVIO_HEADERS = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
'Content-Type': 'application/json'}
def get_comments(video_id, pages=3):
comments = []
cursor = 0
for _ in range(pages):
data = requests.post('https://api.scavio.dev/api/v1/tiktok/video/comments',
headers=SCAVIO_HEADERS,
json={'aweme_id': video_id, 'count': 20, 'cursor': cursor}).json()['data']
comments.extend(data.get('comments', []))
if not data.get('has_more'):
break
cursor = data.get('cursor', cursor + 20)
return comments
comments = get_comments('7123456789')Step 2: Classify sentiment with LLM
Pass comment texts to an LLM for sentiment classification.
from openai import OpenAI
client = OpenAI()
texts = [c['text'] for c in comments[:50]]
resp = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user',
'content': f'Classify each comment as positive, negative, or neutral. '
f'Return JSON array of objects with text and sentiment.\n'
f'Comments: {texts}'}],
response_format={'type': 'json_object'})
results = resp.choices[0].message.content
print(results)Python Example
import requests, os, json
from openai import OpenAI
SCAVIO_H = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
'Content-Type': 'application/json'}
client = OpenAI()
def analyze_sentiment(video_id):
# Extract comments
comments = []
cursor = 0
for _ in range(3):
data = requests.post('https://api.scavio.dev/api/v1/tiktok/video/comments',
headers=SCAVIO_H,
json={'aweme_id': video_id, 'count': 20, 'cursor': cursor}).json()['data']
comments.extend(data.get('comments', []))
if not data.get('has_more'): break
cursor = data.get('cursor', cursor + 20)
# Classify with LLM
texts = [c['text'][:100] for c in comments[:50]]
resp = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user',
'content': f'Classify each as positive/negative/neutral. Return JSON with counts.\n{texts}'}],
response_format={'type': 'json_object'})
return json.loads(resp.choices[0].message.content)
result = analyze_sentiment('7123456789')
print(result)JavaScript Example
const SH = {'Authorization': `Bearer ${process.env.SCAVIO_API_KEY}`, 'Content-Type': 'application/json'};
async function analyzeSentiment(videoId) {
const comments = [];
let cursor = 0;
for (let i = 0; i < 3; i++) {
const r = await fetch('https://api.scavio.dev/api/v1/tiktok/video/comments', {
method: 'POST', headers: SH,
body: JSON.stringify({aweme_id: videoId, count: 20, cursor})
}).then(r => r.json());
comments.push(...(r.data.comments || []));
if (!r.data.has_more) break;
cursor = r.data.cursor || cursor + 20;
}
const texts = comments.slice(0, 50).map(c => c.text.slice(0, 100));
console.log(`Extracted ${comments.length} comments, classifying ${texts.length}`);
return texts;
}
analyzeSentiment('7123456789');Expected Output
Sentiment classification of TikTok comments: count of positive, negative, and neutral comments with key themes identified.