Skip to content
Snippets Groups Projects
get_orders.py 9.78 KiB
# -*- coding: utf-8 -*-
import datetime
import sys
import re
import os
import shutil

from yaml import load, dump

try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

def logme(msg):
    print( msg )

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

init_all=False;

try:
    cparam=sys.argv[1]
    if( cparam == 'init' ):
        logme( '.. initialize -> get orders of all registered repositories ..')
        init_all=True;
except IndexError:
    logme('.. check pull.yml for updates ..')

file = open('../_config.yml','r')
s=file.read()
file.close()

Config=load( s, Loader=Loader )


if( not init_all ):
    pullfile='../'+Config['destination']+'/pull.yml'
    if not os.path.exists(pullfile):
        sys.exit(0)

try:
    shutil.rmtree('../_posts')
except OSError:
    logme( '_posts directory not exists')

os.makedirs('../_posts')

Date=datetime.datetime.now().strftime('%Y-%m-%d')


from urllib.request import urlopen

file = open('../_data/country-codes.yml','r')
s=file.read()
file.close()
Country_codes=load( s, Loader=Loader )

file = open('../_data/language-codes.yml','r')
s=file.read()
file.close()
Language_codes=load( s, Loader=Loader )
print( Language_codes )

file = open('../_data/form.yml','r')
s=file.read()
file.close()
Form=load( s, Loader=Loader )


file = open('../_data/repositories.yml','r')
s=file.read()
file.close()
Sources=load( s, Loader=Loader )

if( not init_all ):
    file = open( pullfile ,'r')
    s=file.read()
    file.close()
    Pulls=load( s, Loader=Loader )
else:
    Pulls=Sources

