<commit_before><?php

namespace MV\ForumParserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Goutte\Client;

class MainController extends Controller
{
    /**
     * @var string
     */
    private $_url;

    /**
     * @var Client
     */
    private $client;

    public function __construct(){
        $this->client = new Client();
    }

    /**
     * Devuelve la lista de foros de MV
     *
     * @return JsonResponse
     */
    public function getForumsAction()
    {
        $mv = array();

        $data = $this->_getHtml();
        $data->filter('li')->each(function(Crawler $headers, $i) use (&$countForums, &$title, &$mv) {
                if($headers->attr('data-role')) {
                    $title = $headers->text();
                    $mv[$title] = '';
                    $countForums = 0;
                } else {
                    $mv[$title][$countForums]['title'] = trim($headers->text());
                    $mv[$title][$countForums]['url'] = $headers->filter('a')->first()->link()->getUri();
                    $countForums++;
                }
            });

        $json = new JsonResponse($mv);
        $json->setEncodingOptions(128);
        return $json;
    }

    /**
     * Devuelve los topics del foro seleccionado
     *
     * @param $slug_forum string
     * @return JsonResponse
     */
    public function getTopicsAction($slug_forum, $page)
    {
        $posts = array();
        $page = 'p' . $page;

        $data = $this->_getGlobalHtml($slug_forum, $page);
        $data->filter('li')->each(function(Crawler $post, $i) use (&$posts) {
                // $href = array_filter(explode('/', $post->filter('a')->attr('href')));
                $resp = $post->filter('a span')->text();

                $posts[$i]['topic'] = trim($post->filter('a')->attr('title'));
                $posts[$i]['href']  = $post->filter('a')->attr('href');
                $posts[$i]['resp']  = $resp;
                $posts[$i]['pages'] = (($resp - $resp % 30) / 30) + 1;
            });

        $json = new JsonResponse($posts);
        $json->setEncodingOptions(128);
        return $json;
    }

    public function getPostsAction($cat, $slug_post, $page)
    {
        $posts = array();



        $json = new JsonResponse($posts);
        $json->setEncodingOptions(128);
        return $json;
    }
    /**
     * Devuelve la URL de MediaVida configurada en el config.yml del bundle
     *
     * @return string
     */
    private function _getMvUrl($foro = '')
    {
        return $this->container->getParameter('mv.forum_url') . $foro;
    }

    /**
     * Devuelve el crawler contenedor de la parte de foros de MV
     *
     * @return \Symfony\Component\DomCrawler\Crawler
     */
    private function _getGlobalHtml($slug = '', $page = '')
    {
        $this->_url = $this->_getMvUrl('/foro/');
        $crawler = $this->client->request('GET', $this->_url . $slug . '/' . $page);
        return $crawler->filter('ul[data-role="listview"]');
    }
}<commit_msg>more refactor<commit_after><?php

namespace MV\ForumParserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Goutte\Client;

class MainController extends Controller
{
    /**
     * @var string
     */
    private $_url = 'http://m.mediavida.com/foro/';

    /**
     * @var Client
     */
    private $client;

    public function __construct(){
        $this->client = new Client();
    }

    /**
     * Devuelve la lista de foros de MV
     *
     * @return JsonResponse
     */
    public function getForumsAction()
    {
        $mv = array();

        $data = $this->_getHtml();
        $data->filter('li')->each(function(Crawler $headers, $i) use (&$countForums, &$title, &$mv) {
                if($headers->attr('data-role')) {
                    $title = $headers->text();
                    $mv[$title] = '';
                    $countForums = 0;
                } else {
                    $mv[$title][$countForums]['title'] = trim($headers->text());
                    $mv[$title][$countForums]['url'] = $headers->filter('a')->first()->link()->getUri();
                    $countForums++;
                }
            });

        $json = new JsonResponse($mv);
        $json->setEncodingOptions(128);
        return $json;
    }

    /**
     * Devuelve los topics del foro seleccionado
     *
     * @param $slug_forum string
     * @return JsonResponse
     */
    public function getTopicsAction($slug_forum, $page)
    {
        $posts = array();
        $page = 'p' . $page;

        $data = $this->_getHtml($slug_forum, $page);
        $data->filter('li')->each(function(Crawler $post, $i) use (&$posts) {
                // $href = array_filter(explode('/', $post->filter('a')->attr('href')));
                $resp = $post->filter('a span')->text();

                $posts[$i]['topic'] = trim($post->filter('a')->attr('title'));
                $posts[$i]['href']  = $post->filter('a')->attr('href');
                $posts[$i]['resp']  = $resp;
                $posts[$i]['pages'] = (($resp - $resp % 30) / 30) + 1;
            });

        $json = new JsonResponse($posts);
        $json->setEncodingOptions(128);
        return $json;
    }

    public function getPostsAction($cat, $slug_post, $page)
    {
        $posts = array();



        $json = new JsonResponse($posts);
        $json->setEncodingOptions(128);
        return $json;
    }

    /**
     * Devuelve el crawler contenedor de la parte de foros de MV
     *
     * @return \Symfony\Component\DomCrawler\Crawler
     */
    private function _getHtml($slug = '', $page = '')
    {
        $url = $this->_url . $slug . '/' . $page;
        $crawler = $this->client->request('GET', $url);
        return $crawler->filter('ul[data-role="listview"]');
    }
}
{'fraction_non_alphanumeric': 0.17531510885578655, 'fraction_numerical': 0.004910787362907186, 'mean_word_length': 2.0458624127617147, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '1082828', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1838, 'n_words': 347}