forked from JustAnotherArchivist/snscrape
/
mastodon.py
341 lines (286 loc) · 13 KB
/
mastodon.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
__all__ = ['Toot', 'Boost', 'Attachment', 'Poll', 'PollOption', 'User', 'CustomEmoji', 'MastodonProfileScraper', 'MastodonTootScraperMode', 'MastodonTootScraper']
import bs4
import dataclasses
import datetime
import enum
import json
import logging
import snscrape.base
import snscrape.utils
import time
import typing
import urllib.parse
_logger = logging.getLogger(__name__)
@dataclasses.dataclass
class Toot(snscrape.base.Item):
url: str
id: str
user: 'User'
date: datetime.datetime
text: str
spoilerText: typing.Optional[str] = None
attachments: typing.Optional[typing.List['Attachment']] = None
links: typing.Optional[typing.List[str]] = None
mentionedUsers: typing.Optional[typing.List['User']] = None
hashtags: typing.Optional[typing.List[str]] = None
poll: typing.Optional['Poll'] = None
def __str__(self):
return self.url
@dataclasses.dataclass
class Boost(snscrape.base.Item):
user: 'User'
toot: Toot
def __str__(self):
# Boosts don't have their own URLs
return str(self.toot)
@dataclasses.dataclass
class Attachment:
url: str
name: str
@dataclasses.dataclass
class Poll:
id: str
expirationDate: datetime.datetime
multiple: bool
options: typing.List['PollOption']
votesCount: int
votersCount: typing.Optional[int] = None # Available since version 3.0.0 (commit 3babf846)
@dataclasses.dataclass
class PollOption:
title: str
votesCount: int
@dataclasses.dataclass
class User(snscrape.base.Item):
account: str # @username@domain.invalid
displayName: typing.Optional[str] = None
displayNameWithCustomEmojis: typing.Optional[typing.List[typing.Union[str, 'CustomEmoji']]] = None
avatarUrl: typing.Optional[str] = None
_url: typing.Optional[str] = None
@property
def url(self):
if self._url:
return self._url
return f'https://{"/@".join(reversed(self.account[1:].split("@")))}'
def __str__(self):
return self.url
@dataclasses.dataclass
class CustomEmoji:
shortName: str
url: str
staticUrl: str
class _MastodonCommonScraper(snscrape.base.Scraper):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0', 'Accept-Language': 'en-US,en;q=0.5'}
self._lastRequest = 0
def _rate_limited_get(self, *args, **kwargs):
if (diff := time.time() - self._lastRequest) < 3:
time.sleep(3 - diff)
self._lastRequest = time.time()
return self._get(*args, **kwargs)
def _entries_to_items(self, entries, url):
for entry in entries:
if entry.find('a', class_ = 'load-more'):
continue
tootKwargs = {}
info = entry.find('div', class_ = 'status__info')
if not info: # Before 2.5.0 (commit bb71538b)
info = entry.find('div', class_ = 'status__header')
if not info: # Detailed status (i.e. toot page rather than timeline)?
info = entry.find('div', class_ = 'detailed-status__meta')
link = info.find('a', class_ = 'status__relative-time')
if not link: # Detailed status?
link = info.find('a', class_ = 'detailed-status__datetime')
tootKwargs['url'] = link['href']
tootKwargs['id'] = tootKwargs['url'].rsplit('/', 1)[1]
tootKwargs['date'] = datetime.datetime.strptime(info.find('data', class_ = 'dt-published')['value'], '%Y-%m-%dT%H:%M:%S+00:00').replace(tzinfo = datetime.timezone.utc)
userKwargs = {}
userLink = info.find('a', class_ = 'status__display-name')
if not userLink: # Detailed status?
userLink = entry.find('a', class_ = 'detailed-status__display-name')
userNameSpan = userLink.find('span', class_ = 'display-name')
userKwargs['account'] = userNameSpan.find('span').text.strip()
if userKwargs['account'].count('@') == 1: # Ancient versions don't include the instance for posts from accounts on the instance itself
userKwargs['account'] = self._url_to_account(userLink['href'])
userKwargs['_url'] = urllib.parse.urljoin(url, userLink['href'])
userKwargs['displayName'], userKwargs['displayNameWithCustomEmojis'] = self._display_name(userNameSpan.find('strong'), url)
userKwargs['avatarUrl'] = urllib.parse.urljoin(url, userLink.find('img', class_ = 'u-photo')['src'])
tootKwargs['user'] = User(**userKwargs)
content = entry.find('div', class_ = 'status__content')
if not content.find(class_ = 'status__content__spoiler-link'):
tootKwargs['text'] = '\n\n'.join(p.text for p in content.find_all('p'))
else:
tootKwargs['text'] = content.find('span', class_ = 'p-summary').text
tootKwargs['spoilerText'] = '\n\n'.join(p.text for p in content.find('div', class_ = 'e-content').find_all('p'))
if (attachmentsDiv := entry.find('div', class_ = 'attachment-list')):
attachments = []
for a in attachmentsDiv.find_all('a'):
attachments.append(Attachment(url = urllib.parse.urljoin(url, a['href']), name = a.text.strip()))
tootKwargs['attachments'] = attachments
elif (mediaGalleryDiv := entry.find('div', attrs = {'data-component': 'MediaGallery'})): # Before 2.7.0 (https://github.com/mastodon/mastodon/issues/6714)
o = json.loads(mediaGalleryDiv['data-props'])
attachments = []
for medium in o['media']:
attachments.append(Attachment(url = urllib.parse.urljoin(url, medium['url']), name = medium['url'].rsplit('/', 1)[-1].strip()))
tootKwargs['attachments'] = attachments
elif (attachmentsDiv := entry.find('div', class_ = 'status__attachments')): # Before 2.3.0 (commit 2bbf987a)
attachments = []
for a in attachmentsDiv.find_all('a'):
attachments.append(Attachment(url = urllib.parse.urljoin(url, a['href']), name = a['href'].rsplit('/', 1)[1]))
tootKwargs['attachments'] = attachments
links = []
mentionedUsers = []
hashtags = []
for a in content.find_all('a'):
cls = a.get('class', [])
if 'mention' in cls and 'u-url' in cls:
mentionUrl = urllib.parse.urljoin(url, a['href'])
mentionedUsers.append(User(account = self._url_to_account(mentionUrl), _url = mentionUrl))
elif 'mention' in cls and 'hashtag' in cls:
hashtags.append(a.text.strip())
else:
links.append(urllib.parse.urljoin(url, a['href']))
if links:
tootKwargs['links'] = links
if mentionedUsers:
tootKwargs['mentionedUsers'] = mentionedUsers
if hashtags:
tootKwargs['hashtags'] = hashtags
if (pollDiv := entry.find('div', attrs = {'data-component': 'Poll'})):
o = json.loads(pollDiv['data-props'])
pollKwargs = {}
pollKwargs['id'] = o['poll']['id']
pollKwargs['expirationDate'] = datetime.datetime.strptime(o['poll']['expires_at'], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo = datetime.timezone.utc)
pollKwargs['multiple'] = o['poll']['multiple']
pollKwargs['options'] = [PollOption(title = op['title'], votesCount = op['votes_count']) for op in o['poll']['options']]
pollKwargs['votesCount'] = o['poll']['votes_count']
if 'voters_count' in o['poll']: # 3.0.0 (commit 3babf846)
pollKwargs['votersCount'] = o['poll']['voters_count']
tootKwargs['poll'] = Poll(**pollKwargs)
toot = Toot(**tootKwargs)
# Boosts
prepend = entry.find('div', class_ = 'status__prepend')
if not prepend: # Before 2.5.0 (commit bb71538b)
prepend = entry.find('div', class_ = 'pre-header')
if prepend and prepend.find('i', class_ = 'fa-retweet'): # Is a boost
userKwargs = {}
userLink = prepend.find('a', class_ = 'status__display-name')
# The user is always on this instance since that's the only place where boosts are shown, hence there is no explicit account span. Reconstruct from URL.
userUrl = urllib.parse.urljoin(url, userLink['href'])
assert userUrl.count('/') == 3 and userUrl.count('/@') == 1
userKwargs['account'] = '@'.join(reversed(userUrl.split('/')[2:]))
userKwargs['displayName'], userKwargs['displayNameWithCustomEmojis'] = self._display_name(userLink.find('strong'), url)
toot = Boost(user = User(**userKwargs), toot = toot)
yield toot
def _display_name(self, strong, url):
outPlain = []
outFull = []
hasCustomEmoji = False
for child in strong.children:
if isinstance(child, bs4.element.NavigableString):
outPlain.append(str(child))
outFull.append(str(child))
elif child.name == 'img' and 'custom-emoji' in child.get('class', []):
hasCustomEmoji = True
outPlain.append(child['alt'])
outFull.append(CustomEmoji(shortName = child['alt'], url = urllib.parse.urljoin(url, child['data-original']), staticUrl = urllib.parse.urljoin(url, child['data-static'])))
elif child.name == 'img' and 'emojione' in child.get('class', []):
# Version 2.0.0 (which first added custom emojis) to 2.9.4: no data-* attributes, only gets one of the URLs with no (easy, reliable) way of knowing which it is.
hasCustomEmoji = True
outPlain.append(child['alt'])
outFull.append(CustomEmoji(shortName = child['alt'], url = urllib.parse.urljoin(url, child['src'])))
else:
_logger.warning(f'Unexpected display name child: {child!r}')
return ''.join(outPlain), outFull if hasCustomEmoji else None
@staticmethod
def _url_to_account(url):
if url.count('/') == 3 and url.count('/@') == 1:
return '@'.join(reversed(url.split('/')[2:]))
if url.count('/') == 4 and '/users/' in url: # E.g. Pleroma, also supported by Mastodon
return '@' + '@'.join(reversed(url.split('/')[2::2]))
if url.count('/') == 4 and '/accounts/' in url: # E.g. Peertube
return '@' + '@'.join(reversed(url.split('/')[2::2]))
if url.count('/') == 4 and '/profile/' in url: # E.g. Friendica
return '@' + '@'.join(reversed(url.split('/')[2::2]))
raise ValueError('Unrecognised account URL format')
class MastodonProfileScraper(_MastodonCommonScraper):
name = 'mastodon-profile'
def __init__(self, account, **kwargs):
super().__init__(**kwargs)
if account.startswith('@') and account.count('@') == 2:
account, domain = account[1:].split('@')
url = f'https://{domain}/@{account}'
else:
url = account
self._url = url
def get_items(self):
initial = True
while True:
if initial:
r = self._rate_limited_get(f'{self._url}/with_replies', headers = self._headers)
if r.status_code not in (200, 404):
raise snscrape.base.ScraperException(f'Got status code {r.status_code}')
if r.status_code == 404: # Possibly an old instance where with_replies doesn't exist, try without that.
r = self._rate_limited_get(self._url, headers = self._headers)
if r.status_code not in (200, 404):
raise snscrape.base.ScraperException(f'Got status code {r.status_code}')
if r.status_code == 404:
_logger.warning('Account does not exist')
return
_logger.warning('Old Mastodon instance, cannot retrieve reply toots')
initial = False
else:
r = self._rate_limited_get(url, headers = self._headers)
if r.status_code != 200:
raise snscrape.base.ScraperException(f'Got status code {r.status_code}')
soup = bs4.BeautifulSoup(r.text, 'lxml')
yield from self._entries_to_items(soup.find('div', class_ = 'activity-stream').find_all('div', class_ = 'entry'), r.url)
nextA = soup.find('a', class_ = 'load-more', href = lambda x: '?max_id=' in x or '&max_id=' in x)
if not nextA: # Before 2.5.0 (commit bb71538b)
paginationDiv = soup.find('div', class_ = 'pagination')
if paginationDiv:
nextA = paginationDiv.find('a', class_ = 'next')
if not nextA: # End of pagination
break
url = urllib.parse.urljoin(r.url, nextA['href'])
@classmethod
def _cli_setup_parser(cls, subparser):
subparser.add_argument('account', type = snscrape.utils.nonempty_string_arg('account'), help = 'A Mastodon account. This can be either a URL to the profile page or a string of the form @account@instance.example.org')
@classmethod
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.account)
class MastodonTootScraperMode(enum.Enum):
SINGLE = 'single'
THREAD = 'thread'
@classmethod
def _cli_from_args(cls, args):
if args.thread:
return cls.THREAD
return cls.SINGLE
class MastodonTootScraper(_MastodonCommonScraper):
name = 'mastodon-toot'
def __init__(self, url, *, mode = MastodonTootScraperMode.SINGLE, **kwargs):
super().__init__(**kwargs)
self._url = url
self._mode = mode
def get_items(self):
r = self._rate_limited_get(self._url, headers = self._headers)
if r.status_code == 404:
_logger.warning('Toot does not exist')
return
if r.status_code != 200:
raise snscrape.base.ScraperException(f'Got status code {r.status_code}')
soup = bs4.BeautifulSoup(r.text, 'lxml')
if self._mode is MastodonTootScraperMode.SINGLE:
status = soup.find('div', class_ = 'detailed-status')
entry = status.parent
yield from self._entries_to_items([entry], r.url)
elif self._mode is MastodonTootScraperMode.THREAD:
yield from self._entries_to_items(soup.find('div', class_ = 'activity-stream').find_all('div', class_ = 'entry'), r.url)
@classmethod
def _cli_setup_parser(cls, subparser):
subparser.add_argument('--thread', action = 'store_true', help = 'Collect thread around the toot referenced by the URL')
subparser.add_argument('url', type = snscrape.utils.nonempty_string_arg('url'), help = 'A URL for a toot')
@classmethod
def _cli_from_args(cls, args):
return cls._cli_construct(args, args.url, mode = MastodonTootScraperMode._cli_from_args(args))