Write your post here.
In [4]:
import arrow
In [5]:
#configparser.p
In [6]:
thtim = arrow.now()
In [ ]:
timnow = arrow.now()
In [8]:
thtim.timestamp
Out[8]:
In [2]:
#import requests
#import bs4
In [ ]:
In [22]:
#for memereq in range(0,21):
# memetm = requests.get('https://imgflip.com/memetemplates?page={}'.format(memereq))
# bsme = bs4.BeautifulSoup(memetm.text)
#print(bsme)
# for ahr in bsme.find_all('h3'):
# print(ahr)
#print(ahr.find('a'))
In [ ]:
In [ ]:
# coding: utf-8
# 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 locally.
#
#
#
# create image from text0 and text1.
#
# search for meme by id or name.
#
# local data base of memes. able to add more image memes to database.
#
# hey gurl. give dimensions and pos that text wrap around
#
#
# In[234]:
import requests
import getpass
import shutil
import PIL
import json
from PIL import ImageDraw, ImageFont
import os
import bs4
import configparser
import subprocess
#import tweepy
#import facebook
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
from shutil import copyfile
import pickle
# In[152]:
myusr = getpass.getuser()
# In[153]:
#def memedata():
# reqimg = requests.get('https://api.imgflip.com/get_memes')
# reqjsn = (reqimg.json())
# return reqjsn['data']['memes']
#def memeadd():
# return memedata()
#return requests.get('https://api.imgflip.com/get_memes')
# In[154]:
#with open('/home/{}/memetest'.format(myusr))
# In[ ]:
# In[155]:
#reqimg = requests.get('https://api.imgflip.com/get_memes')
#reqjsn = (reqimg.json())
#gtmem = (reqjsn['data']['memes'])
# In[ ]:
# In[186]:
#with open('/home/{}/memes.json'.format(myusr), 'w') as mejs:
#print(mejs)import json
#with open('data.txt', 'w') as outfile:
# json.dump(reqjsn, mejs)
#mejs.write(reqjsn)
# In[238]:
with open('/home/{}/meme.pickle'.format(myusr), 'rb') as handle:
memelis = pickle.load(handle)
#print (memelis)
#with open('/home/{}/memedata.pickle'.format(myusr), 'rb') as hand:
# dicinf = pickle.load(hand)
#dicinf = dict({str(thtim.timestamp + usrfolz : dict({'musr' : myusr, 'user' : usrfolz, 'memeid' : gtm['id'], 'memename' : gtm['name'], 'uptext' : upzero, 'bottext' : botzero, 'img' : '/{}/galleries/{}.jpg'.format(usrfolz, gtm['id'])})})
# In[227]:
#with open('/home/{}/memes.json'.format(myusr), 'r') as mejsz:
#print(mejs)
#print(mejsz.read())
# merd = mejsz.read()
# In[228]:
#nerduc = json.loads(merd)
# In[229]:
#gtmem = nerduc['data']['memes']
# In[6]:
#tyro = Image.open('/home/{}/memetest/galleries/default/{}.png')
# In[7]:
#tyro.size[0]
# In[8]:
#tydic = dict({'id': 123, 'name' : 'toast', 'width' : tyro.size[0], 'height' : tyro.size[1]})
# In[9]:
#tydic
# In[10]:
#gtmem.append(tydic)
# In[239]:
for gtm in memelis:
print('# ' +gtm['name'])
print('# ' +gtm['id'])
# In[13]:
'''
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[14]:
#os.listdir('/home/{}/memetest/galleries/'.format(myusr))
# In[ ]:
# In[ ]:
# In[15]:
#specmem = input('what name of meme: ')
#try:
#shutil.copy('/home/{}/memetest/ /home/{}/memesite/{}/'.format(myusr, myusr, usrfolz))
# os.mkdir('/home/{}/userconfig/{}.ini'.format(myusr, usrfolz))
# subprocess.call('rsync -rv /home/{}/git/act.ini /home/{}/userconfig/{}.ini'.format(myusr, myusr, usrfolz), shell=True)
#/home/{}/git/act.ini'.format(myusr)
#except FileExistsError:
#subprocess.call('rsync -rv /home/{}/memetest/ /home/{}/memesite/{}'.format(myusr, myusr, usrfolz), shell=True)
# pass
#print('no dir created')
# In[245]:
config = configparser.RawConfigParser()
#config.read('/home/{}/userconfig/{}.ini'.format(myusr, usrfolz))
config.read('/home/{}/git/act.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')
usrfolz = config.get('default', 'usrfolz')
#toptxfil = config.get('default', 'toptxfil')
#toptxstk = config.get('default', 'toptxstk')
#bottxfil = config.get('default', 'bottxfil')
#bottxstk = config.get('default', 'bottxstk')
newfilid = config.get('default', 'newfilid')
newfilna = config.get('default', 'newfilna')
newfilloc = config.get('default', 'newfilloc')
newfiltf = config.get('default', 'newfiltf')
toptxfil0 = config.get('default', 'toptxfil0')
toptxfil1 = config.get('default', 'toptxfil1')
toptxfil2 = config.get('default', 'toptxfil2')
toptxstk0 = config.get('default', 'toptxstk0')
toptxstk1 = config.get('default', 'toptxstk1')
toptxstk2 = config.get('default', 'toptxstk2')
bottxfil0 = config.get('default', 'bottxfil0')
bottxfil1 = config.get('default', 'bottxfil1')
bottxfil2 = config.get('default', 'bottxfil2')
bottxstk0 = config.get('default', 'bottxstk0')
bottxstk1 = config.get('default', 'bottxstk1')
bottxstk2 = config.get('default', 'bottxstk2')
specslug = specmem.replace(' ', '-')
speclow = specslug.lower()
upzero = textzero.upper()
botzero = textone.upper()
# In[249]:
try:
#shutil.copy('/home/{}/memetest/ /home/{}/memesite/{}/'.format(myusr, myusr, usrfolz))
os.mkdir('/home/{}/memesite/{}'.format(myusr, usrfolz))
subprocess.call('rsync -rv /home/{}/memetest/ /home/{}/memesite/{}'.format(myusr, myusr, usrfolz), shell=True)
subprocess.call('rsync -rv /home/{}/git/act.ini /home/{}/memesite/{}'.format(myusr, myusr, usrfolz), shell=True)
#/home/{}/git/act.ini'.format(myusr)
except FileExistsError:
#subprocess.call('rsync -rv /home/{}/memetest/ /home/{}/memesite/{}'.format(myusr, myusr, usrfolz), shell=True)
pass
#print('no dir created')
# In[ ]:
# In[ ]:
# In[220]:
#type(gtmem)
# In[230]:
if newfiltf == 'y':
print('its true')
copyfile(newfilloc, '/home/{}/memetest/galleries/default/{}.jpg'.format(myusr, newfilid))
tyro = Image.open('/home/{}/memetest/galleries/default/{}.jpg'.format(myusr, newfilid))
tydic = dict({'id': newfilid, 'name' : newfilna, 'width' : tyro.size[0], 'height' : tyro.size[1]})
#nerduc.update(dict({'data': dict({'memes' :tydic})}))
memelis.append(tydic)
with open('/home/{}/meme.pickle'.format(myusr), 'wb') as handle:
pickle.dump(memelis, handle, protocol=pickle.HIGHEST_PROTOCOL)
else:
print('its false')
# In[233]:
#reqjsn
# In[231]:
#with open('/home/{}/memes.json'.format(myusr), 'w') as mejs:
#print(mejs)import json
#with open('data.txt', 'w') as outfile:
#json.dump(reqjsn, mejs)
#mejs.write(reqjsn)
#gtmem
# In[224]:
#nerduc
# In[212]:
#type(newfiltf)
# In[203]:
#tyro = Image.open('/home/{}/memetest/galleries/default/{}.jpg'.format(myusr,newfil))
# In[ ]:
#tydic = dict({'id': 123, 'name' : 'toast', 'width' : tyro.size[0], 'height' : tyro.size[1]})
# In[204]:
#tyro.size
# In[110]:
#with open('/home/{}/config.txt'.format(myusr), 'r') as wckz:
# allkey = wckz.readlines()
# OAUTH_TOKEN = allkey[0]
#print(OAUTH_TOKEN)
# OAUTH_SECRET = allkey[1]
# CONSUMER_KEY = allkey[2]
# CONSUMER_SECRET = allkey[3]
# In[240]:
#OAUTH_TOKEN
# In[112]:
#auth = tweepy.OAuthHandler(CONSUMER_KEY.strip('\n'), CONSUMER_SECRET.strip('\n'))
#auth.set_access_token(OAUTH_TOKEN.strip('\n'), OAUTH_SECRET.strip('\n'))
# In[113]:
#api = tweepy.API(auth)
# In[ ]:
#api.update_with_media('{}{}/{}'.format(gifpat, namofgifsea, ranlocgif), status='Started typing script {} {}'.format(blognam, jointag))
# In[97]:
#import facebook
#graph = facebook.GraphAPI(access_token='EAACEdEose0cBAFUPIWMm3ti6CFZBYwnsU7pOY3L0aRlmFpxqC9VUpzLbN6hpD3Od3pbqdYFMt6S0ykEDoAQ4hhNK8Vs72ZAQudNncuhgU9IWNYkGFh7MKlH65BhRR1KvWG65zROJKo4mV1AlBVwUhR6aEy6PLeZBirqe76YZA1qGkVfk8olIWcwSv6O7mL667sikFyE8xgZDZD', version="2.1")
# In[158]:
for gtm in memelis:
#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('{}{}-reference.jpg'.format(repathz, str(rdz.author)), 'wb') as out_file:
# shutil.copyfileobj(response.raw, out_file)
# del response
#with open('/home/{}/memetest/galleries/{}.png'.format(myusr, gtm['id']), 'wb') as out_file:
# shutil.copyfileobj(response.raw, out_file)
# del response
img = Image.open('/home/{}/memesite/{}/galleries/default/{}.jpg'.format(myusr, usrfolz, gtm['id']))
imageSize = img.size
# find biggest font size that works
fontSize = int(imageSize[1]/5)
font = ImageFont.truetype("/home/{}/Downloads/impact.ttf".format(myusr), fontSize)
topTextSize = font.getsize(upzero)
bottomTextSize = font.getsize(botzero)
#dicinf = dict({str(thtim.timestamp) + usrfolz : dict({'musr' : myusr, 'user' : usrfolz, 'memeid' : gtm['id'],
# 'memename' : gtm['name'],
# 'uptext' : upzero, 'bottext' : botzero,
# 'img' : '/{}/galleries/{}.jpg'.format(usrfolz, gtm['id'])})})
#dicinf.update({str(thtim.timestamp) + usrfolz : dict({'musr' : myusr, 'user' : usrfolz, 'memeid' : gtm['id'],
# 'memename' : gtm['name'],
# 'imgtemp' : '/{}/galleries/default/{}.jpg'.format(usrfolz, gtm['id']),
# 'uptext' : upzero, 'bottext' : botzero,
# 'img' : '/{}/galleries/{}.jpg'.format(usrfolz, gtm['id'])})
#dicinf.update({str(thtim.timestamp + usrfolz : dict({'musr' : myusr, 'user' : usrfolz, 'memeid' : gtm['id'], 'memename' : gtm['name'], 'uptext' : upzero, 'bottext' : botzero, 'img' : '/{}/galleries/{}.jpg'.format(usrfolz, gtm['id'])}))
#print(dicinf)
#with open('/home/{}/memedata.pickle'.format(myusr), 'wb') as hand:
# pickle.dump(dicinf, hand, protocol=pickle.HIGHEST_PROTOCOL)
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20:
fontSize = fontSize - 1
font = ImageFont.truetype("/home/{}/Downloads/impact.ttf".format(myusr), fontSize)
topTextSize = font.getsize(upzero)
bottomTextSize = font.getsize(botzero)
# find top centered position for top text
topTextPositionX = (imageSize[0]/2) - (topTextSize[0]/2)
topTextPositionY = 0
topTextPosition = (topTextPositionX, topTextPositionY)
# find bottom centered position for bottom text
bottomTextPositionX = (imageSize[0]/2) - (bottomTextSize[0]/2)
bottomTextPositionY = imageSize[1] - bottomTextSize[1] -10
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
draw = ImageDraw.Draw(img)
outlineRange = int(fontSize/15)
for x in range(-outlineRange, outlineRange+1):
for y in range(-outlineRange, outlineRange+1):
draw.text((topTextPosition[0]+x, topTextPosition[1]+y), upzero, (0,0,0), font=font)
draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), botzero, (0,0,0), font=font)
draw.text(topTextPosition, upzero, (255,255,255), font=font)
draw.text(bottomTextPosition, botzero, (255,255,255), font=font)
img.save('/home/{}/memesite/{}/galleries/{}.jpg'.format(myusr, usrfolz, gtm['id']))
print(gtm['id'])
filemh = gtm['id']
with open('/home/{}/memesite/{}/posts/{}.md'.format(myusr, usrfolz, gtm['id']), 'w') as resulmd:
resulmd.write('<h2>{}</h2>\n\n![{}](/galleries/default/{})\n\n<h2>{}</h2>\n\n'.format(upzero, str(gtm['id']), str(gtm['id']) + '.jpg', botzero))
with open ('/home/{}/memesite/{}/posts/{}.meta'.format(myusr, usrfolz, gtm['id']), 'w') as opmetat:
opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(gtm['id'], gtm['id'], timnow.for_json()))
#graph.put_photo(image=open("/home/{}/memetest/galleries/{}.jpg".format(myusr, gtm['id']), 'rb'),
# album_path="me/photos")
#print(gtm['url'])
# In[119]:
#os.listdir("/home/{}/memetest/galleries/".format(myusr))
# In[128]:
specmo = specmem.replace(' ', '')
# In[147]:
#api.update_with_media("/home/{}/memetest/galleries/{}/{}.jpg".format(myusr, usrfolz, filemh), status= upzero + ', ' + botzero + ' #' + specmo + ' #dankmeme #meme')
#with open('/home/{}/memesite/{}/posts/{}.md'.format(myusr, usrfolz, gtm['id']), 'w') as resulmd:
# resulmd.write('<h2>{}</h2>\n\n![{}](/galleries/default/{})\n\n<h2>{}</h2>\n\n'.format(upzero, str(gtm['id']), str(gtm['id']) + '.jpg', botzero))
#with open ('/home/{}/memesite/{}/posts/{}.meta'.format(myusr, usrfolz, gtm['id']), 'w') as opmetat:
# 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[47]:
#with open('/home/{}/memetest/posts/{}.md'.format(myusr, str(gtm['id'])), 'w') as resulmd:
# resulmd.write('<h2>{}</h2>\n\n![{}](/galleries/default/{})\n\n<h2>{}</h2>\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[48]:
os.chdir('/home/{}/memesite/{}'.format(myusr, usrfolz))
# In[49]:
subprocess.call('nikola build', shell=True)
# In[50]:
#subprocess.call('ssh-add /home/pi/.ssh/chain', shell=True)
# In[106]:
#subprocess.call('rsync -rv /home/pi/memetest/ wcmckee@rbnz.tech:/home/wcmckee/meme', shell=True)
# In[43]:
subprocess.call('rsync -rv (/home/{}/memesite/{}/output/ /home/{}/artctrl/{}'.format(myusr, usrfolz,myusr, usrfolz), shell=True)
# In[87]:
#gtm['id']
# In[241]:
#para = {'url' : "http://rbnz.tech/meme/galleries/{}.jpg".format(myusr, gtm['id']), 'caption' : upzero +', ' + botzero, 'access_token' :'EAACEdEose0cBAFUPIWMm3ti6CFZBYwnsU7pOY3L0aRlmFpxqC9VUpzLbN6hpD3Od3pbqdYFMt6S0ykEDoAQ4hhNK8Vs72ZAQudNncuhgU9IWNYkGFh7MKlH65BhRR1KvWG65zROJKo4mV1AlBVwUhR6aEy6PLeZBirqe76YZA1qGkVfk8olIWcwSv6O7mL667sikFyE8xgZDZD'}
#para = {'url' : "http://rbnz.tech/meme/galleries/{}.jpg".format(str(gtm['id'])), 'caption' : upzero +', ' + botzero, 'access_token' :'EAACEdEose0cBAFUPIWMm3ti6CFZBYwnsU7pOY3L0aRlmFpxqC9VUpzLbN6hpD3Od3pbqdYFMt6S0ykEDoAQ4hhNK8Vs72ZAQudNncuhgU9IWNYkGFh7MKlH65BhRR1KvWG65zROJKo4mV1AlBVwUhR6aEy6PLeZBirqe76YZA1qGkVfk8olIWcwSv6O7mL667sikFyE8xgZDZD'}
#para
#repu = requests.post('https://graph.facebook.com/v2.10/me/photos', data=para)
#print(repu.content)
# In[182]:
#import os.path, time
#files = ['/home/pi/git/memegen.ini']
#changes = {"/home/pi/git/memegen.ini":os.path.getmtime("/home/pi/git/memegen.ini")}
#while True:
# for f in files:
# if changes.get(f) < os.path.getmtime(f):
# print ("File {} has been modified".format(f))
# changes[f] = os.path.getmtime(f)
# subprocess.call('python3 /home/{}/gen.py'.format(myusr), shell=True)
# else:
# print ("No changes, going to sleep.")
# time.sleep(10)
# In[148]:
#graph = facebook.GraphAPI(access_token='EAAchIRKSmikBAAeEIPsJXZA7lGMuNQoqUTwU5wvv47z7emNZBOrXW8qzJ4AwyAuoJtZCFNblSPB0thH3iZCAjmeKx6lm8eF6J9caLy1f8jddBDGff00M3IfQ5W5HrnFA6SfMSSThCdGxR6ZAMHIZCeHo6XnNIHtcVTvuHsshADeBVzVupYZAVe7', version="2.1")
# In[149]:
#para = {'url' : 'http://rbnz.tech/meme/galleries/{}/{}.jpg'.format(usrfolz, filemh), 'caption' : upzero + ', ' + botzero, 'access_token' :'EAAchIRKSmikBAAeEIPsJXZA7lGMuNQoqUTwU5wvv47z7emNZBOrXW8qzJ4AwyAuoJtZCFNblSPB0thH3iZCAjmeKx6lm8eF6J9caLy1f8jddBDGff00M3IfQ5W5HrnFA6SfMSSThCdGxR6ZAMHIZCeHo6XnNIHtcVTvuHsshADeBVzVupYZAVe7'}
#para
#repu = requests.post('https://graph.facebook.com/v2.10/me/photos', data=para)
#print(repu.content)
# In[ ]:
#graph.put_photo(image=open())
# In[95]:
#help(facebook.GraphAPI.put_photo)
# In[ ]:
In [ ]: