2022-09-02 15:39:46 +03:00
|
|
|
from config import instance
|
2022-09-05 16:49:48 +03:00
|
|
|
import time
|
2022-08-31 13:20:49 +03:00
|
|
|
import requests
|
2022-09-07 02:55:12 +03:00
|
|
|
from loguru import logger
|
2022-08-31 13:20:49 +03:00
|
|
|
|
|
|
|
instance_point = f"https://{instance}/api/v1"
|
|
|
|
|
|
|
|
with open(".auth", mode='rt') as auth:
|
|
|
|
tkn = auth.read().replace('\n', '')
|
|
|
|
|
2022-11-16 03:42:54 +02:00
|
|
|
|
|
|
|
s = requests.Session()
|
|
|
|
s.headers.update({
|
|
|
|
"Authorization": "Bearer " + tkn,
|
|
|
|
"Accept-encoding": 'gzip'
|
|
|
|
})
|
|
|
|
|
2022-08-31 13:20:49 +03:00
|
|
|
|
|
|
|
def get_notifications():
|
|
|
|
params = {
|
|
|
|
"limit": 15,
|
2022-09-05 01:19:46 +03:00
|
|
|
"types": ["mention"]
|
2022-08-31 13:20:49 +03:00
|
|
|
}
|
2022-09-07 12:05:26 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2022-11-16 03:42:54 +02:00
|
|
|
r = s.get(instance_point + "/notifications", json=params)
|
2022-09-07 12:05:26 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
success = 1
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
logger.exception('Error get notificatios')
|
|
|
|
time.sleep(30)
|
|
|
|
logger.info('Retrying get notificatios...')
|
|
|
|
|
2022-08-31 13:20:49 +03:00
|
|
|
|
|
|
|
def mark_as_read_notification(id_notification):
|
2022-09-06 22:12:30 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2022-11-16 03:42:54 +02:00
|
|
|
r = s.post(instance_point + f"/notifications/{id_notification}/dismiss")
|
2022-09-06 22:12:30 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
success = 1
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
logger.exception(f'Error read notification {id_notification}')
|
|
|
|
time.sleep(30)
|
2022-09-07 12:05:26 +03:00
|
|
|
logger.info(f'Retrying read notification {id_notification}...')
|
2022-08-31 13:20:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_status_context(status_id):
|
2023-07-27 17:21:22 +03:00
|
|
|
retry = 0
|
2022-09-06 22:12:30 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2023-07-28 10:38:13 +03:00
|
|
|
r = s.get(instance_point + f"/statuses/{status_id}/context", timeout=30)
|
2022-09-06 22:12:30 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
success = 1
|
|
|
|
return r.json()
|
2023-07-27 17:21:22 +03:00
|
|
|
except Exception as E:
|
2022-09-06 22:12:30 +03:00
|
|
|
logger.exception(f'Ошибка получения контекста треда {status_id}')
|
2022-09-05 16:44:39 +03:00
|
|
|
time.sleep(30)
|
|
|
|
logger.info('Повторный запрос треда...')
|
2023-07-27 17:21:22 +03:00
|
|
|
retry += 1
|
|
|
|
if retry > 5:
|
|
|
|
raise IOError(f'Фетчинг треда поломан! {E}')
|
|
|
|
|
2022-08-31 13:20:49 +03:00
|
|
|
|
|
|
|
def get_status(status_id):
|
2022-09-07 12:05:26 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2022-11-16 03:42:54 +02:00
|
|
|
r = s.get(instance_point + f"/statuses/{status_id}")
|
2022-09-07 12:05:26 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
success = 1
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
logger.exception(f'Error get status {status_id}')
|
|
|
|
time.sleep(30)
|
|
|
|
logger.info(f'Retrying get status {status_id}')
|
|
|
|
|
2022-08-31 13:20:49 +03:00
|
|
|
|
2024-07-04 14:59:47 +03:00
|
|
|
def post_status(text, reply_to_status_id=None, poll_options=None, poll_expires=345600, attachments=None, visibility='unlisted'):
|
2022-08-31 13:20:49 +03:00
|
|
|
poll = None
|
|
|
|
if poll_options is not None:
|
|
|
|
poll = {
|
|
|
|
"options": poll_options,
|
|
|
|
"expires_in": poll_expires,
|
|
|
|
"multiple": True
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
"status": text,
|
|
|
|
"in_reply_to_id": reply_to_status_id,
|
2024-07-04 14:59:47 +03:00
|
|
|
"visibility": visibility,
|
2022-08-31 13:20:49 +03:00
|
|
|
"content_type": "text/plain",
|
2022-09-01 02:06:06 +03:00
|
|
|
"language": "ru",
|
2022-08-31 13:20:49 +03:00
|
|
|
"poll": poll
|
|
|
|
}
|
2022-09-01 02:06:06 +03:00
|
|
|
if attachments:
|
|
|
|
params['media_ids'] = attachments
|
2022-09-06 22:12:30 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2022-11-16 03:42:54 +02:00
|
|
|
r = s.post(instance_point + "/statuses", json=params)
|
2022-09-06 22:12:30 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
success = 1
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
logger.exception('Error send status, retrying...')
|
|
|
|
time.sleep(5)
|
2022-08-31 13:20:49 +03:00
|
|
|
|
2022-09-01 02:06:06 +03:00
|
|
|
|
|
|
|
def upload_attachment(file_path):
|
|
|
|
file = {
|
|
|
|
"file": open(file_path, mode='rb')
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
"description": "Fediverse Movie Night\nВоскресенье, 21:00\nLIVE ON XXIV Production",
|
|
|
|
}
|
2022-12-17 15:40:14 +02:00
|
|
|
r = s.post(instance_point + "/media", params, files=file, timeout=30)
|
|
|
|
r.raise_for_status()
|
|
|
|
return r.json()['id']
|
2022-09-07 12:05:26 +03:00
|
|
|
|
|
|
|
|
2022-09-02 15:39:46 +03:00
|
|
|
def mute_user(acct_id=str, acct=str, duration=None):
|
|
|
|
params = {
|
|
|
|
"duration": duration
|
|
|
|
}
|
2022-09-06 22:12:30 +03:00
|
|
|
success = 0
|
|
|
|
while success == 0:
|
|
|
|
try:
|
2022-11-16 03:42:54 +02:00
|
|
|
r = s.post(instance_point + '/accounts' + f"/{acct_id}/mute", params)
|
2022-09-06 22:12:30 +03:00
|
|
|
r.raise_for_status()
|
|
|
|
logger.info(f'Пользователь {acct} был заглушен на {duration} secs')
|
|
|
|
success = 1
|
|
|
|
except:
|
|
|
|
logger.exception(f'Ошибка глушения {acct}')
|
|
|
|
time.sleep(5)
|
|
|
|
logger.info(f'Повторное глушение {acct}...')
|