npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

juttle-gmail-adapter

v0.6.0

Published

Juttle adapter for Gmail

Downloads

10

Readme

Juttle Gmail Adapter

Gmail adapter for the Juttle data flow language.

This uses the nodejs API for google to read and write gmail messages. It also uses Batchelor to perform batched email fetches, something that the main google API does not currently support.

Examples

read gmail -from :5 days ago: -raw "to:me"
   | reduce count() by from
   | sort count -desc
   | view table -title "Who sends me the most mail?";
read gmail -from :5 days ago: -to :1 day ago: -raw "to:me"
   | batch -every :1h:
   | reduce count()
   | view timechart -title "When during the day do I get mail?"
read gmail -from :5 days ago: -to :1 day ago: -raw "to:me"
   | batch -every :1h:
   | reduce count()
   | write gmail -subject "When during the day do I get mail?"

An end-to-end example is described here and deployed to the demo system demo.juttle.io.

Installation

Like Juttle itself, the adapter is installed as a npm package. Both Juttle and the adapter need to be installed side-by-side:

$ npm install juttle
$ npm install juttle-gmail-adapter

Ecosystem

The juttle-gmail-adapter fits into the overall Juttle Ecosystem as one of the adapters in the below diagram:

Juttle Ecosystem

Configuration

Configuration involves these steps:

  1. Create application credentials that allow your code to access the google nodejs APIs.
  2. Authorize a user using Oauth2 to use the application to access gmail.
  3. Add the appropriate configuration items to .juttle/config.js

Create application credentials

To create application credentials, follow the instructions under Step 1: Turn on the Gmail API on the nodejs quickstart instructions page. (Steps 2-4 are not necessary--you only need step 1 to create the client api credentials). This will result in a file on disk titled client_secret.json with this structure:

{
  "installed": {
    "client_id": "--your-client-id--",
    "project_id": "--your-project-id",
    "auth_uri": "https:\/\/accounts.google.com\/o\/oauth2\/auth",
    "token_uri": "https:\/\/accounts.google.com\/o\/oauth2\/token",
    "auth_provider_x509_cert_url": "https:\/\/www.googleapis.com\/oauth2\/v1\/certs",
    "client_secret": "--your-client-secret-id--",
    "redirect_uris": [
      "urn:ietf:wg:oauth:2.0:oob",
      "http:\/\/localhost"
    ]
  }
}

You'll use this file in the next step.

Authorize a user using OAuth2

You need to create an oauth2 token that allows this program to read your email on your behalf.

To do this, run node create_oauth_token.js <path-to-client_secret.json>. create_oauth_token.js is in the top-level directory where juttle-gmail-adapter is installed (i.e. wherever you ran git clone for github, under node_modules/juttle-gmail-adapter for npm).

This will provide a json config block to add to your .juttle/config.js file.

This will also use the gmail nodejs api to read the list of labels assocated with the authenticated user, to verify that the token was created successfully.

Add the appropriate configuration items to .juttle/config.js

create_oauth_token.js printed a configuration block like this:

{
  "adapters": {
    "gmail": {
      "client-credentials": {
        "installed": {
          "client_id": "--your-client-id--",
          "project_id": "--your-project-id",
          "auth_uri": "https:\/\/accounts.google.com\/o\/oauth2\/auth",
          "token_uri": "https:\/\/accounts.google.com\/o\/oauth2\/token",
          "auth_provider_x509_cert_url": "https:\/\/www.googleapis.com\/oauth2\/v1\/certs",
          "client_secret": "--your-client-secret-id--",
          "redirect_uris": [
            "urn:ietf:wg:oauth:2.0:oob",
            "http:\/\/localhost"
          ]
        }
      },
      "oauth2-token": {
        "access_token": "---your-access-token---",
        "token_type": "Bearer",
        "refresh_token": "---your-refresh-token---",
        "expiry_date": DDDDDDDDDDDDD
      }
    }
  }
}

Add this configuration to your .juttle/config.js file. If you have an existing "adapters" section, for example:

{
  "adapters": {
    "twitter": {...}
  }
}

Add the gmail section as a peer item below "adapters":

{
  "adapters": {
    "twitter": {...},
    "gmail": {...}
  }
}

Usage

Read Options

Name | Type | Required | Description -----|------|----------|------------- raw | string | no | Use the following advanced search filter to select messages. from | moment | no | select messages after this time (inclusive) to | moment | no | select messages before this time (exclusive) last | duration | no | shorthand for -from :now: - -to :now: lag| duration| no | Controls how long to wait behind real time to fetch datapoints.

Write Options

Name | Type | Required | Description -----|------|----------|------------- to | string | no | the to: header of the message. If not specified, defaults to the email address of the authenticated user. subject | string | no | the subject of the message. Default is 'Juttle Program Output'. If output is split, " (part )" is appended to subject. limit | number | no | split output into batches of points. By default all points are buffered in memory until the program has completed. jsonOnly | boolean | no | if true, only include a raw JSON mime part in the email. The default (false) is to attach a plain/text as well as application/json part.

Detailed Walkthough

If you want to write your own adapter, look at the detailed notes on how the Gmail adapter interacts with the Juttle runtime/compiler to fetch messages and pass them as points to the juttle program.

Contributing

Want to contribute? Awesome! Don’t hesitate to file an issue or open a pull request.