async searchTweets()

in source/lambda/ingestion-producer/util/twitter-client.js [39:75]


    async searchTweets(tweetParams, trackerID) {
        const twit = await this.init();

        tweetParams.since_id = await this.feedTracker.getIDFromTracker(trackerID);
        console.debug(`Tweet search parameters is ${JSON.stringify(tweetParams)}`);

        let response = null;
        try {
            response = await twit.get("search/tweets", tweetParams);
            console.debug(`HTTP Status Code: ${response._headers.get('status')} Rate: ${response._headers.get('x-rate-limit-remaining')} / ${response._headers.get('x-rate-limit-limit')}`);
        } catch (error) {
            if ('errors' in error) {
                // Twitter API errors
                if (error.errors[0].code === 88) {
                    console.warn(`Rate limit will reset on: ${new Date(error._headers.get("x-rate-limit-reset") * 1000)}`);
                    console.warn(`The headers from the API call are: ${JSON.stringify(error._headers)}`);
                } else {
                    console.error('Twitter API throw error: ', error);
                }
                throw error;
            } else {
                console.error('Generic error when calling Twitter API: ', error);
                throw error;
            }
        }

        const jsonData = response.statuses;
        console.debug(`Received ${jsonData.length} with the following query: ${response.search_metadata.query}`);

        // if max_id_str is undefined that means there are no further tweets available and hence tracker is not updated
        // max_id_str is a pagination counter for the search query
        if (response.search_metadata.max_id_str !== undefined) {
            await this.feedTracker.updateTracker(response.search_metadata, jsonData.length, trackerID);
        }

        return jsonData;
    }