# -***coding=utf-8***-
#生成用户推荐数据
from tool.db import connect,findall,find,connect_user,connect_recommend
from tool.cos_similar import compare

# user_map = {
#     "叙利亚" : 1.258916900764898,
#     "土耳其" : 0.730705582013571,
#     "少将" : 0.457011874506735,
#     "伊斯兰" : 0.334567150094694,
#     "老虎" : 0.324918628541224,
#     "恐怖组织" : 0.228420968385714,
#     "狙击手" : 0.214966975959184,
#     "精锐部队" : 0.206000309432653,
#     "特种部队" : 0.189704233999796,
#     "双方" : 0.176319771164388,
#     "部队" : 0.173137293552857,
#     "武装" : 0.14395376555898,
#     "兵力" : 0.140860571627143,
#     "坦克" : 0.135671739467755,
#     "势力" : 0.130290701001225,
#     "分子" : 0.130014985334082,
#     "叙军" : 0.18198742349898,
#     "破格提拔" : 0.1173753304,
#     "交火" : 0.113551927854082,
#     "将星" : 0.110777094023469,
#     "巴沙尔" : 0.341564785797143,
#     "美俄" : 0.341564785797143,
#     "中东" : 0.286952017738286,
#     "土军" : 0.227709857198095,
#     "军事" : 0.210250480566476,
#     "五角大楼" : 0.205051113066667,
#     "军事力量" : 0.170111701950476,
#     "对象" : 0.168542504968,
#     "边境" : 0.188300987012381,
#     "阿德尔" : 0.132387406209524,
#     "用心险恶" : 0.115323030311429,
#     "安更" : 0.113854928599048,
#     "会令" : 0.113854928599048,
#     "俄土" : 0.113854928599048,
#     "军队" : 0.108578845556,
#     "全面" : 0.107124352886667,
#     "专家" : 0.106702652524381,
#     "特朗普" : 0.510116611082569,
#     "索萨村" : 0.329030298244954,
#     "空军" : 0.24819813632,
#     "小楼" : 0.24431551737055,
#     "指挥官" : 0.239057116358807,
#     "哈金镇" : 0.219353532163303,
#     "阿萨德" : 0.214742258249541,
#     "政府军" : 0.187372443998165,
#     "边境地区" : 0.175118694042018,
#     "伊朗" : 0.139246657666422,
#     "内战" : 0.137714056034128,
#     "局势" : 0.135823770688073,
#     "一锅端" : 0.112763667334862,
#     "林赛" : 0.112763667334862,
#     "走炮" : 0.109676766081651,
#     "线报" : 0.109676766081651,
#     "祖尔" : 0.109676766081651,
#     "叙伊" : 0.109676766081651
#                }

# python = connect().python
# user_map = {
#     'python':20
# }

user_db = connect().users
users = findall(user_db)
news_keys = connect().news_keys
datas = findall(news_keys)
# count = 0
# num = 0
# for data in datas:
#     value = data['keywords']
#     count += 1
#     # print(compare(user_map, value))
#     print(count)
#     if 'python' in list(value.keys()):
#         num += 1
#         print('*********{}*****'.format(num))
#         python.insert(data)



for user in users:
    username = user['username']

def fun(username):
    user = connect_user()[username]
    recommend = connect_recommend()[username]
    user_map = list(findall(user))[0]['keys']
    count = 0
    num = 0
    for data in datas:
        value = data['keywords']
        count += 1
        # print(compare(user_map, value))
        print(count)
        if compare(user_map, value) > 0.5:
            res = list(find(recommend, {'url': data['url']}))
            if res == []:
                num += 1
                print('*********{}*****'.format(num))
                recommend.insert(data)

if __name__ == '__main__':
    fun('kkkk')

