Last active
January 24, 2019 22:16
-
-
Save clokep/277f89d8799a67b299a21a6a67545385 to your computer and use it in GitHub Desktop.
Get all links a user sent to Slack
This file contains 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
""" | |
Get all HTTP or HTTPS links from a Slack account. | |
Fill in the Slack token, username to query and whether to query for http or https links. | |
First install slackclient: | |
pip install slackclient | |
""" | |
from __future__ import unicode_literals | |
import json | |
from time import sleep | |
from slackclient import SlackClient | |
slack_token = 'XXXXXXXXXXXXXXX' | |
user = 'XXXX' # A username, e.g. slackbot, or user ID, e.g. U123456. | |
sc = SlackClient(slack_token) | |
results = [] | |
for https in [False, True]: | |
print('Checking links for http{}'.format('s' if https else '')) | |
page = 1 | |
page_count = '?' | |
while True: | |
print("Getting page {} of {}".format(page, page_count)) | |
response = sc.api_call( | |
"search.messages", | |
query="http:{}// from:<@{}>".format('s' if https else '', user), | |
count=100, | |
page=page | |
) | |
assert response['ok'] | |
results.extend(response['messages']['matches']) | |
page_count = response['messages']['pagination']['page_count'] | |
if page >= page_count: | |
print("DONE") | |
break | |
page += 1 | |
sleep(1) | |
with open('output.json', 'w') as f: | |
json.dump(results, f) |
This file contains 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
""" | |
Retrieve just the text from the file that is output in download-links.py. | |
""" | |
from __future__ import unicode_literals | |
import json | |
import sys | |
with open(sys.argv[1], 'r') as f: | |
data = json.load(f) | |
for m in data: | |
print(m['text'].encode('utf8')) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment