aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/xvideos.py
blob: 6c016ecfc3f79b5fac6de47fa30567cbf3333e47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-

# Copyright 2017-2025 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

"""Extractors for https://www.xvideos.com/"""

from .common import GalleryExtractor, Extractor, Message
from .. import text, util

BASE_PATTERN = (r"(?:https?://)?(?:www\.)?xvideos\.com"
                r"/(?:profiles|(?:amateur-|model-)?channels)")


class XvideosBase():
    """Base class for xvideos extractors"""
    category = "xvideos"
    root = "https://www.xvideos.com"


class XvideosGalleryExtractor(XvideosBase, GalleryExtractor):
    """Extractor for user profile galleries on xvideos.com"""
    subcategory = "gallery"
    directory_fmt = ("{category}", "{user[name]}",
                     "{gallery[id]} {gallery[title]}")
    filename_fmt = "{category}_{gallery[id]}_{num:>03}.{extension}"
    archive_fmt = "{gallery[id]}_{num}"
    pattern = BASE_PATTERN + r"/([^/?#]+)/photos/(\d+)"
    example = "https://www.xvideos.com/profiles/USER/photos/12345"

    def __init__(self, match):
        self.user, self.gallery_id = match.groups()
        url = f"{self.root}/profiles/{self.user}/photos/{self.gallery_id}"
        GalleryExtractor.__init__(self, match, url)

    def metadata(self, page):
        extr = text.extract_from(page)
        user = {
            "id"     : text.parse_int(extr('"id_user":', ',')),
            "display": extr('"display":"', '"'),
            "sex"    : extr('"sex":"', '"'),
            "name"   : self.user,
        }
        title = extr('"title":"', '"')
        user["description"] = extr(
            '<small class="mobile-hide">', '</small>').strip()
        tags = extr('<em>Tagged:</em>', '<').strip()

        return {
            "user": user,
            "gallery": {
                "id"   : text.parse_int(self.gallery_id),
                "title": text.unescape(title),
                "tags" : text.unescape(tags).split(", ") if tags else [],
            },
        }

    def images(self, page):
        results = [
            (url, None)
            for url in text.extract_iter(
                page, '<a class="embed-responsive-item" href="', '"')
        ]

        if not results:
            return

        while len(results) % 500 == 0:
            path = text.rextr(page, ' href="', '"', page.find(">Next</"))
            if not path:
                break
            page = self.request(self.root + path).text
            results.extend(
                (url, None)
                for url in text.extract_iter(
                    page, '<a class="embed-responsive-item" href="', '"')
            )

        return results


class XvideosUserExtractor(XvideosBase, Extractor):
    """Extractor for user profiles on xvideos.com"""
    subcategory = "user"
    categorytransfer = True
    pattern = BASE_PATTERN + r"/([^/?#]+)/?(?:#.*)?$"
    example = "https://www.xvideos.com/profiles/USER"

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.user = match[1]

    def items(self):
        url = f"{self.root}/profiles/{self.user}"
        page = self.request(url, notfound=self.subcategory).text
        data = util.json_loads(text.extr(
            page, "xv.conf=", ";</script>"))["data"]

        if not isinstance(data["galleries"], dict):
            return
        if "0" in data["galleries"]:
            del data["galleries"]["0"]

        galleries = [
            {
                "id"   : text.parse_int(gid),
                "title": text.unescape(gdata["title"]),
                "count": gdata["nb_pics"],
                "_extractor": XvideosGalleryExtractor,
            }
            for gid, gdata in data["galleries"].items()
        ]
        galleries.sort(key=lambda x: x["id"])

        base = f"{self.root}/profiles/{self.user}/photos/"
        for gallery in galleries:
            url = f"{base}{gallery['id']}"
            yield Message.Queue, url, gallery