render.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
  2. # THE FOLLOWING SOURCE CODE I UNDER THE GNU
  3. # AGPL LICENSE V3 OR ANY LATER VERSION.
  4. # This project is not for simple users, but for
  5. # web-masters and a like, so we are counting on
  6. # your ability to set it up and running.
  7. from modules import search
  8. def html(page, json):
  9. # This function adds a rendering of the json into the page
  10. free = search.is_free(json)
  11. page = page + "\n <h1>"
  12. try:
  13. page = page + '\n<img src="'+ json["links"]["icon"] + '" alt="Logo" style="height:50px;">'
  14. except:
  15. pass
  16. name = json.get("names",["Unknown"])[0]
  17. page = page + "\n" + name
  18. page = page + "</h1>"
  19. # Few words about it
  20. comment = json.get("comment","")
  21. not_foss = ['open source', 'open-source']
  22. if "open source" or "open-source" in comment.lower():
  23. # Well... Here is a thing. Free Software, not open source.
  24. where = comment.lower().find("open source")
  25. # In case it has a slash in it.
  26. if where == -1:
  27. where = comment.lower().find("open-source")
  28. os = comment[where:where+11]
  29. comment = comment.replace(os,
  30. "<a href=\"https://www.gnu.org/philosophy/open-source-misses-the-point.en.html\">"+os+"</a>")
  31. page = page + "<p>"+comment+"</p>"
  32. # I want to show nothing else from if it's proprietary
  33. if "issues" in json:
  34. l = json.get("issues", [])
  35. page = page +"<h2>Anti-Features / Problems:</h2>"
  36. for i in l:
  37. page = page + "&nbsp;&nbsp;"+i+"<br>"
  38. if not free:
  39. return page
  40. # Links
  41. # <table>
  42. # <tr>
  43. # <th>Company</th>
  44. # <th>Contact</th>
  45. # <th>Country</th>
  46. #</tr>
  47. page = page + """
  48. <style>
  49. table, th, td {
  50. border-right:none;
  51. border-left:none;
  52. border-bottom:none;
  53. border-top:none
  54. }</style>
  55. """
  56. page = page + "<table><tr>"
  57. links = json.get("links", {})
  58. for website in links:
  59. if website in ["icon"]:
  60. continue
  61. link = links[website]
  62. page = page + """
  63. <th><form action=\""""+link+"""\">
  64. <button title=\""""+link+"""\" type="submit">"""+website.upper()+"""</button>
  65. </form></th>
  66. """
  67. page = page + "</tr></table>"
  68. # Details
  69. categories = {"generic_name":"Features",
  70. "licenses":"License(s)",
  71. "platforms":"Platforms",
  72. "networks_read":"Accesses Data from",
  73. "networks_write":"Interacts / Publishes to",
  74. "formats_read":"Opens from File-Formats",
  75. "formats_write":"Saves to File-Formats",
  76. "interface":"Interface",
  77. "languages":"Programming Languages"}
  78. for c in categories:
  79. l = json.get(c, [])
  80. if not l:
  81. continue
  82. page = page + "<details>"
  83. page = page +"<summary>"+categories[c]+":</summary>"
  84. for i in l:
  85. page = page + "&nbsp;&nbsp;"+i+"<br>"
  86. page = page + "</details>"
  87. return page
  88. def suggestions(page, json):
  89. # This function will render suggestions
  90. page = page + "<h1>Free Competitors:</h1>"
  91. found = search.suggest(json)
  92. biggest = 0
  93. for i in found:
  94. if i[0] > biggest:
  95. biggest = i[0]
  96. more = False
  97. for i in found:
  98. free = search.is_free(i[-1])
  99. if not i[0] or i[-1] == json or not free:
  100. continue
  101. try:
  102. frac = int(i[0]/biggest*100)
  103. except:
  104. frac = 0
  105. if frac < 40 and not more: # Below 40% features match
  106. page = page + """<hr><details>
  107. <summary><h1 title="Click to show more / less.">Problematic Competitors:</h1></summary>"""
  108. more = True
  109. page = page + "<hr><br>Suggestion score: " + str(frac) + "%"
  110. page = html(page, i[-1])
  111. if more:
  112. page = page + "</details>"
  113. return page
  114. def search_widget(page, address):
  115. # Adds a search bar to the page
  116. page = page + """
  117. <form action="""
  118. page = page + address
  119. page = page + """search method="GET">
  120. <input type="text" name="item" class="search" placeholder="Name of Software">
  121. <button type="submit">Search</button>
  122. </form>
  123. """
  124. #page = page.format(ADDRESS)
  125. return page
  126. def source_code_link(page):
  127. # Adds a source code link
  128. page = page + "<br><br><hr><p>This website is under the GNU AGPL license.</p>"
  129. page = page + """
  130. <form action=https://notabug.org/jyamihud/FreeCompetitors>
  131. <button type="submit">Source Code</button>
  132. </form><br><br>
  133. """
  134. return page