nbagame

In [ ]:
 
In [631]:
import requests
import arrow
import datetime
import nltk
import getpass
import json

import PIL
from PIL import ImageDraw, ImageFont
In [632]:
myusr = getpass.getuser()
In [633]:
myusr
Out[633]:
'pi'
In [634]:
#nbagame = requests.get('http://api.suredbits.com/nba/v0/games')
In [635]:
arrow.now()
Out[635]:
<Arrow [2018-04-17T01:20:13.111655+12:00]>
In [636]:
previouday = arrow.utcnow().replace(days=-1)
In [637]:
pred = previouday.date()
In [638]:
print(pred.day)
15
In [639]:
print(pred.month)
4
In [640]:
print(pred.year)
2018
In [641]:
nbagame = requests.get('http://api.suredbits.com/nba/v0/games/{}/{}/{}'.format(pred.year, pred.month, pred.day))
In [ ]:
 
In [642]:
import json
In [643]:
reqteams = requests.get('https://erikberg.com/nba/teams.json')
In [644]:
tjdump = reqteams.json()
In [ ]:
 
In [645]:
tjdump
Out[645]:
{'error': {'code': '403',
  'description': 'Invalid user agent: python-requests/2.18.4. Please refer to the user agent guidelines specified at https://erikberg.com/api#ua.'}}
In [ ]:
 
In [646]:
with open('/home/pi/dell-wcm/posts/teams.json', 'r') as teamjs:
    tjdump = json.loads(teamjs.read())
In [647]:
abrev = 'BKN'
In [ ]:
 
In [648]:
for tjd in tjdump:
    #print(tjd)
    if tjd['abbreviation'] == abrev:
        print(tjd)
        print(tjd['teamName'])
{'location': 'Brooklyn', 'teamName': 'Brooklyn Nets', 'abbreviation': 'BKN', 'simpleName': 'Nets', 'teamId': 1610612751}
Brooklyn Nets
In [ ]:
 
In [ ]:
 
In [649]:
nbgj =  nbagame.json()
In [650]:
totgame = len(nbgj)
In [651]:
#arrow.get('20170331')
In [652]:
#datim = datetime.datetime('20170331T213000.000Z')
In [653]:
#arrow.get('2017')
In [654]:
timnow = arrow.now()
In [655]:
timnow
Out[655]:
<Arrow [2018-04-17T01:20:19.657238+12:00]>
In [656]:
nbgj
Out[656]:
[]
In [657]:
import glob
import os
filelist=glob.glob("/home/pi/was/static/*.png")
for file in filelist:
    os.remove(file)
In [658]:
homewine = 0
homelise = 0

