<commit_before><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Twitter;
use App\Helpers\Helper;
use Exception;

class TwitterController extends Controller
{
    /**
     * Retrieves tweets from the specified user.
     *
     * @param  string  $name   Twitter username
     * @param  boolean $no_rts 'true' excludes retweets, 'false' includes them.
     * @return array
     */
    private function getTweets($name = null, $no_rts = true, $exclude_replies = true)
    {
        $retweets = $no_rts ? 'false' : 'true';
        $exclude_replies = $exclude_replies ? 'true' : 'false';

        $tweet = Twitter::getUserTimeline([
            'screen_name' => $name,
            'count' => 200,
            'exclude_replies' => 'true',
            'include_rts' => $retweets
        ]);

        return $tweet;
    }

    /**
     * Fetches the latest tweet for the specified user
     *
     * @param  Request $request
     * @param  string  $latest  Route: Can be latest(.php) or latest_url(.php)
     * @param  string  $name    Twitter username
     * @return Response
     */
    public function latest(Request $request, $latest = null, $name = null)
    {
        $name = $name ?: $request->input('name', null);

        if (empty($name)) {
            return Helper::text('You have to specify a (user)name.');
        }

        /**
         * Route: /twitter/latest_url
         *
         * @var boolean
         */
        $onlyUrl = (strpos($latest, 'latest_url') === false ? false : true);

        /**
         * Route: /twitter/latest_id
         *
         * @var boolean
         */
        $onlyId = (strpos($latest, 'latest_id') === false ? false : true);

        try {
            $tweets = $this->getTweets($name, $request->exists('no_rts'));

            if (empty($tweets)) {
                return Helper::text('No tweets were found for this user.');
            }

            $first = $tweets[0];
            $text = [];
            if ($onlyUrl === false) {
                $text[] = str_replace(PHP_EOL, ' ', htmlspecialchars_decode($first->text));
            }

            if ($request->exists('url') || $onlyUrl) {
                $link = Twitter::linkTweet($first);

                if ($request->exists('shorten')) {
                    $link = Helper::get('http://tinyurl.com/api-create.php?url=' . $link, [
                        'User-Agent' => env('SITE_TITLE') . '/Twitter'
                    ], false);
                }

                $text[] = $link;
            }

            if ($request->exists('howlong')) {
                $precision = 4;
                if ($request->has('precision')) {
                    $precision = intval($request->input('precision'));
                }

                $text[] = Helper::getDateDiff($first->created_at, time(), $precision) . " ago";
            }

            if ($onlyId === true) {
                $text = [$first->id];
            }

            return Helper::text(implode(' - ', $text));
        } catch (Exception $e) {
            if ($e->getCode() === 401) {
                return Helper::text('Not authorized (Normally this means locked/private account)');
            }

            return Helper::text('An error has occurred: ' . trim($e->getMessage()));
        }
    }
}<commit_msg>Add support for "search" query parameter (as well as "strict" for case-sensitive search) to /twitter/latest<commit_after><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Twitter;
use App\Helpers\Helper;
use Exception;

class TwitterController extends Controller
{
    /**
     * Retrieves tweets from the specified user.
     *
     * @param  string  $name            Twitter username
     * @param  boolean $no_rts          'true' excludes retweets, 'false' includes them.
     * @param  boolean $exclude_replies Exclude replies or not, default: true.
     * @return array
     */
    private function getTweets($name = null, $no_rts = true, $exclude_replies = true)
    {
        $retweets = $no_rts ? 'false' : 'true';
        $exclude_replies = $exclude_replies ? 'true' : 'false';

        $tweet = Twitter::getUserTimeline([
            'screen_name' => $name,
            'count' => 200,
            'exclude_replies' => 'true',
            'include_rts' => $retweets
        ]);

        return $tweet;
    }

    /**
     * Fetches the latest tweet for the specified user
     *
     * @param  Request $request
     * @param  string  $latest  Route: Can be latest(.php) or latest_url(.php)
     * @param  string  $name    Twitter username
     * @return Response
     */
    public function latest(Request $request, $latest = null, $name = null)
    {
        $name = $name ?: $request->input('name', null);
        $search = $request->input('search', null);

        if (empty($name)) {
            return Helper::text('You have to specify a (user)name.');
        }

        /**
         * Route: /twitter/latest_url
         *
         * @var boolean
         */
        $onlyUrl = (strpos($latest, 'latest_url') === false ? false : true);

        /**
         * Route: /twitter/latest_id
         *
         * @var boolean
         */
        $onlyId = (strpos($latest, 'latest_id') === false ? false : true);

        try {
            $tweets = $this->getTweets($name, $request->exists('no_rts'));

            if (empty($tweets)) {
                return Helper::text('No tweets were found for this user.');
            }

            if (!empty($search)) {
                $search = urldecode($search);
                $strict = $request->exists('strict');

                if ($strict === false) {
                    $search = strtolower($search);
                }

                foreach ($tweets as $current) {
                    $text = htmlspecialchars_decode($current->text);

                    if ($strict === false) {
                        $text = strtolower($text);
                    }

                    if (strpos($text, $search) !== false) {
                        $tweet = $current;
                        break;
                    }
                }

                if (!isset($tweet)) {
                    throw new Exception('No tweets found based on the search query.');
                }
            } else {
                $tweet = $tweets[0];
            }

            $text = [];
            if ($onlyUrl === false) {
                $text[] = str_replace(PHP_EOL, ' ', htmlspecialchars_decode($tweet->text));
            }

            /**
             * Appends the tweet URL to the tweet,
             * or displays only the URL, depending on
             * the query string parameters
             */
            if ($request->exists('url') || $onlyUrl) {
                $link = Twitter::linkTweet($tweet);

                /**
                 * Shortens the URL using TinyURL
                 */
                if ($request->exists('shorten')) {
                    $link = Helper::get('http://tinyurl.com/api-create.php?url=' . $link, [
                        'User-Agent' => env('SITE_TITLE') . '/Twitter'
                    ], false);
                }

                $text[] = $link;
            }

            /**
             * Appends length of time since tweet was posted.
             */
            if ($request->exists('howlong')) {
                $precision = 4;
                if ($request->has('precision')) {
                    $precision = intval($request->input('precision'));
                }

                $text[] = Helper::getDateDiff($tweet->created_at, time(), $precision) . " ago";
            }

            /**
             * Returns just the tweet ID.
             */
            if ($onlyId === true) {
                $text = [$tweet->id];
            }

            return Helper::text(implode(' - ', $text));
        } catch (Exception $e) {
            if ($e->getCode() === 401) {
                return Helper::text('Not authorized (Normally this means locked/private account)');
            }

            return Helper::text('[Error] - ' . trim($e->getMessage()));
        }
    }
}
{'fraction_non_alphanumeric': 0.1426824795207238, 'fraction_numerical': 0.0019562293678933853, 'mean_word_length': 1.5161488772685328, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, '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': '2222914', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2240, 'n_words': 531}