Analysez le sentiment des commentaires TikTok en extrayant les commentaires via l'API Scavio (0.005 $/page) et en les classifiant avec un LLM (0.01-0.05 $ pour 50 commentaires). Coût total par analyse vidéo : 0.02-0.10 $ selon le volume de commentaires.
Prérequis
- Clé API Scavio
- Clé API OpenAI (ou tout autre LLM)
- Python 3.8+ avec les bibliothèques requests et openai
Parcours
Étape 1: Extraire les commentaires d'une vidéo
Récupérer les commentaires via l'endpoint video/comments.
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')Étape 2: Classifier le sentiment avec un LLM
Transmettre les textes des commentaires à un LLM pour la classification des sentiments.
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)Exemple Python
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)Exemple JavaScript
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');Sortie attendue
Sentiment classification of TikTok comments: count of positive, negative, and neutral comments with key themes identified.