def get_user(username: str, ctx: SlashContext, blossom_api: BlossomAPI) -> Optional[BlossomUser]: """Get the given user from Blossom. Special keywords: - "me": Returns the user executing the command (from the SlashContext). - "all"/"everyone"/"everybody": Stats for all users, returns None. If the user could not be found, a UserNotFoundException is thrown. """ if username.casefold() in ["all", "everyone", "everybody"]: # Handle command execution for everyone return None # Handle command execution for the current user _username = ctx.author.display_name if username.casefold( ) == "me" else username _username = extract_username(_username) user_response = blossom_api.get_user(_username) if user_response.status != BlossomStatus.ok: raise UserNotFoundException(_username) user = user_response.data if user["gamma"] == 0: # We don't have stats on new users # They would often create an error so let's just handle them separately raise NewUserException(user["username"]) return user
def blossom(self): return BlossomAPI( email=os.environ["BLOSSOM_EMAIL"], password=os.environ["BLOSSOM_PASSWORD"], api_key=os.environ["BLOSSOM_API_KEY"], api_base_url=os.environ["BLOSSOM_API_URL"], )
async def _get_user_progress( user: Optional[BlossomUser], after_time: Optional[datetime], before_time: Optional[datetime], blossom_api: BlossomAPI, ) -> int: """Get the number of transcriptions made in the given time frame.""" from_str = after_time.isoformat() if after_time else None until_str = before_time.isoformat() if before_time else None # We ask for submission completed by the user in the time frame # The response will contain a count, so we just need 1 result progress_response = blossom_api.get( "submission/", params={ "completed_by": get_user_id(user), "complete_time__gte": from_str, "complete_time__lte": until_str, "page_size": 1, }, ) if progress_response.status_code != 200: raise BlossomException(progress_response) return progress_response.json()["count"]
def setup(bot: ButtercupBot) -> None: """Set up the Find cog.""" cog_config = bot.config["Blossom"] email = cog_config.get("email") password = cog_config.get("password") api_key = cog_config.get("api_key") blossom_api = BlossomAPI(email=email, password=password, api_key=api_key) bot.add_cog(Find(bot=bot, blossom_api=blossom_api))
def setup(bot: ButtercupBot) -> None: """Set up the History cog.""" # Initialize blossom api cog_config = bot.config["Blossom"] email = cog_config.get("email") password = cog_config.get("password") api_key = cog_config.get("api_key") blossom_api = BlossomAPI(email=email, password=password, api_key=api_key) bot.add_cog(History(bot=bot, blossom_api=blossom_api))
def get_user_gamma(user: Optional[BlossomUser], blossom_api: BlossomAPI) -> int: """Get the gamma of the given user. If it is None, it will get the total gamma of everyone. This makes a server request, so the result should be reused. """ if user: return user["gamma"] gamma_response = blossom_api.get( "submission/", params={ "page_size": 1, "completed_by__isnull": False }, ) if not gamma_response.ok: raise BlossomException(gamma_response) return gamma_response.json()["count"]
client_id=os.environ.get("reddit_client_id"), client_secret=os.environ.get("reddit_secret"), user_agent=os.environ.get("reddit_user_agent"), ) app = App( signing_secret=os.environ.get("slack_signing_secret"), token=os.environ.get("slack_oauth_token"), ) if ENABLE_BLOSSOM: # Feature flag means that we can lock away blossom functionality until # blossom is actually ready to roll. blossom = BlossomAPI( email=os.getenv("blossom_email"), password=os.getenv("blossom_password"), api_key=os.getenv("blossom_api_key"), api_base_url=os.getenv("blossom_api_url"), ) print("blossom loaded!") else: blossom = mock.MagicMock() # There is an overloaded __get__ in the underlying Bolt app, so this type # doesn't resolve cleanly. ME: str = app.client.auth_test().data["user_id"] # type: ignore # Slack will send the internal ID to represent the user, so we need to # dynamically add that ID so we can listen for it. This will change # per workspace, so it can't be hardcoded. COMMAND_PREFIXES = (USERNAME, f"@{USERNAME}", ME, f"<@{ME}>") # The above prefixes can trigger anywhere in a sentence; the below ones
import os from datetime import datetime, timedelta from typing import List, Optional import dotenv from blossom_wrapper import BlossomAPI from praw import Reddit dotenv.load_dotenv() blossom: BlossomAPI = ( BlossomAPI( email=os.environ.get("BLOSSOM_EMAIL"), password=os.environ.get("BLOSSOM_PASSWORD"), api_key=os.environ.get("BLOSSOM_API_KEY"), # Set this to https://grafeas.org/api/ if using in production api_base_url=os.environ.get("BLOSSOM_API_BASE_URL") or "http://localhost:8000/api/", ) if os.environ.get("BLOSSOM_EMAIL") else None) REDDIT = (Reddit( client_id=os.getenv("REDDIT_CLIENT_ID"), client_secret=os.getenv("REDDIT_SECRET"), password=os.getenv("REDDIT_PASSWORD"), username=os.getenv("REDDIT_USERNAME"), user_agent=os.getenv("REDDIT_USER_AGENT"), ) if os.getenv("REDDIT_CLIENT_ID") else None) # The path to the log file LOG_FILE_PATH: str = os.environ.get("LOG_FILE_PATH") or os.path.join( os.path.dirname(__file__), "bootstrap.log")