starttechpost

script to make a new blog post. tweet that i have started it. downloads gifs on input keyword and tweet the with start message.

Check dir and if it has the name already - error

.. title: eceret .. slug: eceret .. date: 2016-12-02 11:30:18 UTC .. tags: .. link: .. description: .. type: text

In [3]:
import getpass
import os
import arrow
#from TwitterFollowBot import TwitterBot
import requests
import random
import tweepy
import shutil
#import IPython.nbformat.current as nbf
#import nbformat

from shutil import copyfile
import json

#import pickle
In [4]:
namofgifsea = input('keyword gif search: ')
keyword gif search: rehab
In [5]:
myusr = getpass.getuser()
In [6]:
gifpat = ('/home/{}/giftweet/'.format(myusr))
In [7]:
oslisg = os.listdir(gifpat)
In [8]:
dictgif = dict()
In [9]:
if namofgifsea not in oslisg:
    os.mkdir('{}{}'.format(gifpat, namofgifsea))
    typereq = 'http://api.giphy.com/v1/gifs/search?q={}&api_key=dc6zaTOxFJmzC'.format(namofgifsea)
    reqt = requests.get(typereq)
    gifjsn = reqt.json()
    leng = len(gifjsn['data'])
    
    for legif in range(0, leng):
        gurl = ((gifjsn['data'][legif]['images']['original']['url']))
        gslug = (gifjsn['data'][legif]['slug'])
        dictgif.update({gslug : gurl})
        #print(gurl)
        response = requests.get(gurl, stream=True)#
        response
        with open('{}{}/{}.gif'.format(gifpat, namofgifsea, gslug), 'wb') as out_file:
            shutil.copyfileobj(response.raw, out_file)
            del response
else:
    pass
In [10]:
#import requests
#data = requests.get("http://api.giphy.com/v1/gifs/search?q=ryan+gosling&api_key=dc6zaTOxFJmzC&limit=5")
#print (data.json())
In [11]:
dicfir = dict()
In [12]:
dicfir.update({namofgifsea : dictgif})
In [13]:
indump = (json.dumps(dicfir))
In [15]:
with open('/home/{}/giftweet/{}.json'.format(myusr, namofgifsea), 'w') as gifd:
    gifd.write(indump)
In [16]:
#with open('/home/{}/startgif.pickle'.format(myusr), 'ab') as gifd:
#    pickle.dump(dicfir, gifd, pickle.HIGHEST_PROTOCOL)
In [17]:
#with open('/home/{}/startgif.pickle'.format(myusr), 'rb') as gf:
#    print(pickle.load(gf))
In [18]:
#with open('/home/{}/startgif.pickle'.format(myusr), 'r') as gifd:
#    pickle.load(dicfir, gifd, pickle.HIGHEST_PROTOCOL)

An arbitrary collection of objects supported by pickle. data = { 'a': [1, 2.0, 3, 4+6j], 'b': ("character string", b"byte string"), 'c': set([None, True, False]) }

with open('data.pickle', 'wb') as f:

# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
In [19]:
dictgif.update({namofgifsea : dict({})})
In [20]:
ranlocgif = random.choice(os.listdir(gifpat + namofgifsea))
In [21]:
rangif = random.randint(0, leng)
In [22]:
rangifurl = ((gifjsn['data'][rangif]['images']['original']['url']))
In [23]:
blagpat = ('/home/{}/git/wcmsilver/wcmckee.com/posts/'.format(myusr))
In [24]:
with open('/home/{}/wck.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 [25]:
auth = tweepy.OAuthHandler(CONSUMER_KEY.strip('\n'), CONSUMER_SECRET.strip('\n'))
auth.set_access_token(OAUTH_TOKEN.strip('\n'), OAUTH_SECRET.strip('\n'))
In [26]:
api = tweepy.API(auth)
In [27]:
nameofblogpost = input('name of blog post: ')
name of blog post: rehabtest
In [28]:
postext = '.ipynb'
In [29]:
blognam = nameofblogpost + postext
In [30]:
osdi = os.listdir(blagpat)
In [31]:
#[x for x in osdi if blognam in x]
In [32]:
timnow = arrow.utcnow()
In [33]:
nowtime = (timnow.datetime)
In [34]:
taglis = list()
In [37]:
if any(blognam in s for s in osdi):
    print('post is there - choose a diff name')
    raise SystemExit
else:
    print('it is not there')
    
    copyfile('/home/{}/test.ipynb'.format(myusr), '/home/{}/git/wcmckee.com/posts/{}'.format(myusr, blognam))
    #with open(blagpat + blognam, "w+", encoding="utf8") as fd:
    #    nbformat.write(blagpat + blognam, fd, 'ipynb')
    #nbformat.writes(blagpat + blognam, 'ipynb')
    #os.mknod(blagpat + blognam)
    tagblog = input('blog tags: ')
    with open(blagpat + nameofblogpost + '.meta', 'w') as blmet:
        blmet.write('.. title: ' + nameofblogpost + ' \n' + '.. slug: ' + nameofblogpost + ' \n' + '.. date: ' + str(nowtime) + ' \n' +  '.. tags: ' + tagblog + '\n' + '.. link:\n.. description:\n.. type: text')
        
    tagspl = tagblog.split(',')
    
    for ta in tagspl:
        print( '#' + ta.replace(' ', ''))
        taglis.append( '#' + ta.replace(' ', ''))
        
    jointag = (' '.join(taglis))
    api.update_with_media('{}{}/{}'.format(gifpat, namofgifsea, ranlocgif), status='Started typing script {} {}'.format(blognam, jointag))
        
    #wcm_bot.send_tweet('Started typing script {} {} {}'.format(blognam, rangifurl, jointag))
it is not there
blog tags: test, bot
#test
#bot
In [38]:
os.remove(gifpat + namofgifsea +  '/' + ranlocgif)
In [ ]:
 

Comments

Comments powered by Disqus