|
| 1 | +from functools import cached_property |
| 2 | +from pathlib import Path |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +import requests |
| 6 | +from django.conf import settings |
| 7 | + |
| 8 | +from tournesol.models import Entity |
| 9 | + |
| 10 | + |
| 11 | +class TournesolBotClient: |
| 12 | + def __init__(self, account): |
| 13 | + |
| 14 | + credentials = settings.TWITTERBOT_CREDENTIALS |
| 15 | + if account not in credentials: |
| 16 | + raise ValueError(f"No credentials found for {account} account!") |
| 17 | + |
| 18 | + self.account_cred = credentials[account] |
| 19 | + self.language = self.account_cred["LANGUAGE"] |
| 20 | + |
| 21 | + @cached_property |
| 22 | + def tweepy_api(self): |
| 23 | + # Client for Twitter API v1.1 |
| 24 | + # We need this authentication also because to add media it only works with api v1.1 |
| 25 | + |
| 26 | + import tweepy # pylint:disable=import-outside-toplevel |
| 27 | + auth = tweepy.OAuth1UserHandler( |
| 28 | + consumer_key=self.account_cred["CONSUMER_KEY"], |
| 29 | + consumer_secret=self.account_cred["CONSUMER_SECRET"], |
| 30 | + access_token=self.account_cred["ACCESS_TOKEN"], |
| 31 | + access_token_secret=self.account_cred["ACCESS_TOKEN_SECRET"], |
| 32 | + ) |
| 33 | + return tweepy.API(auth) |
| 34 | + |
| 35 | + @cached_property |
| 36 | + def tweepy_client(self): |
| 37 | + # Client for Twitter API v2 |
| 38 | + |
| 39 | + import tweepy # pylint:disable=import-outside-toplevel |
| 40 | + return tweepy.Client( |
| 41 | + consumer_key=self.account_cred["CONSUMER_KEY"], |
| 42 | + consumer_secret=self.account_cred["CONSUMER_SECRET"], |
| 43 | + access_token=self.account_cred["ACCESS_TOKEN"], |
| 44 | + access_token_secret=self.account_cred["ACCESS_TOKEN_SECRET"], |
| 45 | + ) |
| 46 | + |
| 47 | + @property |
| 48 | + def bluesky_handle(self): |
| 49 | + return self.account_cred["ATPROTO_HANDLE"] |
| 50 | + |
| 51 | + @cached_property |
| 52 | + def atproto_client(self): |
| 53 | + from atproto import Client # pylint:disable=import-outside-toplevel |
| 54 | + client = Client() |
| 55 | + client.login( |
| 56 | + self.account_cred["ATPROTO_HANDLE"], |
| 57 | + self.account_cred["ATPROTO_PASSWORD"], |
| 58 | + ) |
| 59 | + return client |
| 60 | + |
| 61 | + def create_tweet(self, text: str, media_files: Optional[List[Path]] = None): |
| 62 | + if media_files is None: |
| 63 | + media_ids = [] |
| 64 | + else: |
| 65 | + medias = (self.tweepy_api.media_upload(filepath) for filepath in media_files) |
| 66 | + media_ids = [media.media_id for media in medias] |
| 67 | + |
| 68 | + resp = self.tweepy_client.create_tweet( |
| 69 | + text=text, |
| 70 | + media_ids=media_ids, |
| 71 | + ) |
| 72 | + return resp.data["id"] |
| 73 | + |
| 74 | + def create_bluesky_post( |
| 75 | + self, |
| 76 | + text, |
| 77 | + embed_video: Optional[Entity] = None, |
| 78 | + image_files: Optional[List[Path]] = None, |
| 79 | + image_alts: Optional[List[str]] = None, |
| 80 | + ): |
| 81 | + from atproto import models # pylint:disable=import-outside-toplevel |
| 82 | + if image_files is None: |
| 83 | + if embed_video is None: |
| 84 | + embed = None |
| 85 | + else: |
| 86 | + preview_response = requests.get( |
| 87 | + f"https://api.tournesol.app/preview/entities/{embed_video.uid}", |
| 88 | + timeout=10, |
| 89 | + ) |
| 90 | + preview_response.raise_for_status() |
| 91 | + img_data = preview_response.content |
| 92 | + thumb_blob = self.atproto_client.upload_blob(img_data).blob |
| 93 | + embed = models.AppBskyEmbedExternal.Main( |
| 94 | + external=models.AppBskyEmbedExternal.External( |
| 95 | + title=embed_video.metadata.get("name", ""), |
| 96 | + description=embed_video.metadata.get("uploader", ""), |
| 97 | + uri=f"https://tournesol.app/entities/{embed_video.uid}", |
| 98 | + thumb=thumb_blob, |
| 99 | + ) |
| 100 | + ) |
| 101 | + resp = self.atproto_client.send_post( |
| 102 | + text=text, |
| 103 | + embed=embed, |
| 104 | + langs=[self.language], |
| 105 | + ) |
| 106 | + else: |
| 107 | + resp = self.atproto_client.send_images( |
| 108 | + text=text, |
| 109 | + langs=[self.language], |
| 110 | + images=[p.read_bytes() for p in image_files], |
| 111 | + image_alts=image_alts, |
| 112 | + ) |
| 113 | + return resp.uri |
0 commit comments