<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
#= Imports ====================================================================
import time
from string import Template

import requests
import dhtmlparser as d

from concept import Concept, check_error_div
from blogpost import Blogpost, Rating


#= Variables ==================================================================
ABCLINUXU_URL = "https://www.abclinuxu.cz"
PROFILE_URL = ABCLINUXU_URL + "/lide/$USERNAME"
BASE_URL = "/?from=$COUNTER"
LOGIN_URL = ABCLINUXU_URL + "/Profile"

STEP = 50  # sets how much blogpost can be at one page


#= Functions & objects ========================================================
class User(object):
    def __init__(self, username, password=<PASSWORD>):
        self.username = username
        self.password = password
        self.logged_in = False

        self.session = requests.Session()
        self.blog_url = ABCLINUXU_URL + self._parse_blogname()

    def _parse_blogname(self):
        data = self._get(
            Template(PROFILE_URL).substitute(USERNAME=self.username)
        )

        dom = d.parseString(data)
        blogname = filter(
            lambda x: x.getContent().strip().startswith("Můj blog: "),
            dom.find("h2")
        )

        if not blogname:
            raise UserWarning("This user doesn't have blog!")

        href = blogname[0].find("a")

        if not href:
            raise UserWarning("Can't find blog link!")

        return href[0].params["href"]

    def _get(self, url, params=None, as_text=True):
        """
        Shortcut for ``self.session.get().text.encode("utf-8")``.

        Args:
            url (str): Url on which the GET request will be sent.
            params (dict): GET parameters.
            as_text (bool, default True): Return result as text or binary data.

        Returns:
            str/binary data: depending on the `as_text` parameter.
        """
        data = self.session.get(url, params=params)
        return data.text.encode("utf-8") if as_text else data.content

    def _parse_timestamp(self, meta):
        """
        Parse numeric timestamp from the date representation.

        Args:
            meta (str): Meta html from the blogpost body.

        Returns:
            int: Timestamp.
        """
        date = filter(
            lambda x: ":" in x and "." in x,
            str(meta).splitlines()
        )
        date = date[0].strip()

        if len(date) == 11:  # new blogs are without year
            date = date.replace(". ", ".%d " % time.localtime().tm_year)

        return time.mktime(time.strptime(date, "%d.%m.%Y %H:%M"))

    def _parse_comments_n(self, meta):
        """
        Parse number of comments under the blogpost.

        Args:
            meta (str): Meta html from the blogpost body.

        Returns:
            int: Number of comments.
        """
        comments = meta.find("a")[-1].getContent()
        comments = comments.split("&nbsp;")[1]

        return int(comments)

    def _parse_rating(self, meta):
        """
        Parse rating of the blogpost.<commit_msg>Fixed bug in blog timestamp parsing.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Interpreter version: python 2.7
#
#= Imports ====================================================================
import time
from string import Template

import requests
import dhtmlparser as d

from concept import Concept, check_error_div
from blogpost import Blogpost, Rating


#= Variables ==================================================================
ABCLINUXU_URL = "https://www.abclinuxu.cz"
PROFILE_URL = ABCLINUXU_URL + "/lide/$USERNAME"
BASE_URL = "/?from=$COUNTER"
LOGIN_URL = ABCLINUXU_URL + "/Profile"

STEP = 50  # sets how much blogpost can be at one page


#= Functions & objects ========================================================
class User(object):
    def __init__(self, username, password=<PASSWORD>):
        self.username = username
        self.password = password
        self.logged_in = False

        self.session = requests.Session()
        self.blog_url = ABCLINUXU_URL + self._parse_blogname()

    def _parse_blogname(self):
        data = self._get(
            Template(PROFILE_URL).substitute(USERNAME=self.username)
        )

        dom = d.parseString(data)
        blogname = filter(
            lambda x: x.getContent().strip().startswith("Můj blog: "),
            dom.find("h2")
        )

        if not blogname:
            raise UserWarning("This user doesn't have blog!")

        href = blogname[0].find("a")

        if not href:
            raise UserWarning("Can't find blog link!")

        return href[0].params["href"]

    def _get(self, url, params=None, as_text=True):
        """
        Shortcut for ``self.session.get().text.encode("utf-8")``.

        Args:
            url (str): Url on which the GET request will be sent.
            params (dict): GET parameters.
            as_text (bool, default True): Return result as text or binary data.

        Returns:
            str/binary data: depending on the `as_text` parameter.
        """
        data = self.session.get(url, params=params)
        return data.text.encode("utf-8") if as_text else data.content

    def _parse_timestamp(self, meta):
        """
        Parse numeric timestamp from the date representation.

        Args:
            meta (str): Meta html from the blogpost body.

        Returns:
            int: Timestamp.
        """
        date = filter(
            lambda x: ":" in x and "." in x,
            str(meta).splitlines()
        )
        date = date[0].strip()

        if len(date) <= 11:  # new blogs are without year
            date = date.replace(". ", ".%d " % time.localtime().tm_year)

        return time.mktime(time.strptime(date, "%d.%m.%Y %H:%M"))

    def _parse_comments_n(self, meta):
        """
        Parse number of comments under the blogpost.

        Args:
            meta (str): Meta html from the blogpost body.

        Returns:
            int: Number of comments.
        """
        comments = meta.find("a")[-1].getContent()
        comments = comments.split("&nbsp;")[1]

        return int(comments)

    def _parse_rating(self, meta):
        """
        Parse rating of the blogpost.
{'fraction_non_alphanumeric': 0.17157712305025996, 'fraction_numerical': 0.004726642508271624, 'mean_word_length': 2.3322834645669293, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '6399168', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1758, 'n_words': 500}