<commit_before><?php

/*
 * This file is part of the Elcodi package.
 *
 * Copyright (c) 2014-2015 Elcodi.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Feel free to edit as you please, and have fun.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author Elcodi Team <<EMAIL>>
 */

namespace Elcodi\Component\Geo\Populator\Adapters;

use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\LexerConfig;
use Mmoreram\Extractor\Extractor;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;

use Elcodi\Component\Core\Factory\DateTimeFactory;
use Elcodi\Component\Geo\Entity\Interfaces\LocationInterface;
use Elcodi\Component\Geo\Populator\Interfaces\PopulatorInterface;
use Elcodi\Component\Geo\Services\LocationBuilder;

/**
 * Class GeonamesPopulatorAdapter
 *
 * @author <NAME> <<EMAIL>>
 */
class GeonamesPopulatorAdapter implements PopulatorInterface
{
    /**
     * @var Extractor
     *
     * Extractor
     */
    private $extractor;

    /**
     * @var LocationBuilder
     *
     * Location Builder
     */
    private $locationBuilder;

    /**
     * @var DateTimeFactory
     *
     * DateTime Factory
     */
    private $dateTimeFactory;

    /**
     * Constructor
     *
     * @param Extractor       $extractor       Extractor instance
     * @param LocationBuilder $locationBuilder Location builder
     * @param DateTimeFactory $dateTimeFactory DateTime Factory
     */
    public function __construct(
        Extractor $extractor,
        LocationBuilder $locationBuilder,
        DateTimeFactory $dateTimeFactory
    ) {
        $this->extractor = $extractor;
        $this->locationBuilder = $locationBuilder;
        $this->dateTimeFactory = $dateTimeFactory;
    }

    /**
     * Populate a country
     *
     * @param string          $countryCode Country Code
     * @param OutputInterface $output      Console Output
     *
     * @return LocationInterface[] Root locations
     */
    public function populate($countryCode, OutputInterface $output)
    {
        $countryCode = strtoupper($countryCode);
        $downloadedFilePath = $this->downloadFile($countryCode);
        $extractedPath = $this->extractFile($downloadedFilePath);

        $locations = $this
            ->loadLocationsFrom(
                $countryCode,
                $extractedPath,
                $output
            );

        return $locations;
    }

    /**
     * Download data file from country
     * Avoids download if the file exists
     *
     * @param string $countryCode Country code
     *
     * @return string
     */
    private function downloadFile($countryCode)
    {
        $tempname = sys_get_temp_dir() . '/elcodi-locator-geonames-' . $countryCode . '.zip';
        if (!file_exists($tempname)) {
            $dirname = dirname($tempname);
            if (!file_exists($dirname)) {
                mkdir($dirname, 0777, true);
            }
            $downloadUrl = 'http://download.geonames.org/export/zip/' . $countryCode . '.zip';
            copy($downloadUrl, $tempname);
        }

        return $tempname;
    }

    /**
     * Download file and return new pathname of extracted content
     *
     * The geoname's zipfile contains this structure:
     * - {countryCode}.txt
     * - readme.txt
     *
     * We're only interested in the first file.
     *
     * @param string $tempname temporary name for downloaded content
     *
     * @return string Pathname of the resulting file
     */<commit_msg>Fixed geonames location url<commit_after><?php

/*
 * This file is part of the Elcodi package.
 *
 * Copyright (c) 2014-2015 Elcodi.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Feel free to edit as you please, and have fun.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author Elcodi Team <<EMAIL>>
 */

namespace Elcodi\Component\Geo\Populator\Adapters;

use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\LexerConfig;
use Mmoreram\Extractor\Extractor;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;

use Elcodi\Component\Core\Factory\DateTimeFactory;
use Elcodi\Component\Geo\Entity\Interfaces\LocationInterface;
use Elcodi\Component\Geo\Populator\Interfaces\PopulatorInterface;
use Elcodi\Component\Geo\Services\LocationBuilder;

/**
 * Class GeonamesPopulatorAdapter
 *
 * @author <NAME> <<EMAIL>>
 */
class GeonamesPopulatorAdapter implements PopulatorInterface
{
    /**
     * @var Extractor
     *
     * Extractor
     */
    private $extractor;

    /**
     * @var LocationBuilder
     *
     * Location Builder
     */
    private $locationBuilder;

    /**
     * @var DateTimeFactory
     *
     * DateTime Factory
     */
    private $dateTimeFactory;

    /**
     * Constructor
     *
     * @param Extractor       $extractor       Extractor instance
     * @param LocationBuilder $locationBuilder Location builder
     * @param DateTimeFactory $dateTimeFactory DateTime Factory
     */
    public function __construct(
        Extractor $extractor,
        LocationBuilder $locationBuilder,
        DateTimeFactory $dateTimeFactory
    ) {
        $this->extractor = $extractor;
        $this->locationBuilder = $locationBuilder;
        $this->dateTimeFactory = $dateTimeFactory;
    }

    /**
     * Populate a country
     *
     * @param string          $countryCode Country Code
     * @param OutputInterface $output      Console Output
     *
     * @return LocationInterface[] Root locations
     */
    public function populate($countryCode, OutputInterface $output)
    {
        $countryCode = strtoupper($countryCode);
        $downloadedFilePath = $this->downloadFile($countryCode);
        $extractedPath = $this->extractFile($downloadedFilePath);

        $locations = $this
            ->loadLocationsFrom(
                $countryCode,
                $extractedPath,
                $output
            );

        return $locations;
    }

    /**
     * Download data file from country
     * Avoids download if the file exists
     *
     * @param string $countryCode Country code
     *
     * @return string
     */
    private function downloadFile($countryCode)
    {
        $tempname = sys_get_temp_dir() . '/elcodi-locator-geonames-' . $countryCode . '.zip';
        if (!file_exists($tempname)) {
            $dirname = dirname($tempname);
            if (!file_exists($dirname)) {
                mkdir($dirname, 0777, true);
            }
            $downloadUrl = sprintf(
                'http://download.geonames.org/dump/zip/%s.zip',
                strtoupper($countryCode)
            );
            copy($downloadUrl, $tempname);
        }

        return $tempname;
    }

    /**
     * Download file and return new pathname of extracted content
     *
     * The geoname's zipfile contains this structure:
     * - {countryCode}.txt
     * - readme.txt
     *
     * We're only interested in the first file.
     *
     * @param string $tempname temporary name for downloaded content
     *
     * @return string Pathname of the resulting file
     */
{'fraction_non_alphanumeric': 0.10267437172423062, 'fraction_numerical': 0.0032253729337454644, 'mean_word_length': 2.421609195402299, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013900', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2047, 'n_words': 501}