def justxthingshandler(jenni, msg): from modules.brittbot.pil import justxthings from modules.find import load_db hashtag = msg.groups()[1] quote = msg.groups()[0] if not quote: quotes = load_db().get(msg.sender) if quotes and 'last_said' in quotes: quotes['last_said'] = [quote for quote in quotes['last_said'] if not re.match(r'(.*)(#\S+\w)$', quote)] quote = ''.join(quotes['last_said'][-1].split(':')[1:]) if quote[0] in ['!'] or 'http' in quote: return if hashtag.startswith("##"): return if "action" in msg.lower(): return if len(quote.split(' ')) > 25: return url = "http://brittbot.brittg.com/{}".format(justxthings.generate_image( str(quote), hashtag, )) jenni.reply(url)
def justxthingshandler(jenni, msg): from modules.brittbot.pil import justxthings from modules.find import load_db hashtag = msg.groups()[1] quote = msg.groups()[0] if not quote: quotes = load_db().get(msg.sender) if quotes and 'last_said' in quotes: quotes['last_said'] = [quote for quote in quotes['last_said'] if not re.match(r'(.*)(#\S+\w)$', quote)] quote = ''.join(quotes['last_said'][-1].split(':')[1:]) if quote[0] in ['!'] or 'http' in quote: return if hashtag.startswith("##"): return if len(quote.split(' ')) > 25: return url = "http://brittbot.brittg.com/{}".format(justxthings.generate_image( str(quote), hashtag, )) jenni.reply(url)
def grabquote(jenni, input): try: from modules import find except: return jenni.say('Could not load "find" module.') txt = input.group(2) parts = txt.split() if not parts: return jenni.say('Please provide me with a valid nick.') nick = parts[0] channel = input.sender channel = (channel).lower() quote_db = find.load_db() if quote_db and channel in quote_db and nick in quote_db[channel]: quotes_by_nick = quote_db[channel][nick] else: return jenni.say('There are currently no existing quotes by the provided nick in this channel.') quote_by_nick = quotes_by_nick[-1] quote = '<%s> %s' % (nick, quote_by_nick) write_addquote(quote) jenni.say('quote added: %s' % (quote))
def urlshortner(jenni, msg): import requests import urllib from modules.find import load_db, save_db in_url = msg.groups()[0] if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+)" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = urls[0] break if not in_url: return shrls_server = jenni.config.shrls.get('server') shrls_username = jenni.config.shrls.get('username') shrls_password = jenni.config.shrls.get('password') response = requests.get('{}/admin/create'.format(shrls_server), params={ 'u': in_url, 'c': jenni.nick, 'url_only': True, }, auth=(shrls_username, shrls_password)) if response.status_code != 200: jenni.reply("An error has occurred.") return url = response.content jenni.reply(url)
def img_zoom(jenni, msg): url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.zoom(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def img_zoom(jenni, msg): from modules.brittbot.pil import enhance from modules.find import load_db, save_db url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.zoom(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def img_enhance(jenni, msg): from modules.brittbot.pil import enhance from modules.find import load_db, save_db url = msg.groups()[0] if not url: imgs = load_db().get(msg.sender) if imgs and "last_said" in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg|gif))" for img in reversed(imgs["last_said"]): urls = re.findall(url_regex, img) if urls: url = random.choice(urls) break if not url: return filename = enhance.enhance(url) url = "http://brittbot.brittg.com/{}".format(filename) msgs = load_db() msgs[msg.sender]["last_said"].append(url) save_db(msgs) jenni.reply(url)
def markov_generator(jenni, msg): from modules.find import load_db, save_db target = msg.groups()[0] if not target: target = 'last_said' else: target = target.strip() msgs = load_db().get(msg.sender).get(target) if target.lower() == 'everything': db = load_db().get(msg.sender) msgs = [] for key in db.keys(): msgs += db[key] if not msgs: return msgs = [''.join(x.split(':')[1:]) for x in msgs] t = {} for line in msgs: try: ngrams = TextBlob(" ".join(line.split(" ")[2:])).ngrams(n=3) except Exception: pass for ngram in ngrams: grams = list(ngram) word = grams[0] if word not in t: t[word] = [] t[word].append(grams[1:]) reply = [random.choice(t.keys())] for _ in range(random.randint(5,13)): words = t.get(reply[-1]) if not words: break words = random.choice(words) reply += words reply = ' '.join(reply) jenni.say(reply)
def justxthingshandler(jenni, msg): from modules.brittbot.pil import justxthings from modules.find import load_db hashtag = msg.groups()[1] quote = msg.groups()[0] if not quote: quotes = load_db().get(msg.sender) if quotes and "last_said" in quotes: quotes["last_said"] = [quote for quote in quotes["last_said"] if not re.match(r"(.*)(#\S+\w)$", quote)] quote = "".join(quotes["last_said"][-1].split(":")[1:]) if quote[0] in ["!"] or "http" in quote: return if hashtag.startswith("##"): return if len(quote.split(" ")) > 25: return url = "http://brittbot.brittg.com/{}".format(justxthings.generate_image(str(quote), hashtag)) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return in_url = msg.groups()[0] if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder({ 'title': 'wat', 'description': 'none', 'filter': 'trippy', 'submit': "Let's Dream", 'image': ('img0.jpg', open('/tmp/img.jpg', 'rb'), 'image/jpeg'), }) response = requests.post( 'https://dreamscopeapp.com/api/images', data=data, verify=False, headers={ 'Content-Type': data.content_type } ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = re.findall(r'pageUrl\(\'(\S+)\'\)', response.text)[-1] for _ in range(15): time.sleep(1.5) r = requests.get('https://dreamscopeapp.com/api/images/{}'.format(image_id)) final_url = r.json()['filtered_url'] if final_url: break if not final_url: jenni.reply('Deep dream took too long to complete. Try a smaller image.') return jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ['lsd', 'deepdream']: img_filter = 'trippy' if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder({ 'title': 'wat', 'description': 'none', 'filter': img_filter, 'submit': "Let's Dream", 'image': ('img0.jpg', open('/tmp/img.jpg', 'rb'), 'image/jpeg'), }) response = requests.post( 'https://dreamscopeapp.com/api/images', data=data, verify=False, headers={ 'Content-Type': data.content_type } ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = json.loads(response.text)['uuid'] for _ in range(15): time.sleep(1.5) r = requests.get('https://dreamscopeapp.com/api/images/{}'.format(image_id)) final_url = r.json()['filtered_url'] if final_url: break if not final_url: jenni.reply('Deep dream took too long to complete. Try a smaller image.') return jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): import requests from requests_toolbelt import MultipartEncoder import urllib import uuid from modules.find import load_db, save_db if "last_trip" in jenni.brain and time.time() - jenni.brain["last_trip"] < 30: jenni.reply("Tripping too hard right now, please try again in 30 seconds.") return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ["lsd", "deepdream"]: img_filter = "trippy" if not in_url: imgs = load_db().get(msg.sender) if imgs and "last_said" in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs["last_said"]): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open("/tmp/img.jpg", "wb") f.write(urllib.urlopen(in_url).read()) f.close() data = MultipartEncoder( { "title": "wat", "description": "none", "filter": img_filter, "submit": "Let's Dream", "image": ("img0.jpg", open("/tmp/img.jpg", "rb"), "image/jpeg"), } ) response = requests.post( "https://dreamscopeapp.com/api/images", data=data, verify=False, headers={"Content-Type": data.content_type} ) if response.status_code >= 400: print response print response.content jenni.reply("An error has occurred.") image_id = json.loads(response.text)["uuid"] for _ in range(15): time.sleep(1.5) r = requests.get("https://dreamscopeapp.com/api/images/{}".format(image_id)) final_url = r.json()["filtered_url"] if final_url: break if not final_url: jenni.reply("Deep dream took too long to complete. Try a smaller image.") return jenni.brain["last_trip"] = time.time() img = urllib.urlopen(final_url).read() imagepath = "/var/www/htdocs/brittbot/" imagename = "%s.jpg" % str(uuid.uuid4()).replace("-", "")[0:8] f = open(imagepath + imagename, "w") f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]["last_said"].append(url) save_db(msgs) jenni.reply(url)
def deepdream(jenni, msg): if 'last_trip' in jenni.brain and time.time() - jenni.brain['last_trip'] < 30: jenni.reply('Tripping too hard right now, please try again in 30 seconds.') return img_filter = msg.groups()[0] in_url = msg.groups()[1] if img_filter in ['lsd', 'deepdream']: img_filter = 'trippy' if not in_url: imgs = load_db().get(msg.sender) if imgs and 'last_said' in imgs: url_regex = "(https?://\S+\.(?:jpg|png|jpeg))" for img in reversed(imgs['last_said']): urls = re.findall(url_regex, img) if urls: in_url = random.choice(urls) break if not in_url: return f = open('/tmp/img.jpg', 'wb') f.write(urllib.urlopen(in_url).read()) f.close() if in_url.endswith('.gif'): from PIL import ( Image, ImageSequence, ) import io from modules.brittbot.pil.images2gif import writeGif img = Image.open('/tmp/img.jpg') if hasattr(img, 'format') and img.format.lower() == 'gif': if not msg.admin: return original_duration = img.info['duration'] frames = [frame.copy() for frame in ImageSequence.Iterator(img)] if len(frames) > 40: jenni.reply("Too many frames :(") return final_frames = [] for i, frame in enumerate(frames): im = frame.convert('RGBA') im.save('/tmp/frame{}.jpg'.format(i), 'jpeg', quality=100) print "Processing frame {}...".format(i) url = process_deepdream(open('/tmp/frame{}.jpg'.format(i), 'rb'), img_filter) im = Image.open(io.BytesIO(urllib.urlopen(url).read())) final_frames.append(im) time.sleep(2) imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.gif" % str(uuid.uuid4()).replace('-', '')[0:8] writeGif(imagepath + imagename, final_frames, duration=original_duration/1000.0, dither=0) else: final_url = process_deepdream(open('/tmp/img.jpg', 'rb'), img_filter) jenni.brain['last_trip'] = time.time() img = urllib.urlopen(final_url).read() imagepath = '/var/www/htdocs/brittbot/' imagename = "%s.jpg" % str(uuid.uuid4()).replace('-', '')[0:8] f = open(imagepath + imagename, 'w') f.write(img) f.close() url = "http://brittbot.brittg.com/{}".format(imagename) msgs = load_db() msgs[msg.sender]['last_said'].append(url) save_db(msgs) jenni.reply(url)