Explorar o código

Fix profile pictures with no ID

Tulir Asokan %!s(int64=4) %!d(string=hai) anos
pai
achega
bc55102d3e
Modificáronse 2 ficheiros con 9 adicións e 9 borrados
  1. 0 2
      mauigpapi/types/account.py
  2. 9 7
      mautrix_instagram/puppet.py

+ 0 - 2
mauigpapi/types/account.py

@@ -44,9 +44,7 @@ class BaseResponseUser(UserIdentifier, SerializableAttrs['BaseResponseUser']):
     is_private: bool
     is_verified: bool = False
     profile_pic_url: str
-    # When this doesn't exist, the profile picture is probably the default one
     profile_pic_id: Optional[str] = None
-
     has_anonymous_profile_picture: bool = False
     # TODO find type
     account_badges: Optional[List[Any]] = None

+ 9 - 7
mautrix_instagram/puppet.py

@@ -14,8 +14,8 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 from typing import Optional, Dict, AsyncIterable, Awaitable, AsyncGenerator, TYPE_CHECKING, cast
+import os.path
 
-from aiohttp import ClientSession
 from yarl import URL
 
 from mauigpapi.types import BaseResponseUser
@@ -115,17 +115,19 @@ class Puppet(DBPuppet, BasePuppet):
         return False
 
     async def _update_avatar(self, info: BaseResponseUser, source: 'u.User') -> bool:
-        if info.profile_pic_id != self.photo_id or not self.avatar_set:
-            self.photo_id = info.profile_pic_id
-            if info.profile_pic_id:
+        pic_id = (f"id_{info.profile_pic_id}.jpg" if info.profile_pic_id
+                  else os.path.basename(URL(info.profile_pic_url).path))
+        if pic_id != self.photo_id or not self.avatar_set:
+            self.photo_id = pic_id
+            if info.has_anonymous_profile_picture:
+                mxc = None
+            else:
                 async with source.client.raw_http_get(info.profile_pic_url) as resp:
                     content_type = resp.headers["Content-Type"]
                     resp_data = await resp.read()
                 mxc = await self.default_mxid_intent.upload_media(data=resp_data,
                                                                   mime_type=content_type,
-                                                                  filename=info.profile_pic_id)
-            else:
-                mxc = None
+                                                                  filename=pic_id)
             try:
                 await self.default_mxid_intent.set_avatar_url(mxc)
                 self.avatar_set = True