Last active
June 17, 2019 12:26
-
-
Save yangezheng/bdb14c2130566d82612644f6ac4741d9 to your computer and use it in GitHub Desktop.
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 json | |
| import requests | |
| from pathlib import Path | |
| """ | |
| ╭──────────────────────────────────────────────╮ | |
| │ ◎ ○ ○ ░░░░░░░░░░░░ Usage ░░░░░░░░░░░░░░░░░░│ | |
| ├──────────────────────────────────────────────┤ | |
| │ │ | |
| │ This gist is to download all the files from │ | |
| │ the Rocket Chat chat-history. │ | |
| │ │ | |
| │ Only modify the following when needed: │ | |
| │ │ | |
| │ │ | |
| │ baseUrl; │ | |
| │desPath(where you want to download files to); │ | |
| │ chatPath(where are the chat files); │ | |
| │ X-Auth-Token; │ | |
| │ X-User-Id. │ | |
| │ │ | |
| ├──────────────────────────────────────────────┤ | |
| └──────────────────────────────────────────────┘ | |
| """ | |
| baseUrl = "https://xxxxxxxxxxxxxxx.chat" | |
| desPath = "/xxxxxxxxxxxxxxx/" | |
| chatPath = "/xxxxxxxxxxxxxxx/" | |
| headers = { | |
| 'X-Auth-Token': 'xxxxxxxxxxxxxxx', | |
| 'X-User-Id': 'xxxxxxxxxxxxxxx', | |
| } | |
| ################################################################# | |
| ################################################################# | |
| # iterate through files in directory | |
| pathlist = Path(chatPath).glob('*.json') | |
| for file in pathlist: | |
| filename = str(file) | |
| # read file | |
| with open(filename, 'r') as myfile: | |
| data = myfile.read() | |
| # parse file | |
| obj = json.loads(data) | |
| # iterate messages in json | |
| for file in obj["messages"]: | |
| if "attachments" in file: | |
| for attachment in file["attachments"]: | |
| if 'title' in attachment: | |
| print(attachment["title"]) | |
| print(attachment["title_link"]) | |
| response = requests.get(baseUrl + attachment["title_link"], headers=headers) | |
| with open(desPath+attachment["title"], 'wb') as f: | |
| f.write(response.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment