memegen

create memes from name of meme, top text, bottom text. Reads this data from config file. need to add url args that opens and writes args to config file.

memegen

config file that script reads the meme image to search for. text0 (top), text1 (bottom).

Link a meme to a user.

storage of json files for every meme created.

create json file of result. includes user, name, text, url link.

use artcgallery config that reads the next upcoming blog title and create meme from it. Also read tags,

generate config by sending url. flask restful.

add auth key.

download all meme images and stop making request to imgflip. Make one request and then just access the images ocally.

create image from text0 and text1.

search for meme by id or name


In [1]:
import requests
import getpass
import shutil
import PIL
import json
from PIL import ImageDraw, ImageFont
import os
import arrow
import configparser

In [ ]:


In [2]:
myusr = getpass.getuser()


reqimg = requests.get('https://api.imgflip.com/get_memes')

reqjsn = (reqimg.json())

gtmem = (reqjsn['data']['memes'])


# In[ ]:




# In[4]:

#for gtm in gtmem:
#    print(gtm)
#    #grrjs = json.loads(gtm)
#    #print(grrjs)
#    gtm.update({'imgpath' : '/galleries/{}.jpg'.format(gtm['id'])})
#    print(gtm['url'])
#    response = requests.get(gtm['url'], stream=True)
#    
#    with open('/home/{}/memetest/galleries/default/{}.jpg'.format(myusr, gtm['id']), 'wb') as out_file:
#        shutil.copyfileobj(response.raw, out_file)
#        del response


# In[25]:

os.listdir('/home/{}/memetest/galleries/'.format(myusr))


# In[ ]:




# In[ ]:




# In[6]:

#specmem = input('what name of meme: ')


# In[47]:

config = configparser.RawConfigParser()
config.read('/home/{}/.config/memegen.ini'.format(myusr))

# getfloat() raises an exception if the value is not a float
# getint() and getboolean() also do this for their respective types
defpath = (config.get('default', 'defaultpath'))
specmem = (config.get('default', 'memename'))
textzero = config.get('default', 'toptext')
textone = config.get('default', 'bottomtext')



#textzero = input('top text: ')
#textone = input('bottom text: ')

upzero = textzero.upper()

botzero = textone.upper()


lentop = len(textzero)
lenbotm = len(textone)



if lenbotm < 5:
    fontbot = 72
elif lenbotm > 5:
    fontbot = 42

if lentop < 5:
    fontsize = 72
elif lentop > 5:
    fontsize = 42


'''
from flask import request
import flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def api_hello():
    if 'memename' in request.args:
        return request.args['memename']
        for gtm in gtmem:
            #print(gtm)
            grnam = request.args['memename']
            if specmem in grnam:
                return(gtm['url'])
                #print(gtm['id'])
    if 'textzero' in request.args:
        return request.args['textzero']
    if 'textone' in request.args:
        return request.args['textone']
    else:
        return 'Hello John Doe'
'''


'''
parser = reqparse.RequestParser()
parser.add_argument('rate', type=int, help='Rate cannot be converted')
parser.add_argument('name')
args = parser.parse_args()
'''

#from flask import Flask, request

#app = Flask(__name__)


#@app.route('/api/foo/', methods=['GET'])
#def foo():
#    bar = request.args.to_dict()
#    print bar
#    return 'success', 200

#if __name__ == '__main__':   
#    app.run(debug=True)


idict = dict()


timnow = arrow.now()


timnow.for_json()


for gtm in gtmem:
    #print(gtm)
    grnam = gtm['name']
    
    if specmem in grnam:
        print(grnam)
        print(gtm)
        print(gtm['url'])
        print(gtm['id'])
        gheigh = (gtm['height'])
        gwth = (gtm['width'])
        response = requests.get(gtm['url'], stream=True)
        
        with open('/home/{}/memetest/galleries/{}.jpg'.format(myusr, gtm['id']), 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
            del response
            
        meing = PIL.Image.open(('/home/{}/memetest/galleries/{}.jpg'.format(myusr, gtm['id'])))
        #meing
        medraw = ImageDraw.Draw(meing)
        #font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 32)
        #font = ImageFont.truetype("/home/wcm/Downloads/fashi954.ttf", 12)
        fontwo = ImageFont.truetype('/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf', fontsize)
        botfont = ImageFont.truetype('/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf', fontbot)

        medraw.text(((gwth - gheigh / 1.5), 10), upzero, (255,255,255), font=fontwo)
        medraw.text(((gwth - gheigh / 1.25), gheigh - 80), botzero, (255,255,255), font=botfont)
        idict.update({gtm['id'] : dict({'id' : gtm['id']})})

        meing.save('/home/{}/memetest/galleries/{}.jpg'.format(myusr, gtm['id']))
        
        #with open('/home/{}/memetest/posts/{}.md'.format(myusr, gtm['id']), 'w') as resulmd:
        #    resulmd.write(str(gtm['id']) + ' \n' + str(elebody.text))
            
        #with open ('/home/{}/memetest/posts/{}.meta'.format(myusr, gtm['id']), 'w') as opmetat:
                #opmetat.write("{}".format(str(curtim))
            #for arage in alltags:
            #    print(arage)
        #    opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(gtm['id'], gtm['id'], timnow.for_json()))

            
        #template = Template('Hello {{ name }}!')
        #template.render(name='William')

        #print(gtm['url'])


# In[67]:

with open('/home/{}/memetest/posts/{}.md'.format(myusr, str(gtm['id'])), 'w') as resulmd:
            resulmd.write('{}\n\n![{}](/galleries/{})\n\n{}\n'.format(upzero, str(gtm['id']), str(gtm['id']) + '.jpg', botzero))
            
with open ('/home/{}/memetest/posts/{}.meta'.format(myusr, gtm['id']), 'w') as opmetat:
                #opmetat.write("{}".format(str(curtim))
            #for arage in alltags:
            #    print(arage)
    opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(gtm['id'], gtm['id'], timnow.for_json()))


# In[ ]:


3
16
Grandma Finds The Internet
{'id': '61556', 'url': 'https://i.imgflip.com/1bhw.jpg', 'height': 480, 'name': 'Grandma Finds The Internet', 'width': 640}
https://i.imgflip.com/1bhw.jpg
61556

In [ ]: