|
| 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 | +import re |
| 8 | + |
| 9 | +from .common import Extractor, Message |
| 10 | +from .. import text, exception |
| 11 | +from ..cache import cache |
| 12 | + |
| 13 | + |
| 14 | +class GirlswithmuscleExtractor(Extractor): |
| 15 | + def login(self): |
| 16 | + username, password = self._get_auth_info() |
| 17 | + if username: |
| 18 | + self.cookies_update(self._login_impl(username, password)) |
| 19 | + |
| 20 | + @staticmethod |
| 21 | + def _is_logged_in(page_text: str) -> bool: |
| 22 | + return 'Log in' not in page_text |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def _get_csrfmiddlewaretoken(page: str) -> str: |
| 26 | + return text.extract( |
| 27 | + page, |
| 28 | + 'name="csrfmiddlewaretoken" value="', |
| 29 | + '"' |
| 30 | + )[0] |
| 31 | + |
| 32 | + def _open_login_page(self): |
| 33 | + """We need it to get second CSRF token""" |
| 34 | + url = "https://www.girlswithmuscle.com/login/?next=/" |
| 35 | + response = self.request(url) |
| 36 | + return self._get_csrfmiddlewaretoken(response.text) |
| 37 | + |
| 38 | + def _send_login_request(self, username, password, csrf_mw): |
| 39 | + """Actual login action""" |
| 40 | + data = { |
| 41 | + "csrfmiddlewaretoken": csrf_mw, |
| 42 | + "username": username, |
| 43 | + "password": password, |
| 44 | + "next": "/" |
| 45 | + } |
| 46 | + |
| 47 | + # Otherwise will be 403 Forbidden |
| 48 | + self.session.headers['Origin'] = 'https://www.girlswithmuscle.com' |
| 49 | + self.session.headers['Referer'] = \ |
| 50 | + 'https://www.girlswithmuscle.com/login/?next=/' |
| 51 | + |
| 52 | + # if successful, will update cookies |
| 53 | + url = "https://www.girlswithmuscle.com/login/" |
| 54 | + response = self.request(url, method="post", data=data) |
| 55 | + |
| 56 | + if "Wrong username or password" in response.text: |
| 57 | + raise exception.AuthenticationError() |
| 58 | + elif not self._is_logged_in(response.text): |
| 59 | + raise exception.AuthenticationError("Account data is missing") |
| 60 | + |
| 61 | + @cache(maxage=28 * 86400, keyarg=1) |
| 62 | + def _login_impl(self, username, password): |
| 63 | + self.log.info("Logging in as %s", username) |
| 64 | + |
| 65 | + csrf_mw = self._open_login_page() |
| 66 | + self._send_login_request(username, password, csrf_mw) |
| 67 | + return {c.name: c.value for c in self.session.cookies} |
| 68 | + |
| 69 | + |
| 70 | +class GirlswithmusclePostExtractor(GirlswithmuscleExtractor): |
| 71 | + """Extractor for individual posts on girlswithmuscle.com""" |
| 72 | + category = "girlswithmuscle" |
| 73 | + subcategory = "post" |
| 74 | + directory_fmt = ("{category}", "{model}") |
| 75 | + filename_fmt = "{model}_{id}.{extension}" |
| 76 | + archive_fmt = "{type}_{model}_{id}" |
| 77 | + pattern = (r"(?:https?://)?(?:www\.)?girlswithmuscle\.com" |
| 78 | + r"/(\d+)/") |
| 79 | + example = "https://www.girlswithmuscle.com/1841638/" |
| 80 | + |
| 81 | + def __init__(self, match): |
| 82 | + Extractor.__init__(self, match) |
| 83 | + self.id = match.groups()[0] |
| 84 | + |
| 85 | + def items(self): |
| 86 | + self.login() |
| 87 | + url = "https://girlswithmuscle.com/{}/".format(self.id) |
| 88 | + page = self.request(url).text |
| 89 | + |
| 90 | + if page is None: |
| 91 | + raise exception.NotFoundError("post") |
| 92 | + |
| 93 | + url = text.extr(page, 'class="main-image" src="', '"') |
| 94 | + if url: |
| 95 | + metadata = self.metadata(page, url, 'picture') |
| 96 | + else: |
| 97 | + url = text.extr(page, '<source src="', '"') |
| 98 | + metadata = self.metadata(page, url, 'video') |
| 99 | + |
| 100 | + yield Message.Directory, metadata |
| 101 | + yield Message.Url, url, metadata |
| 102 | + |
| 103 | + def metadata(self, page, url, content_type): |
| 104 | + info_source_begin = \ |
| 105 | + '<div class="image-info" id="info-source" style="display: none">' |
| 106 | + info_source_end = "</div>" |
| 107 | + source = text.remove_html( |
| 108 | + text.extr(page, info_source_begin, info_source_end)) |
| 109 | + |
| 110 | + info_uploader_begin = '<div class="image-info" id="info-uploader">' |
| 111 | + info_uploader_end = "</div>" |
| 112 | + uploader = text.remove_html( |
| 113 | + text.extr(page, info_uploader_begin, info_uploader_end)) |
| 114 | + |
| 115 | + tags = text.extr( |
| 116 | + page, 'class="selected-tags">', "</span>", '' |
| 117 | + ).split(', ') |
| 118 | + tags = [tag for tag in tags if tag] |
| 119 | + |
| 120 | + score = text.parse_int(text.remove_html( |
| 121 | + text.extr(page, 'Score: <b>', '</span', '0'))) |
| 122 | + |
| 123 | + model = self._parse_model(page) |
| 124 | + |
| 125 | + return { |
| 126 | + 'id': self.id, |
| 127 | + 'model': model, |
| 128 | + 'model_list': self._parse_model_list(model), |
| 129 | + 'tags': tags, |
| 130 | + 'posted_dt': text.extr( |
| 131 | + page, 'class="hover-time" title="', '"', '' |
| 132 | + ), |
| 133 | + 'is_favorite': self._parse_is_favorite(page), |
| 134 | + 'source_filename': source, |
| 135 | + 'uploader': uploader, |
| 136 | + 'score': score, |
| 137 | + 'comments': self._parse_comments(page), |
| 138 | + 'extension': text.ext_from_url(url), |
| 139 | + 'type': content_type, |
| 140 | + } |
| 141 | + |
| 142 | + @staticmethod |
| 143 | + def _parse_model(page): |
| 144 | + model = text.extr(page, '<title>', "</title>", None) |
| 145 | + return 'unknown' if model.startswith('Picture #') else model |
| 146 | + |
| 147 | + @staticmethod |
| 148 | + def _parse_model_list(model): |
| 149 | + if model == 'unknown': |
| 150 | + return [] |
| 151 | + else: |
| 152 | + return [name.strip() for name in model.split(',')] |
| 153 | + |
| 154 | + @staticmethod |
| 155 | + def _parse_is_favorite(page): |
| 156 | + fav_button = text.extr(page, 'id="favorite-button">', "</span>", '') |
| 157 | + unfav_button = text.extr(page, |
| 158 | + 'class="actionbutton unfavorite-button">', |
| 159 | + "</span>", '') |
| 160 | + |
| 161 | + is_favorite = None |
| 162 | + if unfav_button == 'Unfavorite': |
| 163 | + is_favorite = True |
| 164 | + if fav_button == 'Favorite': |
| 165 | + is_favorite = False |
| 166 | + |
| 167 | + return is_favorite |
| 168 | + |
| 169 | + @staticmethod |
| 170 | + def _parse_comments(page): |
| 171 | + comments = text.extract_iter(page, '<div class="comment-body-inner">', |
| 172 | + '</div>') |
| 173 | + return [comment.strip() for comment in comments] |
| 174 | + |
| 175 | + |
| 176 | +class GirlswithmuscleGalleryExtractor(GirlswithmuscleExtractor): |
| 177 | + """Extractor for galleries on girlswithmuscle.com""" |
| 178 | + category = "girlswithmuscle" |
| 179 | + subcategory = "gallery" |
| 180 | + pattern = r"(?:https?://)?(?:www\.)?girlswithmuscle\.com/images/(.*)" |
| 181 | + example = "https://www.girlswithmuscle.com/images/?name=Samantha%20Jerring" |
| 182 | + |
| 183 | + def __init__(self, match): |
| 184 | + Extractor.__init__(self, match) |
| 185 | + self.query = match.groups()[0] |
| 186 | + |
| 187 | + def pages(self): |
| 188 | + url = "https://www.girlswithmuscle.com/images/{}".format(self.query) |
| 189 | + response = self.request(url) |
| 190 | + if url != response.url: |
| 191 | + msg = ('Request was redirected to "{}", try logging in'. |
| 192 | + format(response.url)) |
| 193 | + raise exception.AuthorizationError(msg) |
| 194 | + page = response.text |
| 195 | + |
| 196 | + match = re.search(r"Page (\d+) of (\d+)", page) |
| 197 | + current, total = match.groups() |
| 198 | + current, total = text.parse_int(current), text.parse_int(total) |
| 199 | + |
| 200 | + yield page |
| 201 | + for i in range(current + 1, total + 1): |
| 202 | + url = ("https://www.girlswithmuscle.com/images/{}/{}". |
| 203 | + format(i, self.query)) |
| 204 | + yield self.request(url).text |
| 205 | + |
| 206 | + def items(self): |
| 207 | + self.login() |
| 208 | + for page in self.pages(): |
| 209 | + for imgid in text.extract_iter(page, 'id="imgid-', '"'): |
| 210 | + url = "https://www.girlswithmuscle.com/{}/".format(imgid) |
| 211 | + yield Message.Queue, url, { |
| 212 | + "gallery_name": self._parse_gallery_name(page), |
| 213 | + "_extractor": GirlswithmusclePostExtractor |
| 214 | + } |
| 215 | + |
| 216 | + @staticmethod |
| 217 | + def _parse_gallery_name(page): |
| 218 | + return text.extr(page, "<title>", "</title>") |
0 commit comments