API.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import json
  2. from yjdaemon.Database import Database as db
  3. import configparser
  4. """
  5. Add a key to the validAPIcalls dictionary, with a corresponding function
  6. Function should return jsonified data, so that it can then be passed on to the client.
  7. example:
  8. Add this to the dictionary
  9. "getsongs": calls.getsongs
  10. then implement this function
  11. @staticmethod
  12. def getsongs():
  13. return calls.jsonify({"song": song})
  14. And the jsonified data will be returned to the client.
  15. Every function MUST return jsonified data!
  16. """
  17. class calls:
  18. @staticmethod
  19. def APIcall(sanitizedpath):
  20. if sanitizedpath in validAPIcalls:
  21. return validAPIcalls[sanitizedpath]
  22. else:
  23. return None
  24. @staticmethod
  25. def getfromrawjson(data, param):
  26. return calls.dejsonify(data)[param]
  27. @staticmethod
  28. def jsonify(data):
  29. return json.dumps(data, sort_keys=True, indent=4).encode("utf-8")
  30. @staticmethod
  31. def dejsonify(rawdata):
  32. return json.loads(rawdata.decode("utf-8"))
  33. @staticmethod
  34. def getallsongs(args):
  35. return calls.jsonify({"songs": db.executequerystatic("SELECT * FROM tracks;"), "args": args , "result": "OK"})
  36. @staticmethod
  37. def getartists(args):
  38. return calls.jsonify({"artists": db.executequerystatic("SELECT artistName FROM tracks GROUP BY artistName;"), "args": args, "result" : "OK"})
  39. @staticmethod
  40. def getalbums(args):
  41. return calls.jsonify({"albums": db.executequerystatic("SELECT albumName FROM tracks GROUP BY albumName;"), "args": args, "result":"OK"})
  42. @staticmethod
  43. def getgenres(args):
  44. return calls.jsonify({"genres": db.executequerystatic("SELECT genre FROM tracks GROUP BY genre;"), "args": args, "result":"OK"})
  45. @staticmethod
  46. def getyears(args):
  47. return calls.jsonify({"years": db.executequerystatic("SELECT year FROM tracks GROUP BY year;"), "args": args, "result":"OK"})
  48. @staticmethod
  49. def getalbumnames(args):
  50. return calls.jsonify({"albumnames": db.executequerystatic("SELECT albumName FROM tracks GROUP BY albumName;"), "args": args})
  51. @staticmethod
  52. def search(args):
  53. """search?
  54. q(gneeral search term)=iets&
  55. artist=bleh
  56. &genre=neeee"""
  57. data = args.split("&")
  58. songnames = """
  59. SELECT * FROM tracks WHERE LCASE(trackName) LIKE LCASE(\"%{}%\") GROUP BY trackName;
  60. """
  61. artists = """
  62. SELECT artistName FROM tracks WHERE LCASE(artistName) LIKE LCASE(\"%{}%\") GROUP BY artistName;
  63. """
  64. genres = """
  65. SELECT genre FROM tracks WHERE LCASE(genre) LIKE LCASE(\"%{}%\") GROUP BY genre;
  66. """
  67. albums = """
  68. SELECT albumName FROM tracks WHERE LCASE(albumName) LIKE LCASE(\"%{}%\") GROUP BY albumName;
  69. """
  70. genquery="""
  71. SELECT * FROM tracks WHERE LCASE(genre) LIKE LCASE(\"%{}%\") OR LCASE(albumName) LIKE LCASE(\"%{}%\") OR LCASE(trackName) LIKE LCASE(\"%{}%\") OR LCASE(artistName) LIKE LCASE(\"%{}%\");
  72. """
  73. genqueryres = ""
  74. artistsres = ""
  75. genresres = ""
  76. songnamesres = ""
  77. albumsres = ""
  78. for entry in data:
  79. values = entry.split("=")
  80. if values[0] == "q":
  81. genqueryres = genquery.format( values[1], values[1], values[1], values[1])
  82. elif values[0] == "artist":
  83. artistsres = artists.format(values[1])
  84. elif values[0] == "genre":
  85. genresres = genres.format(values[1])
  86. elif values[0] == "songname":
  87. songnamesres = songnames.format(values[1])
  88. elif values[0] == "album":
  89. albumsres = albums.format(values[1])
  90. return calls.jsonify({"result":"OK", "genres": db.executequerystatic(genresres), "songnames": db.executequerystatic(songnamesres), "artists": db.executequerystatic(artistsres), "albums": db.executequerystatic(albumsres), "query" : db.executequerystatic(genqueryres)})
  91. return calls.jsonify({"result":"OK"})
  92. @staticmethod
  93. def getsongs(args):
  94. """Get song by album, genre, year, artist"""
  95. data = args.partition("?")
  96. splitstring = data[0].partition("=")
  97. name= splitstring[0]
  98. value = splitstring[2].replace("%20"," ")
  99. if name == "album":
  100. songs = db.executequerystatic("SELECT * FROM tracks WHERE albumName = \"" + value + "\"")
  101. elif name == "genre":
  102. songs = db.executequerystatic("SELECT * FROM tracks WHERE genre = \"" + value + "\"")
  103. elif name == "year":
  104. songs = db.executequerystatic("SELECT * FROM tracks WHERE year = \"" + value + "\"")
  105. elif name == "artist":
  106. songs = db.executequerystatic("SELECT * FROM tracks WHERE artistName = \"" + value + "\"")
  107. elif name == "search":
  108. songs = db.executequerystatic("SELECT * FROM tracks WHERE LCASE(trackName) LIKE LCASE(\"%"+ value + "%\") GROUP BY trackName")
  109. else:
  110. return calls.jsonify({"result":"NOK", "errormsg": "Not a valid argument"})
  111. return calls.jsonify({"result":"OK","songs":songs})
  112. @staticmethod
  113. def getsongbyid(args):
  114. config = configparser.ConfigParser()
  115. try:
  116. config.read("config.cfg")
  117. musicdir = config.get("Library","musicdir")
  118. port = config.get("HTTP","port")
  119. except:
  120. return calls.jsonify({"result" : "NOK" , "errormsg" : "I/O error while reading config."})
  121. data = args.split("&")
  122. splitsting = data[0].split("=")
  123. id = splitsting[1]
  124. file = db.executequerystatic(
  125. "SELECT SUBSTRING_INDEX(trackUrl,'" + musicdir + "',-1) as filedir FROM `tracks` WHERE id = " + id)
  126. try:
  127. url = str(file[0][0])
  128. except:
  129. return calls.jsonify({"result": "NOK", "errormsg" : "Song ID does not exist in database."})
  130. return calls.jsonify({"result": "OK", "songurl": "http://imegumii.nl:"+ "/music/English"+ url})
  131. @staticmethod
  132. def setsong(args, songname):
  133. global song
  134. song = songname
  135. return calls.jsonify({"result": "OK", "args": args})
  136. validAPIcalls = {"getallsongs": calls.getallsongs,
  137. "setsong": calls.setsong,
  138. "search": calls.search,
  139. "getsongs": calls.getsongs,
  140. "getsongbyid": calls.getsongbyid,
  141. "getartists": calls.getartists,
  142. "getalbums": calls.getalbums,
  143. "getgenres": calls.getgenres,
  144. "getyears": calls.getyears,
  145. "getalbumnames": calls.getalbumnames
  146. }