awaywine = 0
awaylise = 0
In [659]:
homewine
Out[659]:
0
In [660]:
homelise
Out[660]:
0
In [661]:
awaywine
Out[661]:
0
In [662]:
awaylise
Out[662]:
0
In [663]:
for nbg in nbgj:
    print(nbg)
    #print(nbg['startTime'])
    print(nbg['homeTeam']['teamID'])
    print(nbg['homeTeam']['finalScore'])
    
    for tjd in tjdump:
    #print(tjd)
        if tjd['abbreviation'] == nbg['homeTeam']['teamID']:
            print(tjd)
            homename = (tjd['teamName'])
        
        elif tjd['abbreviation'] == nbg['awayTeam']['teamID']:
            print(tjd)
            awayname = (tjd['teamName'])
    
    if nbg['homeTeam']['finalScore'] > nbg['awayTeam']['finalScore']:
        homeverdic = 'winner'
        homewine = homewine + 1
        awayverdic = 'loser'
        awaylise = awaylise +1 
        
        
    elif nbg['homeTeam']['finalScore'] < nbg['awayTeam']['finalScore']:
            homeverdic = 'loser'
            homelise = homelise + 1
            awayverdic = 'winner'
            awaywine = awaywine +1
        #memename = request.json.get('memename')

    toptext = homename + ' ' + str(nbg['homeTeam']['finalScore']) + ' ' + homeverdic

    bottomtext = awayname + ' ' + str(nbg['awayTeam']['finalScore']) + ' ' + awayverdic

    #user= request.json.get('user')
    
    

    timnow = arrow.now()
    timstr = timnow.timestamp

    galdirdir = '/home/{}/was/static'.format(myusr)



        #with open('/home/{}/rbnz-tech-backup/artctrl/meme/galleries/default')

    #img = Image.open('/home/{}/artctrl/meme/galleries/default/{}.jpg'.format(myusr, memename))
    img = PIL.Image.new("RGBA", (800, 100), (255,255,255))
    imageSize = img.size

        # find biggest font size th90t works
    fontSize = int(imageSize[1]/5)
    font = ImageFont.truetype("/home/{}/Downloads/impact.ttf".format(myusr), fontSize)
    topTextSize = font.getsize(toptext)
    bottomTextSize = font.getsize(bottomtext)

    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(toptext)
        bottomTextSize = font.getsize(bottomtext)

        # 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), toptext, (0,0,0), font=font)
                draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottomtext, (0,0,0), font=font)

    draw.text(topTextPosition, toptext, (255,255,255), font=font)
    draw.text(bottomTextPosition, bottomtext, (255,255,255), font=font)
    img.save('/home/{}/was/static/{}{}.png'.format(myusr, nbg['homeTeam']['teamID'], nbg['awayTeam']['teamID']))

            #img.save("/home/{}/memetest/galleries/{}/{}.jpg".format(myusr, usrfolz, gtm['id']))
    


    #memedict = dict({'nba' : memename, 'toptext' : toptext.upper(), 'bottomtext' : bottomtext.upper(), 'imagepath' : '/home/{}/rbnz-tech-backup/artctrl/meme/galleries/{}/{}-{}.jpg'.format(myusr, user, memename, timstr)})
    #return(jsonify(memedict))

    print(nbg['awayTeam']['teamID'])
    print(nbg['awayTeam']['finalScore'])
In [664]:
awa
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-664-26d1c169b3d0> in <module>()
----> 1 awa

NameError: name 'awa' is not defined
In [665]:
#memename = request.json.get('memename')

toptext = 'Milwaukee Bucks' + ' ' + str(69) + ' ' + 'Loser'

bottomtext = 'Washington Wizards' + ' ' + str(99) + ' ' + 'Winner'
#toptext = 'MEM'

#bottomtext = request.json.get('bottomtext')
#bottomtext = 'DET'
#user= request.json.get('user')

timnow = arrow.now()
timstr = timnow.timestamp

galdirdir = '/home/{}/was/static/'.format(myusr)



    #with open('/home/{}/rbnz-tech-backup/artctrl/meme/galleries/default')

#img = Image.open('/home/{}/artctrl/meme/galleries/default/{}.jpg'.format(myusr, memename))
img = PIL.Image.new("RGBA", (800, 200), (255,255,255))
imageSize = img.size

    # find biggest font size th90t works
fontSize = int(imageSize[1]/5)
font = ImageFont.truetype("/home/{}/Downloads/impact.ttf".format(myusr), fontSize)
topTextSize = font.getsize(toptext)
bottomTextSize = font.getsize(bottomtext)

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(toptext)
    bottomTextSize = font.getsize(bottomtext)

    # 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), toptext, (0,0,0), font=font)
            draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottomtext, (0,0,0), font=font)

draw.text(topTextPosition, toptext, (255,255,255), font=font)
draw.text(bottomTextPosition, bottomtext, (255,255,255), font=font)
img.save('/home/{}/was/static/WASBUK.png'.format(myusr))

        #img.save("/home/{}/memetest/galleries/{}/{}.jpg".format(myusr, usrfolz, gtm['id']))


#memedict = dict({'nba' : memename, 'toptext' : toptext.upper(), 'bottomtext' : bottomtext.upper(), 'imagepath' : '/home/{}/rbnz-tech-backup/artctrl/meme/galleries/{}/{}-{}.jpg'.format(myusr, user, memename, timstr)})
#return(jsonify(memedict))
In [666]:
import os
In [667]:
os.listdir('/home/pi/was/static')
Out[667]:
['WAS-14.jpg',
 'WAS-3.jpg',
 'WAS-19.jpg',
 'WAS-5.jpg',
 'WAS-12.jpg',
 'WAS-22.jpg',
 'WAS-6.jpg',
 'WAS-13.jpg',
 'WAS-9.jpg',
 'WAS-7.jpg',
 'WAS-18.jpg',
 'WAS-24.jpg',
 'WAS-23.jpg',
 'WAS-20.jpg',
 'WASBUK.png',
 'WAS-11.jpg',
 'WAS-16.jpg',
 'WAS-17.jpg',
 'WAS-1.jpg',
 'WAS-0.jpg',
 'WAS-2.jpg',
 'WAS-8.jpg',
 'WAS-21.jpg',
 'WAS-4.jpg',
 'WAS-10.jpg',
 'WAS-15.jpg']
