| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
from .common import Extractor, Message |
| 8 |
from .. import text |
| 9 |
|
| 10 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?girlswithmuscle\.com" |
| 11 |
|
| 12 |
|
| 13 |
class GirlswithmuscleExtractor(Extractor): |
| 14 |
"""Base class for girlswithmuscle extractors""" |
| 15 |
category = "girlswithmuscle" |
| 16 |
root = "https://www.girlswithmuscle.com" |
| 17 |
directory_fmt = ("{category}", "{model}") |
| 18 |
filename_fmt = "{model}_{id}.{extension}" |
| 19 |
archive_fmt = "{type}_{model}_{id}" |
| 20 |
|
| 21 |
def login(self): |
| 22 |
username, password = self._get_auth_info() |
| 23 |
if username: |
| 24 |
return self.cookies_update(self.cache( |
| 25 |
self._login_impl, username, password, |
| 26 |
_exp=14*86400, _mem=False)) |
| 27 |
|
| 28 |
def _login_impl(self, username, password): |
| 29 |
self.log.info("Logging in as %s", username) |
| 30 |
|
| 31 |
url = self.root + "/login/" |
| 32 |
page = self.request(url).text |
| 33 |
csrf_token = text.extr(page, 'name="csrfmiddlewaretoken" value="', '"') |
| 34 |
|
| 35 |
headers = { |
| 36 |
"Origin" : self.root, |
| 37 |
"Referer": url, |
| 38 |
} |
| 39 |
data = { |
| 40 |
"csrfmiddlewaretoken": csrf_token, |
| 41 |
"username": username, |
| 42 |
"password": password, |
| 43 |
"next": "/", |
| 44 |
} |
| 45 |
response = self.request( |
| 46 |
url, method="POST", headers=headers, data=data) |
| 47 |
|
| 48 |
if not response.history: |
| 49 |
raise self.exc.AuthenticationError() |
| 50 |
|
| 51 |
page = response.text |
| 52 |
if ">Wrong username or password" in page: |
| 53 |
raise self.exc.AuthenticationError() |
| 54 |
if ">Log in<" in page: |
| 55 |
raise self.exc.AuthenticationError("Account data is missing") |
| 56 |
|
| 57 |
return {c.name: c.value for c in response.history[0].cookies} |
| 58 |
|
| 59 |
|
| 60 |
class GirlswithmusclePostExtractor(GirlswithmuscleExtractor): |
| 61 |
"""Extractor for individual posts on girlswithmuscle.com""" |
| 62 |
subcategory = "post" |
| 63 |
pattern = BASE_PATTERN + r"/(\d+)" |
| 64 |
example = "https://www.girlswithmuscle.com/12345/" |
| 65 |
|
| 66 |
def items(self): |
| 67 |
self.login() |
| 68 |
|
| 69 |
url = f"{self.root}/{self.groups[0]}/" |
| 70 |
page = self.request(url).text |
| 71 |
if not page: |
| 72 |
raise self.exc.NotFoundError("post") |
| 73 |
|
| 74 |
metadata = self.metadata(page) |
| 75 |
|
| 76 |
if url := text.extr(page, 'class="main-image" src="', '"'): |
| 77 |
metadata["type"] = "picture" |
| 78 |
else: |
| 79 |
url = text.extr(page, '<source src="', '"') |
| 80 |
metadata["type"] = "video" |
| 81 |
|
| 82 |
text.nameext_from_url(url, metadata) |
| 83 |
yield Message.Directory, "", metadata |
| 84 |
yield Message.Url, url, metadata |
| 85 |
|
| 86 |
def metadata(self, page): |
| 87 |
source = text.remove_html(text.extr( |
| 88 |
page, '<div id="info-source" style="display: none">', "</div>")) |
| 89 |
image_info = text.extr( |
| 90 |
page, '<div class="image-info">', "</div>") |
| 91 |
uploader = text.remove_html(text.extr( |
| 92 |
image_info, '<span class="username-html">', "</a>")) |
| 93 |
|
| 94 |
tags = text.extr(page, 'id="tags-text">', "</div>") |
| 95 |
score = text.parse_int(text.remove_html(text.extr( |
| 96 |
page, "Score: <b>", "</span"))) |
| 97 |
model = self._extract_model(page) |
| 98 |
|
| 99 |
return { |
| 100 |
"id": self.groups[0], |
| 101 |
"model": model, |
| 102 |
"model_list": self._parse_model_list(model), |
| 103 |
"tags": text.split_html(tags)[1::2], |
| 104 |
"date": self.parse_datetime_iso(text.extr( |
| 105 |
page, 'class="hover-time" title="', '"')[:19]), |
| 106 |
"is_favorite": self._parse_is_favorite(page), |
| 107 |
"source_filename": source, |
| 108 |
"uploader": uploader, |
| 109 |
"score": score, |
| 110 |
"comments": self._extract_comments(page), |
| 111 |
} |
| 112 |
|
| 113 |
def _extract_model(self, page): |
| 114 |
model = text.extr(page, "<title>", "</title>") |
| 115 |
return "unknown" if model.startswith("Picture #") else model |
| 116 |
|
| 117 |
def _parse_model_list(self, model): |
| 118 |
if model == "unknown": |
| 119 |
return [] |
| 120 |
else: |
| 121 |
return [name.strip() for name in model.split(",")] |
| 122 |
|
| 123 |
def _parse_is_favorite(self, page): |
| 124 |
fav_button = text.extr( |
| 125 |
page, 'id="favorite-button">', "</span>") |
| 126 |
unfav_button = text.extr( |
| 127 |
page, 'class="actionbutton unfavorite-button">', "</span>") |
| 128 |
|
| 129 |
is_favorite = None |
| 130 |
if unfav_button == "Unfavorite": |
| 131 |
is_favorite = True |
| 132 |
if fav_button == "Favorite": |
| 133 |
is_favorite = False |
| 134 |
|
| 135 |
return is_favorite |
| 136 |
|
| 137 |
def _extract_comments(self, page): |
| 138 |
comments = text.extract_iter( |
| 139 |
page, '<div class="comment-body-inner">', "</div>") |
| 140 |
return [comment.strip() for comment in comments] |
| 141 |
|
| 142 |
|
| 143 |
class GirlswithmuscleSearchExtractor(GirlswithmuscleExtractor): |
| 144 |
"""Extractor for search results on girlswithmuscle.com""" |
| 145 |
subcategory = "search" |
| 146 |
pattern = BASE_PATTERN + r"/images/(.*)" |
| 147 |
example = "https://www.girlswithmuscle.com/images/?name=MODEL" |
| 148 |
|
| 149 |
def pages(self): |
| 150 |
query = self.groups[0] |
| 151 |
url = f"{self.root}/images/{query}" |
| 152 |
response = self.request(url) |
| 153 |
if response.history: |
| 154 |
msg = f'Request was redirected to "{response.url}", try logging in' |
| 155 |
raise self.exc.AuthorizationError(msg) |
| 156 |
page = response.text |
| 157 |
|
| 158 |
match = text.re(r"Page (\d+) of (\d+)").search(page) |
| 159 |
current, total = match.groups() |
| 160 |
current, total = text.parse_int(current), text.parse_int(total) |
| 161 |
|
| 162 |
yield page |
| 163 |
for i in range(current + 1, total + 1): |
| 164 |
url = f"{self.root}/images/{i}/{query}" |
| 165 |
yield self.request(url).text |
| 166 |
|
| 167 |
def items(self): |
| 168 |
self.login() |
| 169 |
for page in self.pages(): |
| 170 |
data = { |
| 171 |
"_extractor" : GirlswithmusclePostExtractor, |
| 172 |
"gallery_name": text.unescape(text.extr(page, "<title>", "<")), |
| 173 |
} |
| 174 |
for imgid in text.extract_iter(page, 'id="imgid-', '"'): |
| 175 |
url = f"{self.root}/{imgid}/" |
| 176 |
yield Message.Queue, url, data |