<commit_before>import Dotenv from 'dotenv';
import goodGuyHttp from 'good-guy-http';
import url from 'url';

Dotenv.config();

// List: "DMIRS accounts"
// https://twitter.com/CommerceWA/lists/dmirs-accounts
const dmirsEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864326054462095361',
  },
});

// List: "Perth transport"
// https://twitter.com/CommerceWA/lists/perth-transport
const transportEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864392667689398272',
  },
});

if (!process.env.TWITTER_BEARER_TOKEN) {
  throw Error('Error: Twitter bearer token must be set in TWITTER_BEARER_TOKEN environment variable.');
}

const goodGuy = goodGuyHttp({
  defaultCaching: {
    timeToLive: 60000,
  },
  headers: {
    Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
  },
  json: true,
  proxy: process.env.HTTP_PROXY,
  timeout: 15000,
});

// Transport related tweets from a list owned by @CommerceWA on Twitter
exports.getTweets = async function getTweets(request, reply) {
  let twitterEndpointUrl = '';
  if (request.params.list === 'dmirs') {
    twitterEndpointUrl = dmirsEndpointUrl;
  } else if (request.params.list === 'transport') {
    twitterEndpointUrl = transportEndpointUrl;
  }
  try {
    const response = await goodGuy(twitterEndpointUrl);
    const tweets = response.body;
    // console.dir(tweets);
    // console.log(`Number of tweets = ${tweets.length}`);
    reply(tweets);
  } catch (error) {
    console.log('Fetch of tweets failed', error);
  }
};<commit_msg>Handle missing Twitter configuration better<commit_after>import Dotenv from 'dotenv';
import goodGuyHttp from 'good-guy-http';
import url from 'url';

Dotenv.config();

// List: "DMIRS accounts"
// https://twitter.com/CommerceWA/lists/dmirs-accounts
const dmirsEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864326054462095361',
  },
});

// List: "Perth transport"
// https://twitter.com/CommerceWA/lists/perth-transport
const transportEndpointUrl = url.format({
  protocol: 'https',
  hostname: 'api.twitter.com',
  pathname: '1.1/lists/statuses.json',
  query: {
    count: 50,
    include_rts: true,
    // If this is passed as a Number, JavaScript will "round it" and break the URL
    list_id: '864392667689398272',
  },
});

exports.getTweets = async function getTweets(request, reply) {
  if (!process.env.TWITTER_BEARER_TOKEN) {
    throw Error('Error: Twitter bearer token must be set in TWITTER_BEARER_TOKEN environment variable.');
  }

  const goodGuy = goodGuyHttp({
    defaultCaching: {
      timeToLive: 60000,
    },
    headers: {
      Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
    },
    json: true,
    proxy: process.env.HTTP_PROXY,
    timeout: 15000,
  });

  let twitterEndpointUrl = '';
  if (request.params.list === 'dmirs') {
    twitterEndpointUrl = dmirsEndpointUrl;
  } else if (request.params.list === 'transport') {
    twitterEndpointUrl = transportEndpointUrl;
  }
  try {
    const response = await goodGuy(twitterEndpointUrl);
    const tweets = response.body;
    // console.dir(tweets);
    // console.log(`Number of tweets = ${tweets.length}`);
    reply(tweets);
  } catch (error) {
    console.log('Fetch of tweets failed', error);
  }
};
{'fraction_non_alphanumeric': 0.1353950103950104, 'fraction_numerical': 0.028066528066528068, 'mean_word_length': 3.6881851400730814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '734041', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1294, 'n_words': 319}