<commit_before><?php namespace App\Console\Commands;

use App\Items;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class ConvertToSpeech extends Command {


    const TTS_URI = "https://stream.watsonplatform.net/text-to-speech-beta/api/";

    const FILENAME_FORMAT_OGG = "%s.ogg";
    const FILENAME_FORMAT_MP3 = "%s.mp3";

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'articles:convert';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Convert article text to ogg files using IBM Watson.';

    /**
     * @var \GuzzleHttp\Client $httpClient
     */
    protected $httpClient;

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        $items_to_convert = Items::where('status', '=', Items::STATUS_FETCHED)
            ->chunk(100, function ($items) {
                $filesystem = Storage::disk("local");

                foreach ($items as $item) {
                    $item->status = Items::STATUS_BEING_CONVERTED;
                    $item->save();

                    $this->info(sprintf("Converting content of '%s' to speech...", $item->url));

                    try {
                        $response = $this->getHttpClient()->post("v1/synthesize", array(
                            "auth" => array(
                                env('WATSON_TTS_USERNAME'),
                                env('WATSON_TTS_PASSWORD')
                            ),
                            "headers" => array(
                                'Content-Type'  => 'application/json',
                                'Accept'        => 'audio/ogg; codecs=opus'
                            ),
                            "body" => json_encode(
                                array(
                                    "text" => $item->content
                                )
                            )
                        ));

                    } catch (ClientException $e) {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error($e->getMessage());
                        continue;
                    }


                    if (substr($response->getStatusCode(), 0, 1) != "2") {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error('Invalid response from watson TTS API!');
                        continue;
                    }


                    // Save successful response
                    $filenameOgg = sprintf(static::FILENAME_FORMAT_OGG, $item->id);
                    $filenameMp3 = sprintf(static::FILENAME_FORMAT_MP3, $item->id);

                    $filesystem->put(
                        $filenameOgg,
                        $response->getBody()
                    );

                    $this->info('Downloaded, converting ogg to mp3...');

                    $output = '';
                    $returnCode = -1;
                    $origPath = storage_path() . "/app/$filenameOgg";
                    $finalPath = storage_path() . "/app/$filenameMp3";

                    exec("ffmpeg -y -i $origPath -acodec libmp3lame $finalPath 2> /dev/null", $output, $returnCode);

                    if ($returnCode !== 0) {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error("Error converting $filenameOgg to $filenameMp3");
                        continue;
                    }

                    // Remove original ogg file
                    $filesystem->delete($filenameOgg);

                    $item->status = Items::STATUS_CONVERTED;
                    $item->save();

                    $this->info(sprintf("Done (%s).", $filenameMp3));

                }
            });
	}

	/**
	 * Get the console command arguments.
	 *
	 * @return array
	 */
	protected function getArguments()
	{
		return [];
	}

	/**<commit_msg>Change from using ffmpeg to avconv.<commit_after><?php namespace App\Console\Commands;

use App\Items;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class ConvertToSpeech extends Command {


    const TTS_URI = "https://stream.watsonplatform.net/text-to-speech-beta/api/";

    const FILENAME_FORMAT_OGG = "%s.ogg";
    const FILENAME_FORMAT_MP3 = "%s.mp3";

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $name = 'articles:convert';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Convert article text to ogg files using IBM Watson.';

    /**
     * @var \GuzzleHttp\Client $httpClient
     */
    protected $httpClient;

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function fire()
	{
        $items_to_convert = Items::where('status', '=', Items::STATUS_FETCHED)
            ->chunk(100, function ($items) {
                $filesystem = Storage::disk("local");

                foreach ($items as $item) {
                    $item->status = Items::STATUS_BEING_CONVERTED;
                    $item->save();

                    $this->info(sprintf("Converting content of '%s' to speech...", $item->url));

                    try {
                        $response = $this->getHttpClient()->post("v1/synthesize", array(
                            "auth" => array(
                                env('WATSON_TTS_USERNAME'),
                                env('WATSON_TTS_PASSWORD')
                            ),
                            "headers" => array(
                                'Content-Type'  => 'application/json',
                                'Accept'        => 'audio/ogg; codecs=opus'
                            ),
                            "body" => json_encode(
                                array(
                                    "text" => $item->content
                                )
                            )
                        ));

                    } catch (ClientException $e) {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error($e->getMessage());
                        continue;
                    }


                    if (substr($response->getStatusCode(), 0, 1) != "2") {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error('Invalid response from watson TTS API!');
                        continue;
                    }


                    // Save successful response
                    $filenameOgg = sprintf(static::FILENAME_FORMAT_OGG, $item->id);
                    $filenameMp3 = sprintf(static::FILENAME_FORMAT_MP3, $item->id);

                    $filesystem->put(
                        $filenameOgg,
                        $response->getBody()
                    );

                    $this->info('Downloaded, converting ogg to mp3...');

                    $output = '';
                    $returnCode = -1;
                    $origPath = storage_path() . "/app/$filenameOgg";
                    $finalPath = storage_path() . "/app/$filenameMp3";

                    exec("avconv -y -i $origPath -acodec libmp3lame $finalPath 2> /dev/null", $output, $returnCode);

                    if ($returnCode !== 0) {
                        $item->status = Items::STATUS_CONVERSION_FAILED;
                        $item->save();
                        $this->error("Error converting $filenameOgg to $filenameMp3");
                        continue;
                    }

                    // Remove original ogg file
                    $filesystem->delete($filenameOgg);

                    $item->status = Items::STATUS_CONVERTED;
                    $item->save();

                    $this->info(sprintf("Done (%s).", $filenameMp3));

                }
            });
	}

	/**
	 * Get the console command arguments.
	 *
	 * @return array
	 */
	protected function getArguments()
	{
		return [];
	}

	/**
{'fraction_non_alphanumeric': 0.12418376865671642, 'fraction_numerical': 0.004430970149253731, 'mean_word_length': 1.2482306684141546, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1997445', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2103, 'n_words': 427}