-
Notifications
You must be signed in to change notification settings - Fork 0
/
client1.py
207 lines (167 loc) · 8.53 KB
/
client1.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import customtkinter as ctk
from apify_client import ApifyClient
import json
import csv
import datetime
import shared_variables as shv
from pandas import read_csv
import logging
from tkinter import filedialog
from logging_window import LoggingWindow, TextHandler # Ensure this is correctly imported
def save_to_json(data, filename, root_path):
if root_path:
path = ""
else:
path = filedialog.asksaveasfilename(defaultextension=f".json",
filetypes=[(f"JSON files", f"*.json")])
with open(path + filename, "w", encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
def save_to_csv(data, filename, root_path):
if not data:
return
# Extract all field names from the data
all_keys = set()
for item in data:
all_keys.update(item.keys())
if root_path:
path = ""
else:
path = filedialog.asksaveasfilename(defaultextension=f".csv",
filetypes=[(f"CSV files", f"*.csv")])
with open(path + filename, "w", newline='', encoding='utf-8') as csv_file:
dict_writer = csv.DictWriter(csv_file, fieldnames=list(all_keys))
dict_writer.writeheader()
dict_writer.writerows(data)
class Client1:
def __init__(self, api_token):
self.first_actor_ran = False # Flag to track if the first actor ran successfully
self.client = ApifyClient(api_token)
self.urls = [] # Initialize urls as an instance variable
self.init_ui()
timestamp = datetime.datetime.now()
self.str_timestamp = timestamp.strftime("%Y-%m-%d %H-%M-%S")
# Set up logging
self.logger = logging.getLogger('apify')
self.logger.setLevel(logging.DEBUG)
# Initialize the logging window but don't show it yet
self.logging_window = None
def init_ui(self):
self.app = ctk.CTk()
self.app.title("Instagram Client")
self.app.geometry("600x800")
ctk.set_appearance_mode(shv.appearance)
ctk.set_default_color_theme(shv.theme)
# Initialize the logging window
self.direct_urls_label = ctk.CTkLabel(self.app, text="usernames (comma separated)")
self.direct_urls_label.pack(pady=5)
self.direct_urls_entry = ctk.CTkEntry(self.app, placeholder_text="e.g. atlantasanad")
self.direct_urls_entry.pack(pady=5)
self.post_results_limit_label = ctk.CTkLabel(self.app, text="Post Results Limit:")
self.post_results_limit_label.pack(pady=5)
self.post_results_limit_entry = ctk.CTkEntry(self.app, placeholder_text="200")
self.post_results_limit_entry.pack(pady=5)
self.run_first_actor_button = ctk.CTkButton(self.app, text="Get posts", command=self.run_first_actor)
self.run_first_actor_button.pack(pady=20)
self.comments_results_limit_label = ctk.CTkLabel(self.app, text="Comment Results Limit:")
self.comments_results_limit_label.pack(pady=5)
self.comments_results_limit_entry = ctk.CTkEntry(self.app, placeholder_text="200")
self.comments_results_limit_entry.pack(pady=5)
self.run_second_actor_button = ctk.CTkButton(self.app, text="Get the Comments", command=self.run_second_actor)
self.run_second_actor_button.pack(pady=20)
self.add_parent_data_var = ctk.BooleanVar()
self.add_parent_data_checkbox = ctk.CTkCheckBox(
self.app, text="Add Parent Data", variable=self.add_parent_data_var)
self.add_parent_data_checkbox.pack(pady=5)
self.save_as_json_var = ctk.BooleanVar()
self.save_as_json_checkbox = ctk.CTkCheckBox(
self.app, text="Save as JSON", variable=self.save_as_json_var)
self.save_as_json_checkbox.pack(pady=5)
self.save_as_csv_var = ctk.BooleanVar()
self.save_as_csv_checkbox = ctk.CTkCheckBox(
self.app, text="Save as CSV", variable=self.save_as_csv_var)
self.save_as_csv_checkbox.pack(pady=5)
self.Warning_label = ctk.CTkLabel(self.app,
text="DO NOT INTERRUPT!!!!! \nIf it shows that the app do not respond it's normal, just wait. \nIn case it takes too long you can still check the logs on the apify website ")
self.Warning_label.pack(pady=20)
self.open_log_button = ctk.CTkButton(self.app, text="Open Log Window", command=self.open_log_window)
self.open_log_button.pack(pady=20)
def open_log_window(self):
if self.logging_window is None:
self.logging_window = LoggingWindow(ctk.CTk())
self.handler = TextHandler(self.logging_window.text_area)
self.logger.addHandler(self.handler)
self.logging_window.root.mainloop()
def run_first_actor(self):
try:
direct_urls = ["https://www.instagram.com/" + url for url in
list(filter(None, self.direct_urls_entry.get().split(",")))]
post_results_limit = self.post_results_limit_entry.get()
post_search_type = "hashtag"
post_search_limit = "1"
add_parent_data = bool(self.add_parent_data_var.get())
if post_results_limit and post_search_limit:
post_results_limit = int(post_results_limit)
post_search_limit = int(post_search_limit)
else:
raise ValueError("Results Limit and Search Limit must be provided")
run_input = {
"directUrls": direct_urls,
"resultsType": "posts",
"resultsLimit": post_results_limit,
"searchType": post_search_type,
"searchLimit": post_search_limit,
"addParentData": add_parent_data,
}
run = self.client.actor("shu8hvrXbJbY3Eb9W").call(run_input=run_input)
results = [item for item in self.client.dataset(run["defaultDatasetId"]).iterate_items()]
# Save results to CSV
csv_filename = f"temp_instagram_post.csv"
save_to_csv(results, csv_filename, True)
self.urls = list(read_csv(csv_filename)["url"])
# Log the obtained URLs
self.logger.info(f"URLs obtained: {self.urls}")
except Exception as e:
self.logger.error(f"An error occurred while running the first actor: {e}")
self.urls = [] # Clear URLs on error
def run_second_actor(self):
try:
if not self.urls:
self.logger.error(f"URLs is empty: {self.urls}")
raise RuntimeError("First actor must run successfully and provide URLs before running the second actor")
comments_results_limit = self.comments_results_limit_entry.get()
comment_search_type = "hashtag"
comments_search_limit = "1"
add_parent_data = bool(self.add_parent_data_var.get())
save_as_json = self.save_as_json_var.get()
save_as_csv = self.save_as_csv_var.get()
if comments_results_limit and comments_search_limit:
comments_results_limit = int(comments_results_limit)
comments_search_limit = int(comments_search_limit)
else:
raise ValueError("Results Limit and Search Limit must be provided")
run_input = {
"directUrls": self.urls,
"resultsType": "comments",
"resultsLimit": comments_results_limit,
"searchType": comment_search_type,
"searchLimit": comments_search_limit,
"addParentData": add_parent_data,
}
run = self.client.actor("shu8hvrXbJbY3Eb9W").call(run_input=run_input)
results = [item for item in self.client.dataset(run["defaultDatasetId"]).iterate_items()]
if save_as_json:
json_filename = f"output_instagram_comments_{self.str_timestamp}.json"
save_to_json(results, json_filename, root_path="")
self.logger.info(f"Comments saved as JSON to {json_filename}")
if save_as_csv:
csv_filename = f"output_instagram_comments_{self.str_timestamp}.csv"
save_to_csv(results, csv_filename, root_path="")
self.logger.info(f"Comments saved as CSV to {csv_filename}")
except Exception as e:
self.logger.error(f"An error occurred while running the second actor: {e}")
def start(self):
self.app.mainloop()
if __name__ == "__main__":
api_token = shv.default_token
client_app = Client1(api_token)
client_app.start()