<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
from pycurl import HTTPHEADER
from module.network.RequestFactory import getRequest, getURL
from module.network.HTTPRequest import BadHeader
from module.plugins.internal.SimpleHoster import SimpleHoster, parseFileInfo
from module.common.json_layer import json_loads


def checkFile(url):
    response = getURL("http://share-rapid.com/checkfiles.php", post={"files": url}, decode=True)
    info = json_loads(response)

    if "error" in info:
        if info['error'] == False:
            info['name'] = info['filename']
            info['status'] = 2
        elif info['msg'] == "Not found":
            info['status'] = 1  # offline
        elif info['msg'] == "Service Unavailable":
            info['status'] = 6  # temp.offline

    return info


def getInfo(urls):
    for url in urls:
        info = checkFile(url)
        if "filename" in info:
            yield info['name'], info['size'], info['status'], url
        else:
            file_info = (url, 0, 3, url)
            h = getRequest()
            try:
                h.c.setopt(HTTPHEADER, ["Accept: text/html"])
                html = h.load(url, cookies=True, decode=True)
                file_info = parseFileInfo(ShareRapidCom, url, html)
            finally:
                h.close()
                yield file_info


class ShareRapidCom(SimpleHoster):
    __name__ = "ShareRapidCom"
    __type__ = "hoster"
    __pattern__ = r"http://(?:www\.)?((share(-?rapid\.(biz|com|cz|info|eu|net|org|pl|sk)|-(central|credit|free|net)\.cz|-ms\.net)|(s-?rapid|rapids)\.(cz|sk))|(e-stahuj|mediatack|premium-rapidshare|rapidshare-premium|qiuck)\.cz|kadzet\.com|stahuj-zdarma\.eu|strelci\.net|universal-share\.com)/stahuj/(\w+)"
    __version__ = "0.52"
    __description__ = """Share-rapid.com plugin - premium only"""
    __author_name__ = ("MikyWoW", "zoidberg")
    __author_mail__ = ("<EMAIL>", "<EMAIL>")

    FILE_NAME_PATTERN = r'<h1[^>]*><span[^>]*>(?:<a[^>]*>)?(?P<N>[^<]+)'
    FILE_SIZE_PATTERN = r'<td class="i">Velikost:</td>\s*<td class="h"><strong>\s*(?P<S>[0-9.]+) (?P<U>[kKMG])i?B</strong></td>'
    FILE_OFFLINE_PATTERN = ur'Nastala chyba 404|Soubor byl smazán'

    DOWNLOAD_URL_PATTERN = r'<a href="([^"]+)" title="Stahnout">([^<]+)</a>'
    ERR_LOGIN_PATTERN = ur'<div class="error_div"><strong>Stahování je přístupné pouze přihlášeným uživatelům'
    ERR_CREDIT_PATTERN = ur'<div class="error_div"><strong>Stahování zdarma je možné jen přes náš'

    FILE_URL_REPLACEMENTS = [(__pattern__, r'http://share-rapid.com/stahuj/\1')]

    def setup(self):
        self.chunkLimit = 1
        self.resumeDownload = True

    def process(self, pyfile):
        if not self.account:
            self.fail("User not logged in")

        self.info = checkFile(pyfile.url)
        self.logDebug(self.info)

        pyfile.status = self.info['status']

        if pyfile.status == 2:
            pyfile.name = self.info['name']
            pyfile.size = self.info['size']
        elif pyfile.status == 1:
            self.offline()
        elif pyfile.status == 6:
            self.tempOffline()
        else:
            self.fail("Unexpected file status")

        url = "http://share-rapid.com/stahuj/%s" % self.info['filepath']
        try:
            self.html = self.load(url, decode=True)
        except BadHeader, e:
            self.account.relogin(self.user)
            self.retry(3, 0, str(e))

        found = re.search(self.DOWNLOAD_URL_PATTERN, self.html)
        if found is not None:
            link = found.group(1)
            self.logDebug("Premium link: %s" % link)

            self.check_data = {"size": pyfile.size}
            self.download(link)
        else:
            if re.search(self.ERR_LOGIN_PATTERN, self.html):
                self.relogin(self.user)
                self.retry(3, 0, "User login failed")
            elif re.search(self.ERR_CREDIT_PATTERN, self.html):
                self.fail("Not enough credit left")
            else:
                self.fail("Download link not found")           <commit_msg>ShareRapidCom: rewritten most of the plugin<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

