Created
April 5, 2019 14:17
-
-
Save brianly/89456d11dbaf73d42707af38752a80fc to your computer and use it in GitHub Desktop.
Use the Yammer REST API to dump new thread starters. Requires a modern version of Python 3.
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 | |
from dataclasses import dataclass | |
from time import sleep | |
TOKEN = '' | |
GROUP_ID = '15824708' | |
@dataclass | |
class YammerMessage: | |
id: int | |
thread_id: int | |
body_text: str | |
body_html: str | |
excerpt: str | |
web_url: str | |
def __str__(self): | |
return f'{self.id},{self.thread_id},{self.excerpt[0:30]}' | |
def get_html(self): | |
return f'<div id="message-{self.id}"><p><strong>Thread ID: <a href="{self.web_url}">{self.thread_id}<a></strong></p><p>{self.body_html}</p></div>' | |
def fetch_group_threadstarters(group_id, token, older_than=None): | |
request_url = f'https://www.yammer.com/api/v1/messages/in_group/{group_id}.json?threaded=true' | |
if older_than is not None: | |
request_url += f'&older_than={older_than}' | |
yammer_auth_header = {'Authorization': f'Bearer {token}'} | |
resp = requests.get(request_url, headers = yammer_auth_header) | |
messages = [] | |
for message in resp.json()['messages']: # messages not at the root of JSON | |
ym = YammerMessage(message['id'], | |
message['thread_id'], | |
message['body']['plain'], | |
message['body']['rich'], | |
message['content_excerpt'], | |
message['web_url']) | |
messages.append(ym) | |
return messages | |
def dump_messages(group_id, token, older_than=None): | |
messages = fetch_group_threadstarters(group_id, token, older_than=older_than) | |
for message in messages: | |
print(message.get_html()) | |
sleep(10) # respect the rate limits | |
return messages[-1].id | |
# First set of 20 | |
last_message_id = dump_messages(GROUP_ID, TOKEN) | |
# Next 20 | |
last_message_id = dump_messages(GROUP_ID, TOKEN, older_than=last_message_id) | |
# Next 20 | |
last_message_id = dump_messages(GROUP_ID, TOKEN, older_than=last_message_id) | |
# Next 20 | |
last_message_id = dump_messages(GROUP_ID, TOKEN, older_than=last_message_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment