suggest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. import os
  30. import json
  31. # YouTube and similar dis-services use user data to generate suggestions
  32. # for users. I want to implement a similar convenience but totally offline.
  33. # So the privacy will be saved, while suggestions will be presented.
  34. from flbry import settings
  35. def record_tags_to_suggestions(tags):
  36. # This function will run everytime a person is clicking the download
  37. # button. Or everytime a person is loading an article. Or when launching
  38. # a livestream.
  39. print(tags)
  40. # Loading the already existing tags
  41. try:
  42. with open(settings.get_settings_folder()+'suggest_tags.json') as json_file:
  43. data = json.load(json_file)
  44. except:
  45. data = {}
  46. # Adding the new tags
  47. for tag in tags:
  48. if tag not in data:
  49. data[tag] = 1
  50. data[tag] = data[tag] * 1.7
  51. # Filter out irrelevant tags
  52. dels = []
  53. for tag in data:
  54. if tag not in tags:
  55. data[tag] = data[tag] * 0.6
  56. if data[tag] < 0.30:
  57. dels.append(tag)
  58. for tag in dels:
  59. del data[tag]
  60. # Saving the file
  61. with open(settings.get_settings_folder()+'suggest_tags.json', 'w') as fp:
  62. json.dump(data, fp, indent=4)
  63. def suggest():
  64. # This function will output a number of most visited tags based on the
  65. # tags file.
  66. # Loading the already existing tags
  67. try:
  68. with open(settings.get_settings_folder()+'suggest_tags.json') as json_file:
  69. data = json.load(json_file)
  70. except:
  71. data = {}
  72. #Then we sort the dictionary
  73. sortd = sorted(data.items(), key=lambda x:x[1], reverse=True)
  74. data = dict(sortd)
  75. # Creating a list
  76. tags = []
  77. for n, tag in enumerate(data):
  78. # We do not want all of the tags. Only a handfull.
  79. if n < 10:
  80. tags.append(tag)
  81. else:
  82. break
  83. # Give the tags back
  84. return tags