from pycurl import HTTPHEADER
from module.network.HTTPRequest import BadHeader
from module.network.RequestFactory import getRequest
from module.plugins.internal.SimpleHoster import SimpleHoster, parseFileInfo, replace_patterns


def getInfo(urls):
    h = getRequest()
    h.c.setopt(HTTPHEADER,
               ["Accept: text/html",
                "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"])
    for url in urls:
        html = h.load(url, decode=True)
        file_info = parseFileInfo(ShareRapidCom, replace_patterns(url, ShareRapidCom.FILE_URL_REPLACEMENTS), html)
        yield file_info


class ShareRapidCom(SimpleHoster):
    __name__ = "ShareRapidCom"
    __type__ = "hoster"
    __pattern__ = r"http://(?:www\.)?((share(-?rapid\.(biz|com|cz|info|eu|net|org|pl|sk)|-(central|credit|free|net)\.cz|-ms\.net)|(s-?rapid|rapids)\.(cz|sk))|(e-stahuj|mediatack|premium-rapidshare|rapidshare-premium|qiuck)\.cz|kadzet\.com|stahuj-zdarma\.eu|strelci\.net|universal-share\.com)/stahuj/(?P<id>\w+)"
    __version__ = "0.53"
    __description__ = """Share-rapid.com plugin - premium only"""
    __author_name__ = ("MikyWoW", "zoidberg", "stickell")
    __author_mail__ = ("<EMAIL>", "<EMAIL>", "<EMAIL>")

    FILE_NAME_PATTERN = r'<h1[^>]*><span[^>]*>(?:<a[^>]*>)?(?P<N>[^<]+)'
    FILE_SIZE_PATTERN = r'<td class="i">Velikost:</td>\s*<td class="h"><strong>\s*(?P<S>[0-9.]+) (?P<U>[kKMG])i?B</strong></td>'
    FILE_OFFLINE_PATTERN = ur'Nastala chyba 404|Soubor byl smazán'

    DOWNLOAD_URL_PATTERN = r'<a href="([^"]+)" title="Stahnout">([^<]+)</a>'
    ERR_LOGIN_PATTERN = ur'<div class="error_div"><strong>Stahování je přístupné pouze přihlášeným uživatelům'
    ERR_CREDIT_PATTERN = ur'<div class="error_div"><strong>Stahování zdarma je možné jen přes náš'

    FILE_URL_REPLACEMENTS = [(__pattern__, r'http://share-rapid.com/stahuj/\g<id>')]

    def setup(self):
        self.chunkLimit = 1
        self.resumeDownload = True

    def process(self, pyfile):
        if not self.account:
            self.fail("User not logged in")

        try:
            self.html = self.load(pyfile.url, decode=True)
        except BadHeader, e:
            self.account.relogin(self.user)
            self.retry(3, 0, str(e))

        self.getFileInfo()

        found = re.search(self.DOWNLOAD_URL_PATTERN, self.html)
        if found:
            link = found.group(1)
            self.logDebug("Premium link: %s" % link)

            self.download(link, disposition=True)
        else:
            if re.search(self.ERR_LOGIN_PATTERN, self.html):
                self.relogin(self.user)
                self.retry(3, 0, "User login failed")
            elif re.search(self.ERR_CREDIT_PATTERN, self.html):
                self.fail("Not enough credit left")
            else:
                self.fail("Download link not found")           
{'fraction_non_alphanumeric': 0.15820642978003385, 'fraction_numerical': 0.008883248730964468, 'mean_word_length': 2.7136125654450263, 'pattern_counts': {'":': 4, '<': 50, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5310061', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2444, 'n_words': 440}