fix _update_browse_history_setup (#345)

This commit is contained in:
Yanxiao Zhao
2025-09-25 13:22:40 +08:00
committed by GitHub
parent a4f8fe2f00
commit 6827949418

View File

@@ -813,7 +813,7 @@ class SetupController:
def _update_browse_history_setup(self, **config): def _update_browse_history_setup(self, **config):
cache_path = os.path.join(self.cache_dir, "history_new.sqlite") 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): if not os.path.exists(cache_path):
max_retries = 3 max_retries = 3
downloaded = False downloaded = False
@@ -839,80 +839,82 @@ class SetupController:
else: else:
logger.info("File already exists in cache directory") logger.info("File already exists in cache directory")
# copy a new history file in the tmp folder # 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: for history_item in history:
url = history_item['url'] url = history_item['url']
title = history_item['title'] title = history_item['title']
visit_time = datetime.now() - timedelta(seconds=history_item['visit_time_from_now_in_seconds']) visit_time = datetime.now() - timedelta(seconds=history_item['visit_time_from_now_in_seconds'])
# Chrome use ms from 1601-01-01 as timestamp # Chrome use ms from 1601-01-01 as timestamp
epoch_start = datetime(1601, 1, 1) epoch_start = datetime(1601, 1, 1)
chrome_timestamp = int((visit_time - epoch_start).total_seconds() * 1000000) chrome_timestamp = int((visit_time - epoch_start).total_seconds() * 1000000)
conn = sqlite3.connect(db_path) conn = sqlite3.connect(db_path)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(''' cursor.execute('''
INSERT INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden) INSERT INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
''', (url, title, 1, 0, chrome_timestamp, 0)) ''', (url, title, 1, 0, chrome_timestamp, 0))
url_id = cursor.lastrowid url_id = cursor.lastrowid
cursor.execute(''' cursor.execute('''
INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration) INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration)
VALUES (?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
''', (url_id, chrome_timestamp, 0, 805306368, 0, 0)) ''', (url_id, chrome_timestamp, 0, 805306368, 0, 0))
conn.commit() conn.commit()
conn.close() 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 # get the path of the history file according to the platform
os_type = controller.get_vm_platform() os_type = controller.get_vm_platform()
if os_type == 'Windows': 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():
chrome_history_path = controller.execute_python_command( 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() 'output'].strip()
else: elif os_type == 'Darwin':
chrome_history_path = controller.execute_python_command( 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() 'output'].strip()
else: elif os_type == 'Linux':
raise Exception('Unsupported operating system') if "arm" in platform.machine():
chrome_history_path = controller.execute_python_command(
form = MultipartEncoder({ "import os; print(os.path.join(os.getenv('HOME'), 'snap', 'chromium', 'common', 'chromium', 'Default', 'History'))")[
"file_path": chrome_history_path, 'output'].strip()
"file_data": (os.path.basename(chrome_history_path), open(db_path, "rb")) else:
}) chrome_history_path = controller.execute_python_command(
headers = {"Content-Type": form.content_type} "import os; print(os.path.join(os.getenv('HOME'), '.config', 'google-chrome', 'Default', 'History'))")[
logger.debug(form.content_type) 'output'].strip()
# 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: else:
logger.error("Failed to upload file. Status code: %s", response.text) raise Exception('Unsupported operating system')
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) 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)