<jupyter_start><jupyter_text>zalando brands python script to interact with zalando brands and articles. Improve the brands api with more data from the Zalando brand page and other sources - giphy, duckduckgo, analyze the description text for each brand. Create images stories with keywords.fix the text that is failed to be parsed.create flask json api with new and updated brand. title, slug, tags, logo, images.<jupyter_code>import getpass
import requests
import shutil

import arrow
import bs4

import nltk
from nltk.tag import pos_tag
import random
curtim = arrow.now()
myusr = getpass.getuser()
zalbr = ('/home/{}/zalando/'.format(myusr))
zalgal = zalbr + 'galleries/'
branreq = requests.get('https://api.zalando.com/brands')
branjs = branreq.json()
totele = branjs['totalElements']
brnconl = len(branjs['content'])
branjs
brnconl
totpag = branjs['totalPages']
zalgal
lilwhi = requests.get('https://www.zalando.co.uk/little-white-lies/')

lilw = lilwhi.text

bsliw = bs4.BeautifulSoup(lilw)

bsliw

desfil =  (bsliw.find('div', id=('description')))

desp = desfil.p

desp.text
#deshe = desfil.h2
desp = desfil.p
toktx = nltk.word_tokenize(desp.text)
nlmytx = nltk.Text(toktx)
nlmytx.concordance('Your')
toktx
for blogb in blogbrdic:
    print(blogb)
len(blogbrdic)
for ble in range(0, len(blogbrdic)):
    print(blogbrdic[ble])
desp.text
blogbrdic = dict()
bloglisn = list()
reqph = requests.get('https://api.zalando.com/brands?pageSize={}'.format(totpag))
alljs = (reqph.json())
brnconz = len(alljs['content'])
for sel in range(0, brnconz):
    #print(alljs['content'][sel]['name'])
    namsl = (alljs['content'][sel]['name'])
    namcd = (alljs['content'][sel]['key'])
    print(namcd)
    namsq = namsl.replace(' ', '-')

    mamsq = namsq.lower

    print(mamsq())
    blogbrdic.update({mamsq() : 'test'})
                      
    try:
        logurl = (alljs['content'][sel]['logoUrl'])
        
        shurl = (alljs['content'][sel]['shopUrl'])
        
        print(logurl)
        print(shurl)
        
        lilwhi = requests.get(shurl)

        lilw = lilwhi.text

        bsliw = bs4.BeautifulSoup(lilw)

        #bsliw

        desfil =  (bsliw.find('div', id=('description')))

        try:
            desp = desfil.p
            
            depa = desfil.strong
        except AttributeError:
            pass
        
        print(depa.text)
        print(desp.text)
        blogbrdic.update({mamsq() : dict({'description' : desp.text, 'header': depa.text})})
        bloglisn.append(desp.text)
        
        #response = requests.get(logurl, stream=True)
        #print(response.headers)
        #print(response.history)
        #splilog = urlsplit(logurl)
        #print(splilog[2])
        with open ('/home/{}/zalando/posts/{}.md'.format(myusr, namcd), 'w') as oppost:
            #oppost.write('![{}'.format())
            oppost.write(('![' + mamsq() + '](' + logurl + ')\n\n' + mamsq() + '\n\n' + depa.text + '\n\n' + desp.text))
        
        with open ('/home/{}/zalando/posts/{}.meta'.format(myusr, namcd), 'w') as opmetat:
            #opmetat.write("{}".format(str(curtim))
            opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(namcd, namcd, curtim))

        #with open('{}{}.png'.format(zalgal, str(totp)), 'wb') as out_file:
                
        #    shutil.copyfileobj(response.raw, out_file)
        #    del response
    except KeyError:
        continue
g = Generator(bloglisn)
myblog = ' '.join(bloglisn)
nlblog = (myblog.split)
fixcom = myblog.replace(',', '')
foxcom = fixcom.replace('.', '')
foxcom
bltxe = nltk.Text(myblog)
post = pos_tag(foxcom.split())
post
jjword = [word for word,pos in post if pos == 'JJ']
sejj = set(jjword)
for sej in sejj:
    if ',' in sej:
        #print(', is here')
        sej.replace(',', '')
    elif '.' in sej:
        #print('. is here')
        sej.replace(',', '')
   
propernouns = [word for word,pos in post if pos == 'NN']
for sej in propernouns:
    if ',' in sej:
        #print(', is here')
        sej.replace(',', '')
    elif '.' in sej:
        print('. is here')
        #sej.replace('.', '')
        #print(sej)
        s = sej.strip('.')
        print(s)
ppset = set(propernouns)
doword = [word for word,pos in post if pos == 'VB']
for prpno in propernouns:
    prprep = (prpno.replace(',', ''))
    prprez = (prprep.replace('.', ''))
