Created
April 18, 2023 18:28
-
-
Save gonfva/2cb85c0b15363b5872f48bf164ba1e22 to your computer and use it in GitHub Desktop.
A quick script to download messages from some place
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| import sqlite3 | |
| import time | |
| # Define the API endpoint URL and bearer token | |
| bearer_token = <put token> | |
| server_url = https://api.<something here>.com/chat/v2/conversations/ | |
| chats = ["<chatA>","<chatB>"] | |
| # Create SQLite database and cursor | |
| conn = sqlite3.connect('records.db') | |
| c = conn.cursor() | |
| # Create records table if not exists | |
| c.execute('''CREATE TABLE IF NOT EXISTS records | |
| (chat TEXT, id TEXT, postedAt TEXT, authorID TEXT, content TEXT, clientMessageId TEXT)''') | |
| # Define function to save records into SQLite | |
| def save_records_to_db(chat, records): | |
| for record in records: | |
| if record['kind']=='message': | |
| c.execute("INSERT INTO records (chat, id, postedAt, authorID, content, clientMessageId) VALUES (?, ?, ?, ?, ?, ?)", | |
| (chat, record['id'], record['postedAt'], record['authorId'], record['content'], record['clientMessageId'])) | |
| conn.commit() | |
| # Define function to fetch records from API with pagination | |
| def fetch_records_from_api(chat, url): | |
| headers = { | |
| 'Authorization': f'Bearer {bearer_token}', | |
| 'Content-Type': 'application/json', | |
| 'X-Long-Encoding': 'string' | |
| } | |
| has_next_page = True | |
| page=1 | |
| while has_next_page: | |
| print(f"Page {page}") | |
| response = requests.get(url, headers=headers) | |
| if response.status_code == 200: | |
| data = json.loads(response.text) | |
| records = data['data']['results'] | |
| pagination_token = data['data']['pagination']['nextCursor'] | |
| save_records_to_db(chat, records) | |
| if pagination_token: | |
| url = f'{url}?nextCursor={pagination_token}' | |
| else: | |
| has_next_page = False | |
| else: | |
| print('Error',response.text) | |
| print(f'Error fetching records from API. Status code: {response.status_code}') | |
| has_next_page = False | |
| time.sleep(1) | |
| page +=1 | |
| # Fetch records from API and save to SQLite | |
| for chat in chats: | |
| url = f"{server_url}{chat}/messages?limit=50" | |
| fetch_records_from_api(chat, url) | |
| # Close database connection | |
| conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment