diff --git a/desktop_env/controllers/setup.py b/desktop_env/controllers/setup.py index 2635cfc..f7637c1 100644 --- a/desktop_env/controllers/setup.py +++ b/desktop_env/controllers/setup.py @@ -813,7 +813,7 @@ class SetupController: def _update_browse_history_setup(self, **config): cache_path = os.path.join(self.cache_dir, "history_new.sqlite") - db_url = "https://drive.usercontent.google.com/u/0/uc?id=1Lv74QkJYDWVX0RIgg0Co-DUcoYpVL0oX&export=download" # google drive + db_url = "https://huggingface.co/datasets/xlangai/ubuntu_osworld_file_cache/resolve/main/chrome/44ee5668-ecd5-4366-a6ce-c1c9b8d4e938/history_empty.sqlite?download=true" if not os.path.exists(cache_path): max_retries = 3 downloaded = False @@ -839,80 +839,82 @@ class SetupController: else: logger.info("File already exists in cache directory") # copy a new history file in the tmp folder - db_path = cache_path + with tempfile.TemporaryDirectory() as tmp_dir: + db_path = os.path.join(tmp_dir, "history_empty.sqlite") + shutil.copy(cache_path, db_path) - history = config['history'] + history = config['history'] - for history_item in history: - url = history_item['url'] - title = history_item['title'] - visit_time = datetime.now() - timedelta(seconds=history_item['visit_time_from_now_in_seconds']) + for history_item in history: + url = history_item['url'] + title = history_item['title'] + visit_time = datetime.now() - timedelta(seconds=history_item['visit_time_from_now_in_seconds']) - # Chrome use ms from 1601-01-01 as timestamp - epoch_start = datetime(1601, 1, 1) - chrome_timestamp = int((visit_time - epoch_start).total_seconds() * 1000000) + # Chrome use ms from 1601-01-01 as timestamp + epoch_start = datetime(1601, 1, 1) + chrome_timestamp = int((visit_time - epoch_start).total_seconds() * 1000000) - conn = sqlite3.connect(db_path) - cursor = conn.cursor() + conn = sqlite3.connect(db_path) + cursor = conn.cursor() - cursor.execute(''' - INSERT INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden) - VALUES (?, ?, ?, ?, ?, ?) - ''', (url, title, 1, 0, chrome_timestamp, 0)) + cursor.execute(''' + INSERT INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden) + VALUES (?, ?, ?, ?, ?, ?) + ''', (url, title, 1, 0, chrome_timestamp, 0)) - url_id = cursor.lastrowid + url_id = cursor.lastrowid - cursor.execute(''' - INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration) - VALUES (?, ?, ?, ?, ?, ?) - ''', (url_id, chrome_timestamp, 0, 805306368, 0, 0)) + cursor.execute(''' + INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration) + VALUES (?, ?, ?, ?, ?, ?) + ''', (url_id, chrome_timestamp, 0, 805306368, 0, 0)) - conn.commit() - conn.close() + conn.commit() + conn.close() - logger.info('Fake browsing history added successfully.') + logger.info('Fake browsing history added successfully.') - controller = PythonController(self.vm_ip, self.server_port) + controller = PythonController(self.vm_ip, self.server_port) - # get the path of the history file according to the platform - os_type = controller.get_vm_platform() + # get the path of the history file according to the platform + os_type = controller.get_vm_platform() - if os_type == 'Windows': - chrome_history_path = controller.execute_python_command( - """import os; print(os.path.join(os.getenv('USERPROFILE'), "AppData", "Local", "Google", "Chrome", "User Data", "Default", "History"))""")[ - 'output'].strip() - elif os_type == 'Darwin': - chrome_history_path = controller.execute_python_command( - """import os; print(os.path.join(os.getenv('HOME'), "Library", "Application Support", "Google", "Chrome", "Default", "History"))""")[ - 'output'].strip() - elif os_type == 'Linux': - if "arm" in platform.machine(): + if os_type == 'Windows': chrome_history_path = controller.execute_python_command( - "import os; print(os.path.join(os.getenv('HOME'), 'snap', 'chromium', 'common', 'chromium', 'Default', 'History'))")[ + """import os; print(os.path.join(os.getenv('USERPROFILE'), "AppData", "Local", "Google", "Chrome", "User Data", "Default", "History"))""")[ 'output'].strip() - else: + elif os_type == 'Darwin': chrome_history_path = controller.execute_python_command( - "import os; print(os.path.join(os.getenv('HOME'), '.config', 'google-chrome', 'Default', 'History'))")[ + """import os; print(os.path.join(os.getenv('HOME'), "Library", "Application Support", "Google", "Chrome", "Default", "History"))""")[ 'output'].strip() - else: - raise Exception('Unsupported operating system') - - form = MultipartEncoder({ - "file_path": chrome_history_path, - "file_data": (os.path.basename(chrome_history_path), open(db_path, "rb")) - }) - headers = {"Content-Type": form.content_type} - logger.debug(form.content_type) - - # send request to server to upload file - try: - logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload") - response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form) - if response.status_code == 200: - logger.info("Command executed successfully: %s", response.text) + elif os_type == 'Linux': + if "arm" in platform.machine(): + chrome_history_path = controller.execute_python_command( + "import os; print(os.path.join(os.getenv('HOME'), 'snap', 'chromium', 'common', 'chromium', 'Default', 'History'))")[ + 'output'].strip() + else: + chrome_history_path = controller.execute_python_command( + "import os; print(os.path.join(os.getenv('HOME'), '.config', 'google-chrome', 'Default', 'History'))")[ + 'output'].strip() else: - logger.error("Failed to upload file. Status code: %s", response.text) - except requests.exceptions.RequestException as e: - logger.error("An error occurred while trying to send the request: %s", e) + raise Exception('Unsupported operating system') - self._execute_setup(["sudo chown -R user:user /home/user/.config/google-chrome/Default/History"], shell=True) + form = MultipartEncoder({ + "file_path": chrome_history_path, + "file_data": (os.path.basename(chrome_history_path), open(db_path, "rb")) + }) + headers = {"Content-Type": form.content_type} + logger.debug(form.content_type) + + # send request to server to upload file + try: + logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload") + response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form) + if response.status_code == 200: + logger.info("Command executed successfully: %s", response.text) + else: + logger.error("Failed to upload file. Status code: %s", response.text) + except requests.exceptions.RequestException as e: + logger.error("An error occurred while trying to send the request: %s", e) + + self._execute_setup(["sudo chown -R user:user /home/user/.config/google-chrome/Default/History"], shell=True)