Download restorer.py from nyakaii/max-posts: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/datasets/nyakaii/max-posts/resolve/main/restorer.py
- Command line
-
hf download hf://datasets/nyakaii/max-posts/restorer.py
-
curl -L -o restorer.py https://huggingface.co/datasets/nyakaii/max-posts/resolve/main/restorer.py
3.67 kB
| import sqlite3 | |
| import zlib | |
| import os | |
| OLD_DB = 'posts.db' | |
| NEW_DB = 'posts_uncompressed.db' | |
| def decompress_text(blob_data): | |
| if not blob_data: | |
| return None | |
| if isinstance(blob_data, str): | |
| return blob_data | |
| try: | |
| return zlib.decompress(blob_data).decode('utf-8') | |
| except zlib.error: | |
| return blob_data.decode('utf-8', errors='replace') | |
| def restore_database(): | |
| if not os.path.exists(OLD_DB): | |
| print(f"База данных {OLD_DB} не найдена.") | |
| return | |
| if os.path.exists(NEW_DB): | |
| os.remove(NEW_DB) | |
| conn_old = sqlite3.connect(OLD_DB) | |
| conn_old.row_factory = sqlite3.Row | |
| cursor_old = conn_old.cursor() | |
| conn_new = sqlite3.connect(NEW_DB) | |
| cursor_new = conn_new.cursor() | |
| cursor_new.execute(''' | |
| CREATE TABLE IF NOT EXISTS posts ( | |
| id TEXT PRIMARY KEY, | |
| channel_id TEXT, | |
| html TEXT, | |
| text TEXT, | |
| views_count INTEGER, | |
| reactions_count INTEGER, | |
| created_at TEXT, | |
| image_url TEXT, | |
| content_type TEXT, | |
| forward_from_chat_name TEXT, | |
| forward_from_chat_link TEXT, | |
| forward_from_icon_url TEXT, | |
| er REAL, | |
| err REAL, | |
| reactions TEXT, | |
| id_full TEXT, | |
| comments_count INTEGER, | |
| link TEXT, | |
| views_total INTEGER, | |
| views_1h INTEGER, | |
| views_24h INTEGER, | |
| channel_name TEXT, | |
| channel_image_url TEXT, | |
| attaches TEXT, | |
| elements TEXT | |
| ) | |
| ''') | |
| cursor_new.execute(''' | |
| CREATE TABLE IF NOT EXISTS scraped_channels ( | |
| channel_id TEXT PRIMARY KEY, | |
| target_count INTEGER, | |
| fetched_count INTEGER, | |
| total_available INTEGER, | |
| is_completed INTEGER | |
| ) | |
| ''') | |
| print("Чтение сжатых данных...") | |
| cursor_old.execute("SELECT * FROM posts") | |
| rows = cursor_old.fetchall() | |
| if not rows: | |
| print("Таблица posts пуста.") | |
| else: | |
| print(f"Найдено {len(rows)} постов. Начинаю распаковку...") | |
| columns = rows[0].keys() | |
| placeholders = ",".join(["?"] * len(columns)) | |
| fields_to_decompress = {'html', 'text', 'reactions', 'attaches', 'elements'} | |
| new_records = [] | |
| for row in rows: | |
| new_row = [] | |
| for col in columns: | |
| if col in fields_to_decompress: | |
| new_row.append(decompress_text(row[col])) | |
| else: | |
| new_row.append(row[col]) | |
| new_records.append(tuple(new_row)) | |
| cursor_new.executemany(f"INSERT INTO posts VALUES ({placeholders})", new_records) | |
| cursor_old.execute("SELECT * FROM scraped_channels") | |
| channels = cursor_old.fetchall() | |
| if channels: | |
| ch_columns = len(channels[0].keys()) | |
| ch_placeholders = ",".join(["?"] * ch_columns) | |
| ch_records = [tuple(row) for row in channels] | |
| cursor_new.executemany(f"INSERT INTO scraped_channels VALUES ({ch_placeholders})", ch_records) | |
| conn_new.commit() | |
| conn_old.close() | |
| conn_new.close() | |
| old_size = os.path.getsize(OLD_DB) / (1024 * 1024) | |
| new_size = os.path.getsize(NEW_DB) / (1024 * 1024) | |
| print("\nБаза данных успешно распакована.") | |
| print(f"Размер сжатой БД ({OLD_DB}): {old_size:.2f} MB") | |
| print(f"Размер распакованной БД ({NEW_DB}): {new_size:.2f} MB") | |
| if __name__ == '__main__': | |
| restore_database() |