#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

import os
import json

# YouTube and similar dis-services use user data to generate suggestions
# for users. I want to implement a similar convenience but totally offline.
# So the privacy will be saved, while suggestions will be presented.

from flbry import settings


def record_tags_to_suggestions(tags):

    # This function will run everytime a person is clicking the download
    # button. Or everytime a person is loading an article. Or when launching
    # a livestream.

    print(tags)

    # Loading the already existing tags
    try:
        with open(settings.get_settings_folder()+'suggest_tags.json') as json_file: 
            data = json.load(json_file)
    except:
        data = {}

    # Adding the new tags
    for tag in tags:
        if tag not in data:
            data[tag] = 1

        data[tag] = data[tag] * 1.7

    # Filter out irrelevant tags
    dels = []
    for tag in data:
        if tag not in tags:
            data[tag] = data[tag] * 0.6
        if data[tag] < 0.30:
            dels.append(tag)
    for tag in dels:
        del data[tag]
        
    # Saving the file
    with open(settings.get_settings_folder()+'suggest_tags.json', 'w') as fp:
            json.dump(data, fp, indent=4)

def suggest():
    
    # This function will output a number of most visited tags based on the
    # tags file.

    # Loading the already existing tags
    try:
        with open(settings.get_settings_folder()+'suggest_tags.json') as json_file: 
            data = json.load(json_file)
    except:
        data = {}

    #Then we sort the dictionary
    sortd = sorted(data.items(), key=lambda x:x[1], reverse=True)
    data = dict(sortd)

    # Creating a list
    tags = []
    for n, tag in enumerate(data):

        # We do not want all of the tags. Only a handfull.

        if n < 10:
            tags.append(tag)
        else:
            break

    # Give the tags back
    return tags
