forked from adib4lf1/Machine-Learning-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (50 loc) · 2.32 KB
/
main.py
File metadata and controls
63 lines (50 loc) · 2.32 KB
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
import os
import re
import csv # Import library csv
from argparse import ArgumentParser
from comment.Comment import Comment
from comment.helpers import logging
if(__name__ == '__main__'):
argp: ArgumentParser = ArgumentParser()
argp.add_argument("--url", '-u', type=str, default='Cm2cJmABD1p')
argp.add_argument("--cookie", '-c', type=str)
argp.add_argument("--output", '-o', type=str, default='data')
args = argp.parse_args()
# Ekstrak Post ID dari URL
post_id: str = (match := re.compile(r'https://www\.instagram\.com/(p|reel)/([^/?]+)|([^/]+)$').search(args.url)) and (match.group(2) or match.group(3))
comment: Comment = Comment(args.cookie)
if(not os.path.exists(args.output)):
os.makedirs(args.output)
# Eksekusi scraping
result_data = comment.excecute(post_id)
# Ambil waktu scraping dari root object result
scraped_at = result_data.get("date_now", "")
# Tentukan nama file output CSV
output_filename = f'{args.output}/{post_id}.csv'
with open(output_filename, 'w', newline='', encoding='utf-8') as csvfile:
# Definisi nama kolom
fieldnames = ['scraped_at', 'created_at', 'username', 'comment', 'likes']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Tulis Header
writer.writeheader()
# Iterasi setiap komentar utama
for c in result_data.get('comments', []):
# 1. Tulis Komentar Utama
writer.writerow({
'scraped_at': scraped_at, # Kapan discraping
'created_at': c['create_time'], # Kapan dibuat
'username': c['username'], # Isi Komentar
'comment': c['comment'], # Username
'likes': c['total_like'] # Jumlah Like
})
# 2. Tulis Reply/Balasan (jika ada)
if 'replies' in c and c['replies']:
for reply in c['replies']:
writer.writerow({
'scraped_at': scraped_at,
'created_at': reply['create_time'],
'username': reply['username'],
'comment': reply['comment'],
'likes': reply['total_like']
})
logging.info(f'Output data saved to: {output_filename}')