In [668]:
gifcity = 'http://api.giphy.com/v1/gifs/search?q=Washington Wizards&api_key=dc6zaTOxFJmzC'
In [669]:
reqcit = requests.get(gifcity)
In [670]:
nbajs = (reqcit.json())
In [671]:
lendat = len(nbajs['data'])
In [672]:
print(lendat)
25
In [673]:
import shutil
In [674]:
thlis= list()
In [675]:
thenumber = 0
In [676]:
somelis = list()
In [ ]:
 
In [677]:
for lend in range(0, 25):
    #print(nbajs['data'][lend]['images']['480w_still']['url'])
    response = requests.get(nbajs['data'][lend]['images']['480w_still']['url'], stream=True)
    with open('/home/pi/was/static/WAS-{}.jpg'.format(lend), 'wb') as out_file:
        shutil.copyfileobj(response.raw, out_file)
        del response

    imgp = PIL.Image.open("WAS-{}.jpg".format(lend))
    fulen = imgp.size
    thlis.append(imgp.size[1])
    thenumber = thenumber + fulen[0]
    print(thenumber)
    somelis.append("WAS-{}.jpg".format(lend))
    

    #somimg = PIL.Image(nbajs['data'][lend]['images']['480w_still']['url'])
    
    #print(soming.size)
    
480
960
1440
1920
2400
2880
3360
3840
4320
4800
5280
5760
6240
6720
7200
7680
8160
8640
9120
9600
10080
10560
11040
11520
12000
In [678]:
somelis
Out[678]:
['WAS-0.jpg',
 'WAS-1.jpg',
 'WAS-2.jpg',
 'WAS-3.jpg',
 'WAS-4.jpg',
 'WAS-5.jpg',
 'WAS-6.jpg',
 'WAS-7.jpg',
 'WAS-8.jpg',
 'WAS-9.jpg',
 'WAS-10.jpg',
 'WAS-11.jpg',
 'WAS-12.jpg',
 'WAS-13.jpg',
 'WAS-14.jpg',
 'WAS-15.jpg',
 'WAS-16.jpg',
 'WAS-17.jpg',
 'WAS-18.jpg',
 'WAS-19.jpg',
 'WAS-20.jpg',
 'WAS-21.jpg',
 'WAS-22.jpg',
 'WAS-23.jpg',
 'WAS-24.jpg']
In [679]:
thlis
Out[679]:
[270,
 270,
 268,
 270,
 270,
 270,
 270,
 270,
 476,
 344,
 270,
 270,
 282,
 268,
 270,
 270,
 270,
 270,
 266,
 270,
 270,
 270,
 270,
 270,
 270]
In [680]:
bigimg = PIL.Image.new("RGBA", (20640, 476), (255,255,255))
bigimg.save('nba.png')
In [681]:
somelis
Out[681]:
['WAS-0.jpg',
 'WAS-1.jpg',
 'WAS-2.jpg',
 'WAS-3.jpg',
 'WAS-4.jpg',
 'WAS-5.jpg',
 'WAS-6.jpg',
 'WAS-7.jpg',
 'WAS-8.jpg',
 'WAS-9.jpg',
 'WAS-10.jpg',
 'WAS-11.jpg',
 'WAS-12.jpg',
 'WAS-13.jpg',
 'WAS-14.jpg',
 'WAS-15.jpg',
 'WAS-16.jpg',
 'WAS-17.jpg',
 'WAS-18.jpg',
 'WAS-19.jpg',
 'WAS-20.jpg',
 'WAS-21.jpg',
 'WAS-22.jpg',
 'WAS-23.jpg',
 'WAS-24.jpg']
In [682]:
newlis = list()
In [ ]:
 
In [683]:
for som in somelis:
    print('/home/pi/was/static/' + som)
    newlis.append('/home/pi/was/static/' + som)
