zulip / python-zulip-api

Python library for the Zulip API.
https://zulip.com/api/
Apache License 2.0
361 stars 362 forks source link

zulip/examples: Add a script to get complete history of a narrow. #501

Closed SivagiriVisakan closed 5 years ago

SivagiriVisakan commented 5 years ago

This PR adds a script that can download the history of a stream or a topic from Zulip.

$ ./get-history --help
usage:
Gets the complete history of a particular stream or a particular topic in Zulip
and store them in JSON format.

    get-history --stream <stream_name> [--topic <topic_name> --filename <file_to_store_messages>]

Example: get-history --stream announce --topic important

optional arguments:
  -h, --help            show this help message and exit
  --stream STREAM       The stream name to get the history
  --topic TOPIC         The topic name to get the history
  --filename FILENAME   The file name to store the fetched history. Default
                        'history.json'

Written JSON file format :

[
    {
        "content": "I am interested in working on #P476.\n\nIf I understand it correctly, the script should be able to download the complete history of any narrow(eg. : `stream:integrations sender:iago@zulip.com topic:test`)  and not just the `stream:stream-name` narrow, right ?",
        "client": "website",
        "stream_id": 127,
        "sender_realm_str": "",
        "flags": [
            "read"
        ],
        "id": 689524,
        "content_type": "text/x-markdown",
        "sender_id": 3893,
        "subject_links": [
            "https://github.com/zulip/python-zulip-api/pull/476"
        ],
        "recipient_id": 2643,
        "subject": "#P476",
        "display_recipient": "integrations",
        "sender_short_name": "siva.g.visakan",
        "avatar_url": "/user_avatars/2/786c599efd61bcb402d57e96488bd84d445d5aa6.png?x=x&version=2",
        "is_me_message": false,
        "sender_full_name": "Sivagiri Visakan",
        "type": "stream",
        "submessages": [],
        "sender_email": "siva.g.visakan@gmail.com",
        "reactions": [],
        "timestamp": 1548507562
    },
    {
        "client": "ZulipMobile",
        "stream_id": 127,
        "sender_realm_str": "",
        "flags": [
            "read"
        ],
        "content": "Yes I think so",
        "id": 689586,
        "content_type": "text/x-markdown",
        "sender_id": 58,
        "subject_links": [
            "https://github.com/zulip/python-zulip-api/pull/476"
        ],
        "sender_email": "showell@zulipchat.com",
        "subject": "#P476",
        "display_recipient": "integrations",
        "sender_short_name": "showell30",
        "avatar_url": "/user_avatars/2/0f93cc1fd329b3c4d1636c9ff168c524c66023b5.png?x=x&version=4",
        "is_me_message": false,
        "sender_full_name": "Steve Howell",
        "type": "stream",
        "submessages": [],
        "recipient_id": 2643,
        "reactions": [
            {
                "reaction_type": "unicode_emoji",
                "user": {
                    "full_name": "Sivagiri Visakan",
                    "email": "siva.g.visakan@gmail.com",
                    "id": 3893
                },
                "emoji_name": "+1",
                "emoji_code": "1f44d"
            }
        ],
        "timestamp": 1548522416
    }
]

Fixes #476.

eeshangarg commented 5 years ago

@SivagiriVisakan: Thanks for working on this! Looks good to me! Merging! :)

timabbott commented 5 years ago

We should add documentation for this on /api in zulip/zulip. Maybe we can put it in the "terminal command" tab of the GET /messages page?