Beispiel #1
0
def ParseContent(country, assettype, source, starturl, asseturl, name):   
    if gL.trace: gL.log(gL.DEBUG)   

    try:
        Asset = gL.dbAsset(country, assettype, source, name, asseturl)  # inserisco l'asset
        if Asset == 0:
            raise Exception("Errore nella creazione dell'asset")
        fn = gL.GetFunzione("PARSE", source, assettype, country)
        if not fn:            
            raise Exception("Funzione PARSE non trovata")           
        rc = globals()[fn](country, asseturl, name, Asset)
        if rc:
            return Asset
        else:
            raise Exception("Funzione PARSE con errori")
    except Exception as err:
        gL.log(gL.ERROR, err)
        return False
Beispiel #2
0
def ParseGooglePlaces(Asset, assettype, name, street, zip, city, country, address, AAsset):
    if gL.trace: gL.log(gL.DEBUG)   
    if address != '':
        indirizzo = address
    else:
        indirizzo =  street + " " + zip + " " + city 

    try:
        qry = name + " " + city
        qry = qry.encode()
        API_KEY  = "AIzaSyDbLzwj-f_IJOEWYdgx12n0CizPN3xPUfM"
        searchurl = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
    
        params = dict(
            query = qry,
            key = API_KEY,    
            language='it',    
            #types='cafe|reastaurant|bakery|bar|food|meal_takeaway'
            )

        response = requests.get(url=searchurl, params=params)
        data = json.loads(response.text)
        if data['status'] == 'ZERO_RESULTS':
            gL.log(gL.WARNING, "GooglePlaces ZERO RESULTS:" + str(qry))
            return False
        if data['status'] != 'OK':
            gL.log(gL.WARNING, "GooglePlaces Status " + data['status'])
            return False

        # se ci sono piu' elementi ritornati scelgo quello che meglio matcha ---------------------
        chk = []   
        namepeso = 1.5
        streetpeso = 1     
        if len(data['results']) > 0:
            for idx, test in enumerate(data['results']):
                if 'formatted_address' in test:
                    adr = test['formatted_address']
                #nam =             
                nam = gL.StdName(test['name'])    
                nameratio = streetratio = 0
                nameratio = difflib.SequenceMatcher(None, a = name, b = nam).ratio()
                streetratio = difflib.SequenceMatcher(None, a = indirizzo, b = adr).ratio()    
                gblratio = ((nameratio * namepeso) + (streetratio * streetpeso)) / len(data['results'])          
                chk.append((gblratio, idx, nam, adr, nameratio, streetratio))      # nome, indirizzo, ratio del nome, ratio dell'indirizzo
            #chk.sort(reverse=True) 
            chk.sort(reverse=True, key=lambda tup: tup[0])  
            if gL.debug:            
                gL.DumpGoogleResults(Asset, name, indirizzo, chk)
            if chk[0][0] < 0.8:  #global ratio
                idx = 0
            else:
                idx = chk[0][1]
        else:
            idx = 0

        # --------------------------------------------------------------------- OK LEGGO 
        a = data['results'][idx]   # l'elemento selezionato tra quelli ritornati
        lat = 0; lng = 0; tag=[]; prz = None
        if 'geometry' in a:
            lat = a['geometry']['location']['lat']
            lng = a['geometry']['location']['lng']
        nam = a['name'].title()                  
        if 'place_id' in a:
            pid = a['place_id']
        if 'reference' in a:
            ref = a['reference']
        if 'price_level' in a:
            prz = a['price_level'] # 0 � Free 1 � Inexpensive 2 � Moderate 3 � Expensive 4 � Very Expensive
        if 'rating' in a:
            rat = a['rating']        
        if 'formatted_address' in a:
            adr = a['formatted_address']
        tag = []
        if 'types' in a:
            for type in a['types']:
                if type == 'cafe' or type == 'bar':
                    tag.append("Caffetteria")
                if type == 'restaurant' :
                    tag.append("Ristorante")
                if type == 'lodging' :
                    tag.append("Hotel")
                if type == 'bakery' :
                    tag.append("Panetteria")
        PriceList = []
        if prz is not None:
            if prz == 0:
                PriceFrom = 5
                PriceTo = 12
            if prz == 1:
                PriceFrom = 5
                PriceTo = 12
            if prz == 2:
                PriceFrom = 13
                PriceTo = 25
            if prz == 3:
                PriceFrom = 26
                PriceTo = 60
            if prz == 4:
                PriceFrom = 60
                PriceTo = 100
            PriceList = [['PriceCurr', gL.currency],
                        ['PriceFrom', PriceFrom],
                        ['PriceTo', PriceTo]]

        # chiedo il dettaglio
        detailurl = 'https://maps.googleapis.com/maps/api/place/details/json'
        params = dict(
            placeid = pid,
            key = API_KEY,    
            language='it',         
            #types='cafe|reastaurant|bakery|bar|food|meal_takeaway'
            )

        response = requests.get(url=detailurl, params=params)
        data2 = json.loads(response.text)
        if data2['status'] != 'OK':
            gL.log(gL.WARNING, "GooglePlaces Status " + data['status'])
            return False    
        d = data2['result']
        if d['url']:
            url = d['url']
        else:
            url = ''
        
        # ---------------------------- INSERISCO L'ASSET
        AddrCity=AddrCounty=AddrZIP=AddrPhone=AddrPhone1=AddrWebsite=AddrLat=AddrLong=AddrRegion=FormattedAddress=AddrCountry=Address=''
        Asset = gL.dbAsset(country, assettype, gL.GoogleSource, nam, url, AAsset, pid)  # inserisco l'asset        
        if Asset == 0:
            return Asset
        rc = gL.dbAssetTag(Asset, tag, "Tipologia")
        rc = gL.dbAssetPrice(Asset, PriceList, gL.currency)
        
        AddrCounty = AddrStreet = AddrNumber = AddrRegion = AddrCity = AddrZIP = ""; 
        for component in d['address_components']:
            a = component['types']
            if a:
                if a[0] == "locality":             
                            AddrCity = component['long_name']     
                if a[0] == "route":             
                            AddrStreet = component['long_name']     
                if a[0] == "administrative_area_level_1":             
                            AddrRegion = component['long_name']     
                if a[0] == "administrative_area_level_2":             
                            AddrCounty = component['short_name']     
                if a[0] == "administrative_area_level_3":             
                            AddrCity = component['long_name']     
                if a[0] == "street_number":             
                            AddrNumber = component['long_name']     
                if a[0] == "postal_code":             
                            AddrZIP = component['long_name']     
    
        AddrStreet = AddrStreet + " " + AddrNumber                    
        if 'international_phone_number' in d:
            if d['international_phone_number']:
                AddrPhone = d['international_phone_number']
            elif d['formatted_phone_number']:
                AddPhone = d['formatted_phone_number']
        
        punt = 0; nreview = 0
        if 'rating' in d:
            punt = d['rating']        
            nreview = d['user_ratings_total']
        if 'website' in d:
            AddrWebsite = d['website']
        if 'geometry' in d:        
            AddrLat  = d['geometry']['location']['lat']
            AddrLong = d['geometry']['location']['lng']        

        AddrValidated = gL.NO
        FormattedAddress = d['formatted_address']
        AddrList = {'AddrStreet': AddrStreet,
            'AddrCity': AddrCity,
            'AddrCounty': AddrCounty,
            'AddrZIP': AddrZIP,
            'AddrPhone': AddrPhone,
            'AddrPhone1': '',
            'AddrWebsite': AddrWebsite,
            'AddrLat': AddrLat,
            'AddrLong': AddrLong,
            'AddrRegion': AddrRegion,
            'FormattedAddress': FormattedAddress,
            'AddrValidated': gL.YES,
            'AddrCountry': country,
            'Address': indirizzo}
        rc = gL.dbAssettAddress(Asset, AddrList) 
                   
        # gestione recensioni    
        if punt and nreview:            
            r = []
            r.append((int(nreview), int(punt)))
            rc = gL.dbAssetReview(Asset, r)
        
        # gestione orario
        if 'opening_hours' in d:        
            ope = d['opening_hours']['periods']
            #orario = namedtuple('orario', 'ggft')
            orario = []
            for item in ope:
                dayo = fro = dayc = to = '0000'
                if 'close' in item:
                    o = item['open']
                    if 'day' in o:
                        dayo = o['day']
                    if 'time' in o:
                        fro = o['time']
                if 'close' in item:
                    c = item['close']
                    if 'day' in c:
                        dayc = c['day']
                    if 'time' in c:
                        to = c['time']

                orario.append((dayo, fro, to))
            rc = gL.dbAssetOpening(Asset, orario)
    
    except Exception as err:        
        gL.log(gL.ERROR, (name + " " + indirizzo), err)
        return False

    return Asset