/home/pi/was/static/WAS-0.jpg
/home/pi/was/static/WAS-1.jpg
/home/pi/was/static/WAS-2.jpg
/home/pi/was/static/WAS-3.jpg
/home/pi/was/static/WAS-4.jpg
/home/pi/was/static/WAS-5.jpg
/home/pi/was/static/WAS-6.jpg
/home/pi/was/static/WAS-7.jpg
/home/pi/was/static/WAS-8.jpg
/home/pi/was/static/WAS-9.jpg
/home/pi/was/static/WAS-10.jpg
/home/pi/was/static/WAS-11.jpg
/home/pi/was/static/WAS-12.jpg
/home/pi/was/static/WAS-13.jpg
/home/pi/was/static/WAS-14.jpg
/home/pi/was/static/WAS-15.jpg
/home/pi/was/static/WAS-16.jpg
/home/pi/was/static/WAS-17.jpg
/home/pi/was/static/WAS-18.jpg
/home/pi/was/static/WAS-19.jpg
/home/pi/was/static/WAS-20.jpg
/home/pi/was/static/WAS-21.jpg
/home/pi/was/static/WAS-22.jpg
/home/pi/was/static/WAS-23.jpg
/home/pi/was/static/WAS-24.jpg
In [ ]:
 
In [ ]:
 
In [684]:
'/home/pi/was/static/'
Out[684]:
'/home/pi/was/static/'
In [685]:
Image('/home/pi/was/static/WAS-0.jpg')
Out[685]:
In [686]:
Image('/home/pi/was/static/WAS-24.jpg')
Out[686]:
In [687]:
import numpy as np
import PIL

list_im =  newlis
imgs    = [ PIL.Image.open(i) for i in list_im ]
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here)
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )

# save that beautiful picture
imgs_comb = PIL.Image.fromarray( imgs_comb)
imgs_comb.save( '/home/pi/was/test.jpg' )    

# for a vertical stacking it is simple: use vstack
imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
imgs_comb = PIL.Image.fromarray( imgs_comb)
imgs_comb.save( '/home/pi/was/test-vert.jpg' )
In [688]:
Image('/home/pi/was/test-vert.jpg')
Out[688]:
In [ ]:
 
In [618]:
images = map(PIL.Image.open, ['/home/pi/was/static/WAS-0.jpg', '/home/pi/was/static/WAS-24.jpg'])
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = PIL.Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
    new_im.paste(im, (x_offset,0))

    x_offset += im.size[0]
    #Image('/home/pi/test.jpg')

new_im.save('/home/pi/was/test-two.jpg')
In [619]:
Image('/home/pi/was/test-two.jpg')
Out[619]:
In [ ]:
 
In [603]:
images = map(PIL.Image.open, newlis)
widths, heights = zip(*(i.size for i in images))

total_width = sum(widths)
max_height = max(heights)

new_im = PIL.Image.new('RGB', (total_width, max_height))

x_offset = 0
for im in images:
    new_im.paste(im, (x_offset,0))

    x_offset += im.size[0]
    #Image('/home/pi/test.jpg')

new_im.save('/home/pi/was/test.jpg')
In [ ]:
 
In [606]:
Image('/home/pi/was/test.jpg')
Out[606]:
In [487]:
heights
Out[487]:
(270,
 270,
 268,
 270,
 270,
 270,
 270,
 270,
 476,
 344,
 270,
 270,
 282,
 268,
 270,
 270,
 270,
 270,
 266,
 270,
 270,
 270,
 270,
 270,
 270,
 270,
 270,
 268,
 270,
 270,
 270,
 270,
 270,
 476,
 344,
 270,
 270,
 282,
 268,
 270,
 270,
 270,
 270,
 266,
 270,
 270,
 270,
 270,
 270,
 270)
In [488]:
images
Out[488]:
<map at 0x6e727310>
In [489]:
 postimg = os.listdir('/home/pi/was/static')
In [490]:
print(postimg)
['MIATOR.png', 'LACLAL.png', 'CLENYK.png', 'NOPSAS.png', 'BOSBKN.png', 'MINDEN.png', 'CHIDET.png', 'SACHOU.png', 'OKCMEM.png', 'PHIMIL.png', 'ORLWAS.png', 'PORUTA.png']
In [ ]:
 
In [491]:
Image('/home/pi/test.jpg')
Out[491]: