Get 1000 posts in Python order from all Slack channels and put them together in a txt file

Why did you decide to do it

I wanted data for morphological analysis. There was no choice.

procedure

First, get the Token of slack. I think this article will be helpful. https://qiita.com/ykhirao/items/0d6b9f4a0cc626884dbb

code

Since it is organized on github, there is no problem if you see here for the basics, pull it, install the necessary library and execute it. A cohesive output.txt will appear. https://github.com/hiwatee/get-txt-slack-python

import os
import requests
from os.path import join, dirname
from dotenv import load_dotenv


def main():
    # .Read environment variables from env
    dotenv_path = join(dirname(__file__), '.env')
    load_dotenv(dotenv_path)
    token = os.environ.get("TOKEN")
    host = os.environ.get("HOST")
    #Get / format channel list
    url = host + 'channels.list?token=' + token + '&exclude_archived=true'
    r = requests.get(url)
    data = r.json()
    channels = [{'id': channel['id'], 'name': channel['name']}
                for channel in data['channels']]
    for channel in channels:
        #Get the top 1000 from each channel
        url = host + 'channels.history?token=' + \
            token + '&channel=' + channel['id'] + '&count=1000'
        r = requests.get(url)
        data = r.json()
        #Write to file
        with open(dirname(__file__) + 'output.txt', 'a') as f:
            for message in data['messages']:
                print(message['text'], file=f)


if __name__ == '__main__':
    main()

More detailed explanation

Python-dotenv is required for actual use. The intention of using it is to reuse environment variables in .env to make it easier to manage permissions and to prevent my Token from leaking to git.

python-dotenv pip install python-dotenv It can be installed with.

Please rewrite the sample writing method as .env.txt.

By the way

The extracted txt file contains reactions, bot messages, and reactions and cannot be used for morphological analysis as it is. Please clean and use by yourself.

Recommended Posts

Get 1000 posts in Python order from all Slack channels and put them together in a txt file
Get 1000 posts in order of posting from all Slack channels and rank emoji reactions
Get a participant's username and screen name in Slack
[Python] Start a batch file from Python and pass variables.
Get the MIME type in Python and determine the file format
Get macro constants from C (++) header file (.h) in Python
Get all xlsx filenames in a folder in a natural sort order list and build an absolute path
Post a message from IBM Cloud Functions to Slack in Python
How to get a string from a command line argument in python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Read a file in Python with a relative path from the program
Get the formula in an excel file as a string in Python
Get OCTA simulation conditions from a file and save with pandas
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
Get data from Quandl in Python
Create a binary file in Python
A script that retrieves tweets with Python, saves them in an external file, and performs morphological analysis.
Replace the directory name and the file name in the directory together with a Linux command.
Tips: [Python] Randomly restore and extract an array from a fasta file
Read a Python # .txt file for a super beginner in Python with a working .py
How to get a value from a parameter store in lambda (using python)
How to put a half-width space before letters and numbers in Python.
Connect to postgreSQL from Python and use stored procedures in a loop.
Get options in Python from both JSON files and command line arguments
Get exchange rates from open exchange rates in Python
Send a message from Python to Slack
Write O_SYNC file in C and Python
How to get a stacktrace in python
Get battery level from SwitchBot in Python
Get a token for conoha in python
Create a deb file from a python package
[GPS] Create a kml file in Python
Generate a class from a string in Python
Get Precipitation Probability from XML in Python
Get metric history from MLflow in Python
Create a Python image in Django without a dummy image file and test the image upload
Put Docker in Windows Home and run a simple web server with Python
How to put a lot of pipelines together and put them away at once
Python --Read data from a numeric data file and find the multiple regression line.
How to drop Google Docs in one folder in a .txt file with python
Get all standard inputs used in paiza and competitive programming with int (python)
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
I want to get the file name, line number, and function name in Python 3.4