Agregar soporte para Matrix
This commit is contained in:
parent
da41f67dc2
commit
cf3812da42
|
@ -1,4 +1,3 @@
|
|||
import threading
|
||||
import time
|
||||
|
||||
from django.conf import settings
|
||||
|
@ -6,19 +5,9 @@ from django.contrib import admin
|
|||
from django.contrib import messages
|
||||
|
||||
from django_admin_listfilter_dropdown.filters import DropdownFilter
|
||||
import httpx
|
||||
from mastodon import Mastodon
|
||||
|
||||
|
||||
from . import models
|
||||
|
||||
|
||||
def run_in_thread(fn):
|
||||
def run(*k, **kw):
|
||||
t = threading.Thread(target=fn, args=k, kwargs=kw)
|
||||
t.start()
|
||||
return t
|
||||
return run
|
||||
from . import util
|
||||
|
||||
|
||||
@admin.register(models.Person)
|
||||
|
@ -80,33 +69,7 @@ class AdminMovie(admin.ModelAdmin):
|
|||
self.message_user(request, 'Publicado correctamente', messages.SUCCESS)
|
||||
published.short_description = 'Republicar en redes'
|
||||
|
||||
def _public_mastodon(self, message, cartel):
|
||||
MT = {
|
||||
'jpg': 'image/jpeg',
|
||||
'png': 'image/png',
|
||||
}
|
||||
message += '\n\n#mauflix'
|
||||
media_type = MT[cartel.url[-3:]]
|
||||
|
||||
try:
|
||||
server = Mastodon(
|
||||
access_token=settings.TOKEN_MASTODON,
|
||||
api_base_url=settings.URL_MASTODON)
|
||||
|
||||
media = server.media_post(cartel.read(), media_type)
|
||||
server.status_post(message, media_ids=media)
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
return
|
||||
|
||||
def _public_telegram(self, message, cartel):
|
||||
url = f'https://api.telegram.org/bot{settings.TOKEN_TELEGRAM}/sendPhoto'
|
||||
url_cartel = settings.URL_CDN.format(cartel)
|
||||
data = {'chat_id': settings.CHAT_ID, 'photo': url_cartel, 'caption': message}
|
||||
result = httpx.post(url, data=data).json()
|
||||
return
|
||||
|
||||
@run_in_thread
|
||||
@util.run_in_thread
|
||||
def _public(self, obj):
|
||||
time.sleep(3)
|
||||
message = f"""\"{obj.name}\"
|
||||
|
@ -114,8 +77,9 @@ Dir: {', '.join(obj.directors.values_list('name', flat=True))}
|
|||
País: {', '.join(obj.countries.values_list('name', flat=True))}
|
||||
Año: {obj.year}
|
||||
"""
|
||||
self._public_mastodon(message, obj.cartel)
|
||||
self._public_telegram(message, obj.cartel.url)
|
||||
# ~ util.public_mastodon(message, obj.cartel)
|
||||
# ~ util.public_telegram(message, obj.cartel.url)
|
||||
util.public_social(message, obj.cartel)
|
||||
return
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
import httpx
|
||||
from mastodon import Mastodon
|
||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||
|
||||
|
||||
def run_in_thread(fn):
|
||||
def run(*k, **kw):
|
||||
t = threading.Thread(target=fn, args=k, kwargs=kw)
|
||||
t.start()
|
||||
return t
|
||||
return run
|
||||
|
||||
|
||||
def public_social(message, cartel):
|
||||
|
||||
return
|
||||
|
||||
|
||||
def public_mastodon(message, cartel):
|
||||
MT = {
|
||||
'jpg': 'image/jpeg',
|
||||
'png': 'image/png',
|
||||
}
|
||||
message += '\n\n#mauflix'
|
||||
media_type = MT[cartel.url[-3:]]
|
||||
|
||||
try:
|
||||
server = Mastodon(
|
||||
access_token=settings.TOKEN_MASTODON,
|
||||
api_base_url=settings.URL_MASTODON)
|
||||
|
||||
media = server.media_post(cartel.read(), media_type)
|
||||
server.status_post(message, media_ids=media)
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
return
|
||||
|
||||
|
||||
def public_telegram(message, cartel):
|
||||
url = f'https://api.telegram.org/bot{settings.TOKEN_TELEGRAM}/sendPhoto'
|
||||
url_cartel = settings.URL_CDN.format(cartel)
|
||||
data = {'chat_id': settings.CHAT_ID, 'photo': url_cartel, 'caption': message}
|
||||
result = httpx.post(url, data=data).json()
|
||||
print(result)
|
||||
return
|
||||
|
||||
|
||||
def public_matrix():
|
||||
|
||||
return
|
Loading…
Reference in New Issue