def create_reply(self): message = "" text = self.text text = text.strip() if text_contains_emoji(text): emoji = text else: emoji = emojis_for_emoticons.get(text, None) if not emoji: raise RuntimeError("Improperly identified message type") # If it's a multi-emoji that we don't track, just grab the first emoji. # TODO: abstract out use of `keys()`. if len(emoji) > 1 and emoji not in google_spreadsheets.keys(): emoji = text[0] # Check for skin-toned emojis. # (This only handles the one-emoji case for now.) if not emoji_contains_skin_tone(text) and not emoji_is_numeric(text): message = render_template('txt/requested_emoji_does_not_exist.txt', requested_emoji=text, fallback_emoji=emoji) # Time to grab the filepath for the emoji! corgi_urls = api.get(emoji)['results'] # If that still doesn't work, we'll just grab a random one. if not corgi_urls: logger.warn("Couldn't find corgi for {}. Using random one.".format( emoji)) while not corgi_urls: results = api.get() emoji, corgi_urls = results['emoji'], results['results'] message = render_template('txt/requested_emoji_does_not_exist.txt', requested_emoji=text, fallback_emoji=emoji) corgi_url = random.choice(corgi_urls) if message == "": supplements = get_supplement_messsage(self.phone_number, self.text) if supplements: message = supplements return create_response(message, image_url=corgi_url)
def get_all(self): all_emojis = google_spreadsheets.keys(include_empty_keys=True) corjis = [] for this_emoji in all_emojis: corgi_urls = "" if settings.Config.REMOTE_CACHE_RETRIEVE: try: corgi_urls = s3.get_all(this_emoji) except CorgiNotFoundException as e: logger.warn("Corji not found for emoji %s", this_emoji) if not corgi_urls: corgi_urls = google_spreadsheets.get_all(this_emoji) emoji_name = emoji.demojize(this_emoji).replace(":", "") corjis.append({ "urls": corgi_urls, "emoji": this_emoji, "emoji_name": emoji_name }) return { "count": len(corjis), "emojis": [corji["emoji"] for corji in corjis], "results": corjis }
def get(self, emoji=None): """Returns an emoji associated with the given emoji. If no emoji is supplied, a random corji is returned.""" if not emoji: emoji = random.choice(google_spreadsheets.keys()) corgi_urls = [] # Check for skin-toned emojis and fallback to the undecorated one. if len(emoji) == 2 and emoji_contains_skin_tone(emoji): emoji = emoji[0] # First we'll try using S3. if settings.Config.REMOTE_CACHE_RETRIEVE: try: corgi_urls = s3.get_all(emoji) except CorgiNotFoundException as e: logger.warn("Corji not found for emoji %s", emoji) # If S3 is a no-go, fall back to Spreadsheets. if not corgi_urls: corgi_urls = google_spreadsheets.get_all(emoji) # TODO: do this smarter somehow. for url in corgi_urls: try: requests.get(url) except Exception as e: logger.warn("URL {} is invalid; not returning.".format(url)) corgi_urls.remove(url) return { "count": len(corgi_urls), "emoji": emoji if text_contains_emoji(emoji) else "", "results": corgi_urls }
import random from twilio.rest import TwilioRestClient from corji.api import CorgiResource from corji.data_sources import ( google_spreadsheets ) from corji.settings import Config api = CorgiResource() client = TwilioRestClient(Config.TWILIO_ACCOUNT_SID, Config.TWILIO_AUTH_TOKEN) number_of_corgis_to_send = 2 corgis = [] while len(corgis) < number_of_corgis_to_send: random_emoji = random.choice(google_spreadsheets.keys()) results = api.get(random_emoji)['results'] if len(results): for result in results: if result not in corgis: corgis.append(result) for corgi in corgis: message = client.messages.create( media_url=corgi, to="8046989478", from_=Config.TWILIO_PHONE_NUMBER )
from tabulate import tabulate from corji.data_sources import google_spreadsheets from corji.logging import Logger from corji.settings import Config logger = Logger(Config.LOGGER_NAME, Config.LOG_PATH, Config.LOG_NAME) SPREADSHEET_URL = Config.SPREADSHEET_URL if __name__ == "__main__": logger.debug("START: Spreadsheet URL defined: %s", SPREADSHEET_URL) google_spreadsheets.load(SPREADSHEET_URL) corgi_counter = Counter() keys = google_spreadsheets.keys(include_empty_keys=True) for emoji in keys: corgis = google_spreadsheets.get_all(emoji) for corgi in corgis: try: requests.get(corgi) except: print("FAILURE: {}".format(corgi)) corgi_counter[len(corgis)] += 1 print(tabulate(corgi_counter.items(), headers=["# of URLs", "Count"], tablefmt="psql"))