random.sample(propernouns, 5)
random.sample(doword, 5)
'''
for totp in range(1, totpag):
    reqph = requests.get('https://api.zalando.com/brands?page={}'.format(totp))
    alljs = (reqph.json())
    brnconz = len(alljs['content'])
    for sel in range(0, brnconz):
        #print(alljs['content'][sel]['name'])
        namsl = (alljs['content'][sel]['name'])
        namcd = (alljs['content'][sel]['key'])
        namsq = namsl.replace(' ', '-')
        
        mamsq = namsq.lower
        print(namcd)
        print(mamsq())
        try:
            logurl = (alljs['content'][sel]['logoUrl'])
            print(logurl)
            response = requests.get(logurl, stream=True)
            print(response.headers)
            print(response.history)
            #print(response.)
            with open ('/home/{}/zalando/posts/'.format(myusr) + fulyr + '.md', 'w') as oppost:
                for toda in imgtoday:
                    oppost.write(('!' + '[' + toda.strip('.jpg') + '](' + galpath + toda + ')\n\n' + '[' + toda.strip('-reference.jpg') + ']' + '(https://reddit.com/u/' + toda.strip('-reference.jpg') + ')' + '\n\n'))

            with open('{}{}.png'.format(zalgal, str(totp)), 'wb') as out_file:
                
                shutil.copyfileobj(response.raw, out_file)
                del response
        except KeyError:
            continue
            
'''
bloglisn
zalaart = requests.get('https://api.zalando.com/articles')
print('hello')
zaljs = zalaart.json()
len(zaljs['content'])
zaljs['content'][0]['categoryKeys']
zalpage = zaljs['totalPages']
zalpage
reqartz = requests.get('https://api.zalando.com/articles?pageSize=200')

artjsz = reqartz.json()
arjsct = artjsz['content']
arle = len(arjsct)
for arj in arjsct:
    artnam = (arj['name'])
    artimg = (arj['media']['images'][0]['largeUrl'])
    artpric = (arj['units'][0]['price']['value'])
    nacd = (arj['brand']['key'])
    branam = (arj['brand']['name'])
    
    namsq = branam.replace(' ', '-')

    mamsq = namsq.lower()
    
    print(mamsq)
    with open ('/home/{}/zalando/posts/{}.md'.format(myusr, nacd), 'a') as oppost:
    #oppost.write('![{}'.format())
        oppost.write('\n\n{}\n\n{}\n![{}]({})'.format(artnam, str(artpric), nacd, artimg))
    #oppost.write(('![' + mamsq() + '](' + logurl + ')\n\n' + mamsq() + '\n\n' + depa.text + '\n\n' + desp.text))

    with open ('/home/{}/zalando/posts/{}.meta'.format(myusr, nacd), 'w') as opmetat:
            #opmetat.write("{}".format(str(curtim))
        opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(nacd, nacd, curtim))

'''
zaart = requests.get('https://api.zalando.com/articles?pageSize=200')
zajs = zaart.json()
print(zal)
artnam = (zajs['content'][zal]['name'])
    
artimg = (zajs['content'][zal]['media']['images'][0]['largeUrl'])
artpric = (zajs['content'][zal]['units'][0]['price']['value'])

print(zajs['content'][zal]['brand'])
    
nacd = (zajs['content'][zal]['brand']['key'])
    
print(zajs['content'][zal]['brand']['name'])
    
    
with open ('/home/{}/zalando/posts/{}.md'.format(myusr, nacd), 'a') as oppost:
    #oppost.write('![{}'.format())
    oppost.write('\n\n{}\n\n{}\n![{}]({})'.format(artnam, str(artpric), nacd, artimg))
    #oppost.write(('![' + mamsq() + '](' + logurl + ')\n\n' + mamsq() + '\n\n' + depa.text + '\n\n' + desp.text))

with open ('/home/{}/zalando/posts/{}.meta'.format(myusr, nacd), 'w') as opmetat:
            #opmetat.write("{}".format(str(curtim))
            opmetat.write('.. title: {}\n.. slug: {}\n.. date: {}\n.. tags: \n.. link:\n.. description:\n.. type: text'.format(namcd, namcd, curtim))
'''
'''
for zal in range(0, zalpage):
    zaart = requests.get('https://api.zalando.com/articles')
    zajs = zaart.json()
    print(zal)
    artnam = (zajs['content'][zal]['name'])
    
    artimg = (zajs['content'][zal]['media']['images'][0]['largeUrl'])
    artpric = (zajs['content'][zal]['units'][0]['price']['value'])
    
    print(zajs['content'][zal]['brand'])
    
    nacd = (zajs['content'][zal]['brand']['key'])
    
    print(zajs['content'][zal]['brand']['name'])
    
    
    with open ('/home/{}/zalando/posts/{}.md'.format(myusr, nacd), 'a') as oppost:
            #oppost.write('![{}'.format())
            oppost.write('\n\n{}\n\n{}\n![{}]({})'.format(artnam, str(artpric), nacd, artimg))
            #oppost.write(('![' + mamsq() + '](' + logurl + ')\n\n' + mamsq() + '\n\n' + depa.text + '\n\n' + desp.text))
        
        
    #try:
    #    stufav = (zajs['content'][zal]['available'])
    #    if stufav == True:
    #        print('yeah in stock')
    #    elif stufav == False:
    #        print('not in stock')
    #except IndexError:
    #    continue
    
        
    
    #try:
    #    print(zajs['content'][zal]['available'])
    #except KeyError:
    #    continue
    
'''<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.210221793635487, 'fraction_numerical': 0.002678667095253402, 'mean_word_length': 2.7958519723464823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 9, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '341647', 'license': 'MIT', 'n_tokens_mistral': 3507, 'n_tokens_neox': 3481, 'n_words': 634, 'path': 'posts/zalandobrands.ipynb', 'repo_name': 'wcmckee/wcmckee.com', 'size': '9333'}