LoginSignup
9
7

More than 5 years have passed since last update.

Gmailで検索して絞り込んだメール群のメール本文をまとめて出力する

Last updated at Posted at 2015-12-16

はじめに

GmailのLabsにあったgmailのドキュメント出力機能がなくなってしまったため、Google AppsのGmailAPIを使ってpythonで簡単に書いてみた。

準備

以下のドキュメント(英語)を読んで、client_secret.jsonを入手しよう。

アプリケーションを定義して、ダウンロードするだけなので、1分くらいでできる。

インストール

pipでapi clientをインストール。

$ pip install --upgrade google-api-python-client

サンプルコード

上記のアドレスの例だと、ラベルの出力で終わってしまうので、メッセージ本文を手に入れるコードを追記、以上。

sample.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# from __future__ import print_function
import httplib2
import os
import base64
import logging
import traceback

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'


def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(
        credential_dir,
        'gmail-python-quickstart.json'
    )

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:  # Needed only for compatibility with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials


def extract_message(service, message):
    try:
        msg_obj = service.users().messages().get(userId='me', id=message['id'], format='raw').execute()
        return base64.urlsafe_b64decode(msg_obj['raw'].encode('ASCII'))
    except:
        logging.error(traceback.format_exc())


def extract_message_body(service, message):
    try:
        email_str = extract_message(service, message)
        subject_idx = email_str.find('Subject:')
        body_idx = email_str[subject_idx:].find(os.linesep)
        return email_str[subject_idx + body_idx:].strip()
    except:
        logging.error(traceback.format_exc())


def main():
    """Shows basic usage of the Gmail API.

    Creates a Gmail API service object and outputs a list of label names
    of the user's Gmail account.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)

    results = service.users().messages().list(userId='me', q="").execute()
    print results
    messages = results.get('messages', [])

    if not messages:
        print 'No messages found.'
    else:
        print 'messages:'
        for message in messages:
            print 'message body: ', extract_message_body(service, message)


if __name__ == '__main__':
    main()

検索キーワードは、list関数のqに文字列で放り込む。書式はgmailの検索と同じ。

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7