for source in Pulls:

    if not source in Sources:
        logme( '[' + source + '] not found !' )
        continue
    else:
        logme( '[' + source + ']\n .. accessing .. !')

    G=source.split('/')
    gituser=G[-2]
    gitrepo=G[-1]
    git_=G[-3]
    git_=git_.replace('.','_')

    giturl='/'.join(G[0:-2])

    contents = urlopen( source + '/raw/master/index.yml' ).read()
    O=load( contents, Loader=Loader )

    # check index.yml
    if( 'latitude' in O and 'longitude' in O and 'country-code' in O and 'orders' in O ):

        if( O['latitude'] < -90 or O['latitude'] > 90 ):
            logme( 'index.yml[latitude] out of range (-90 .. 90) -> SKIP repository !' )
            continue
        if( O['longitude'] < -180 or O['longitude'] > 180 ):
            logme( 'index.yml[longitude] out of range (-180 .. 180) -> SKIP repository !' )
            continue
        if( O['country-code'].upper() not in Country_codes ):
            logme( 'index.yml[country-code] = "' + O['country-code'] + '" invalid, check _data/country-codes.yml -> SKIP repository !' )
            continue
    else:
        logme( 'index.yml NOT all required fields exists [latitude, longitude, country-code, orders ] -> SKIP repository !' )
        continue

    # get order yml files from repo
    for furl in O['orders']:

        logme('- ['+source + '/raw/master/' + furl+']\n- ..accessing..')
        contents = urlopen( source + '/raw/master/' + furl).read()
        OO=load( contents, Loader=Loader )

        # check order.yml
        if( 'language-code' not in OO ):
            logme('- order.yml[language-code] not found -> SKIP order !')
            continue
        else:
            if( OO['language-code'] not in Language_codes ):
                logme('- order.yml[language-code] = "'+ OO['language-code'] +'" invalid, check _data/language-codes.yml -> SKIP order !')

        if( 'tags' not in OO ):
            logme('- order.yml[tags] not found -> SKIP order !')
            continue
        else:
            if( len(OO['tags']) > 0 and len(OO['tags']) < 4 ):
                if not os.path.exists('../_data/categories/'+(OO['tags'][0]).strip()+'.yml' ):
                    logme('- order.yml[tags] = "'+(OO['tags'][0]).strip()+'" not found -> SKIP order !')
                    continue
            else:
                logme('- order.yml[tags] bad array length = ' + len(OO['tags']) + ' ( should be 1 or 2 or 3 )-> SKIP order !')
                continue

        if( 'headline' not in OO ):
            logme('- order.yml[headline] not found -> SKIP order !')
            continue

        if( 'condition' not in OO ):
            logme('- order.yml[condition] not found -> SKIP order !')
            continue
        else:
            if not os.path.exists('../_data/condition/'+(OO['condition']).strip()+'.yml' ):
                logme('- order.yml[condition] = '+(OO['condition']).strip()+' not found -> SKIP order !')
                continue

        if( 'description' not in OO ):
            logme('- order.yml[description] not found -> SKIP order !')
            continue

        if( 'image-profile' not in OO ):
            OO['image-profile']='none'
            OO['image-preview']=''
            OO['image']=''
        else:
            if( OO['image-profile'] not in Form['fields']['image-profile']['values'] ):
                logme('- order.yml[image-profil] = "'+OO['image-profile']+'", invalid, check _data/form.yml[fields][image-profile][values] -> SKIP order !')
                continue

            if( 'image' not in OO ):
                OO['image']=Form['fields']['image']['default']

        if( 'min-size' not in OO ):
            OO['min-size']=Form['fields']['min-size']['default']
        else:
            if( OO['min-size'] == '' ):
                OO['min-size']=Form['fields']['min-size']['default']
            else:
                if( not is_number(OO['min-size']) ):
                    logme('- order.yml[min-size] = '+OO['min-size']+', invalid number -> SKIP order !')
                    continue

        if( 'max-size' not in OO ):
            OO['max-size']=Form['fields']['max-size']['default']
        else:
            if( OO['max-size'] == '' ):
                OO['max-size']=Form['fields']['max-size']['default']
            else:
                if( not is_number(OO['max-size'] ) ):
                    logme('- order.yml[max-size] = '+OO['max-size']+', invalid number -> SKIP order !')
                    continue

        if( OO['min-size'] > OO['max-size'] ):
            logme('- order.yml[max-size] lower than order.yml[min-size] -> SKIP order !')
            continue

        if( 'unit' not in OO ):
            OO['unit']=Form['fields']['unit']['default']
        else:
            if( OO['unit'] not in Form['fields']['unit']['values'] ):
                logme('- order.yml[unit] = '+OO['unit']+', invalid, check _data/form.yml[fields][unit][values] -> SKIP order !')
                continue

        if( 'price-per-unit' not in OO ):
            logme('- order.yml[price-per-unit] not found -> SKIP order !')
            continue
        else:
            if( not is_number( OO['price-per-unit']) ):
                logme('- order.yml[price-per-unit] = '+ OO['price-per-unit'] +', invalid number -> SKIP order !')
                continue

        if( 'price-per-unit' not in OO ):
            logme('- order.yml[price-per-unit] not found -> SKIP order !')
            continue
        else:
            if( not is_number( OO['price-per-unit']) ):
                logme('- order.yml[price-per-unit] = '+ OO['price-per-unit'] +', invalid number -> SKIP order !')
                continue

        if( 'price-reference' not in OO ):
            logme('- order.yml[price-reference] not found -> SKIP order !')
            continue
        else:
            ps=False
            for p in OO['price-reference']:
                if( len(p) != 3 ):
                    logme('- order.yml[price-reference] requires array with length 3 but is '+str( len(p) )+' -> SKIP price-reference !')
                    continue
                else:
                    if not os.path.exists('../_data/currencies/'+(p[0]).strip()+'.yml' ):
                        logme('- order.yml[price-reference] = '+(p[0]).strip()+'.yml not found in _data/currencies -> SKIP price-reference !')
                        continue
                    else:
                        file = open('../_data/currencies/'+p[0].strip()+'.yml','r')
                        s=file.read()
                        file.close()
                        P=load( s, Loader=Loader )

                        if( p[1].strip() not in P['price-sources'] ):
                            logme('- order.yml[price-reference] = '+(p[0]).strip()+'.yml[price-reference]['+p[1].strip()+']not found in _data/currencies -> SKIP price-reference !')
                            continue
                        else:
                            if( p[2].strip() not in P['price-sources'][p[1].strip()] ):
                                logme('- order.yml[price-reference] = '+(p[0]).strip()+'.yml[price-reference]['+p[1].strip()+']['+p[2].strip()+'] not found in _data/currencies -> SKIP price-reference !')
                                continue
                            else:
                                # OO['price-currency']=P['price-sources'][p[1].strip()]['currency']
                                ps=True

            if( ps == False ):
                logme('- order.yml[price-reference] all sources invalid -> SKIP order !')
                continue

        file = open('../_posts/'+Date + '-' +furl+'.md','w')
        file.write('---\n')
        file.write('layout: none\n')
        file.write('git-repo: '+source+'\n')
        file.write('git-profile: '+giturl + '/'+gituser+'\n' )
        file.write('order-title: '+furl+'\n' )
        file.write('latitude: '+str( O['latitude'] )+'\n' )
        file.write('longitude: '+str( O['longitude'] )+'\n' )
        file.write('country-code: '+ O['country-code'].upper() +'\n' )
        file.write(contents.decode("utf-8"))
        file.write('categories: \n')
        file.write('- '+ git_ +'\n' )
        file.write('- '+ gituser +'\n' )
        file.write('- '+ gitrepo +'\n' )
        file.write('\n---')
        file.close()

if( not init_all ):
    os.remove(pullfile)