Skip to content

Commit 54d2367

Browse files
authored
Merge pull request #29 from simular-ai/1124-macos
Add more helpers to macOS and an example for web crawler on chrome
2 parents 561fae6 + 3ec42ad commit 54d2367

9 files changed

Lines changed: 725 additions & 2 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ python openaci/cli_app.py
4242

4343
Please remember to review and modify the code according to your specific use case and requirements. It is highly recommended to thoroughly test the code in a controlled environment before using it in any production or critical systems.
4444

45+
### Mac OS
46+
To run OpenACI on macOS, we need xcode developer package.
47+
```
48+
xcode-select --install
49+
```
50+
51+
Also install
52+
```
53+
pip install pyobjc-framework-ApplicationServices
54+
```
55+
4556
## Contributing
4657
Contributions to this experimental codebase are welcome. If you discover any issues, have suggestions, or would like to add new features, please open an issue or submit a pull request.
4758

examples/crawler/main.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from openaci.macos.Grounding import GroundingAgent
2+
import openaci.macos.system as system
3+
import openaci.macos.web as web
4+
import time
5+
import json
6+
import os
7+
8+
def crawl(main_url, root_dir=None, browser="Chrome"):
9+
# get domain from url in general way
10+
domain = web.get_domain(main_url)
11+
print('domain', domain)
12+
content = system.fetch_content_from_url(main_url, where=browser, wait_time=2, retry=3)
13+
14+
# pretty print content
15+
# print(json.dumps(content, indent=4))
16+
17+
# get all the links from content and open them in chrome
18+
# do not open the same link twice
19+
# do not open links that are not in the same domain
20+
# create a directory to save the json content
21+
visited_links = []
22+
# default goes to home directory downloads folder
23+
root_dir = root_dir or os.path.expanduser("~/Downloads/simular/")
24+
root_dir = f"{root_dir}/{domain}"
25+
os.makedirs(root_dir, exist_ok=True)
26+
27+
def get_file_path(link):
28+
# handle / inside the link and remove https://
29+
link = link.replace('/', '_').replace('https://', '')
30+
return f"{root_dir}/{link}.json"
31+
32+
# save json content to a file
33+
def save_json_content(content, link):
34+
with open(get_file_path(link), "w") as f:
35+
# pretty print json
36+
json.dump(content, f, indent=4)
37+
38+
def load_json_content(link):
39+
with open(get_file_path(link), "r") as f:
40+
return json.load(f)
41+
return None
42+
43+
save_json_content(content, main_url)
44+
45+
# breadth first search visit all the links
46+
queue = []
47+
48+
def add_to_queue(links):
49+
for link in links:
50+
# skip if link is already visited
51+
if link in visited_links:
52+
continue
53+
# skip if link is not in the same domain
54+
print(web.get_domain(link), domain)
55+
if web.get_domain(link) != domain:
56+
continue
57+
queue.append(link)
58+
59+
try:
60+
add_to_queue([x['url'] for x in content['links'] if x['url']])
61+
except Exception as e:
62+
print(f"Error adding to queue: {e}")
63+
64+
print(content['links'])
65+
print(f"Queue size: {len(queue)}")
66+
67+
while queue:
68+
link = queue.pop(0)
69+
print(f"Visiting {link}")
70+
71+
visited_links.append(link)
72+
# skip if file already exists
73+
if os.path.exists(get_file_path(link)):
74+
# load the json content
75+
content = load_json_content(link)
76+
else:
77+
content = system.fetch_content_from_url(link, where=browser, wait_time=2)
78+
save_json_content(content, link)
79+
80+
try:
81+
add_to_queue([x['url'] for x in content['links'] if x['url']])
82+
except Exception as e:
83+
print(f"Error adding to queue: {e}")
84+
85+
if __name__ == "__main__":
86+
crawl("smarthistory.org")

openaci/macos/Grounding.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
from openaci.macos.UIElement import UIElement
24+
from openaci.macos.system import open_running_app
2425

2526
from AppKit import NSWorkspace, NSRunningApplication
2627

@@ -289,6 +290,9 @@ def open_app(self, app_name):
289290
if app_name in self.all_apps:
290291
print(f"{app_name} has been opened successfully.")
291292
return f"""import subprocess; subprocess.run(["open", "-a", "{app_name}"], check=True)"""
293+
elif open_running_app(app_name):
294+
print(f"{app_name} has been opened successfully.")
295+
return """WAIT"""
292296
else:
293297
self.execution_feedback = "There is no application " + app_name + " installed on the system. Please replan and avoid this action."
294298
print(self.execution_feedback)

0 commit comments

Comments
 (0)