2019-02-10 15:26:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
from json import loads, dumps
|
2019-03-02 07:54:25 +03:00
|
|
|
from os import path, environ
|
2019-02-10 15:26:50 +03:00
|
|
|
from pathlib import Path
|
|
|
|
from random import choice
|
|
|
|
from sys import argv, stdout
|
|
|
|
from re import findall
|
|
|
|
from vars import useragent
|
|
|
|
from tinydb import TinyDB, Query, operations
|
|
|
|
from time import sleep
|
|
|
|
import sqlite3
|
|
|
|
import requests
|
|
|
|
|
|
|
|
dirname = path.dirname(path.realpath(__file__))
|
2019-03-02 07:54:25 +03:00
|
|
|
|
|
|
|
queryid = argv[1]
|
|
|
|
|
|
|
|
serversrc = 'dist' if environ.get('LEON_NODE_ENV') == 'production' else 'src'
|
|
|
|
queryobjfile = open(dirname + '/../../server/' + serversrc + '/tmp/' + queryid + '.json', 'r', encoding = 'utf8')
|
|
|
|
queryobj = loads(queryobjfile.read())
|
|
|
|
queryobjfile.close()
|
|
|
|
|
|
|
|
def getqueryobj():
|
|
|
|
"""Return query object"""
|
|
|
|
|
|
|
|
return queryobj
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
def translate(key, d = { }):
|
|
|
|
"""Pickup the language file according to the cmd arg
|
|
|
|
and return the value regarding to the params"""
|
|
|
|
|
|
|
|
output = ''
|
|
|
|
|
2019-03-02 07:54:25 +03:00
|
|
|
file = open(dirname + '/../../packages/' + queryobj['package'] + '/' + 'data/answers/' + queryobj['lang'] + '.json', 'r', encoding = 'utf8')
|
2019-02-10 15:26:50 +03:00
|
|
|
obj = loads(file.read())
|
|
|
|
file.close()
|
|
|
|
|
2019-03-02 07:54:25 +03:00
|
|
|
prop = obj[queryobj['module']][key]
|
2019-02-10 15:26:50 +03:00
|
|
|
if isinstance(prop, list):
|
|
|
|
output = choice(prop)
|
|
|
|
else:
|
|
|
|
output = prop
|
|
|
|
|
|
|
|
if d:
|
|
|
|
for k in d:
|
|
|
|
output = output.replace('%' + k + '%', str(d[k]))
|
|
|
|
|
|
|
|
# "Temporize" for the data buffer ouput on the core
|
|
|
|
sleep(0.1)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def output(type, code, speech = ''):
|
|
|
|
"""Communicate with the Core"""
|
|
|
|
|
|
|
|
print(dumps({
|
2019-03-02 07:54:25 +03:00
|
|
|
'package': queryobj['package'],
|
|
|
|
'module': queryobj['module'],
|
|
|
|
'lang': queryobj['lang'],
|
|
|
|
'input': queryobj['query'],
|
|
|
|
'entities': queryobj['entities'],
|
2019-02-10 15:26:50 +03:00
|
|
|
'output': {
|
|
|
|
'type': type,
|
|
|
|
'code': code,
|
|
|
|
'speech': speech,
|
|
|
|
'options': config('options')
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
if (type == 'inter'):
|
|
|
|
stdout.flush()
|
|
|
|
|
2019-03-02 07:54:25 +03:00
|
|
|
def finddomains(string, entities):
|
2019-02-10 15:26:50 +03:00
|
|
|
"""Find a domain name substring from a string"""
|
|
|
|
|
2019-02-17 12:32:04 +03:00
|
|
|
return findall('[a-z0-9\-]{,63}\.[a-z0-9\-\.]{2,191}', string.lower())
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
def http(method, url):
|
|
|
|
"""Send HTTP request with the Leon user agent"""
|
|
|
|
|
|
|
|
session = requests.Session()
|
|
|
|
session.headers.update({ 'User-Agent': useragent, 'Cache-Control': 'no-cache' })
|
|
|
|
|
|
|
|
return session.request(method, url)
|
|
|
|
|
|
|
|
def config(key):
|
|
|
|
"""Get a package configuration value"""
|
|
|
|
|
2019-03-02 07:54:25 +03:00
|
|
|
file = open(dirname + '/../../packages/' + queryobj['package'] + '/config/config.json', 'r', encoding = 'utf8')
|
2019-02-10 15:26:50 +03:00
|
|
|
obj = loads(file.read())
|
|
|
|
file.close()
|
|
|
|
|
2019-03-02 07:54:25 +03:00
|
|
|
return obj[queryobj['module']][key]
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
def info():
|
|
|
|
"""Get information from the current query"""
|
|
|
|
|
|
|
|
return { 'lang': lang, 'package': package, 'module': module }
|
|
|
|
|
|
|
|
def createdldir():
|
|
|
|
"""Create the downloads folder of a current module"""
|
|
|
|
|
|
|
|
dldir = path.dirname(path.realpath(__file__)) + '/../../downloads/'
|
2019-03-02 07:54:25 +03:00
|
|
|
moduledldir = dldir + queryobj['package'] + '/' + queryobj['module']
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
Path(moduledldir).mkdir(parents = True, exist_ok = True)
|
|
|
|
|
|
|
|
return moduledldir
|
|
|
|
|
|
|
|
def db(dbtype = 'tinydb'):
|
|
|
|
"""Create a new dedicated database
|
|
|
|
for a specific package"""
|
|
|
|
|
|
|
|
if dbtype == 'tinydb':
|
2019-03-02 07:54:25 +03:00
|
|
|
db = TinyDB(dirname + '/../../packages/' + queryobj['package'] + '/data/db/' + queryobj['package'] + '.json')
|
2019-02-10 15:26:50 +03:00
|
|
|
return { 'db': db, 'query': Query, 'operations': operations }
|
|
|
|
|