Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import os, sys, math, cmath, hashlib, codecs
  6.  
  7. def md5_file(fname):
  8. md5ob = hashlib.md5()
  9. with open(fname,"rb") as f:
  10. for chunk in iter(lambda: f.read(8192), ""):
  11. md5ob.update(chunk)
  12. return md5ob.hexdigest()
  13.  
  14.  
  15. def strstr(haystack, needle, b_needle = False):
  16. pos = haystack.find(needle)
  17. if pos < 0:
  18. return None
  19. else:
  20. if b_needle == True:
  21. return haystack[:pos]
  22. else:
  23. return haystack[pos:]
  24.  
  25.  
  26. class BMS_Reader:
  27.  
  28. # Directives for basic information (metadatas)
  29. B_PLAYTYPE = "PLAYER" # Play mode
  30. B_MUSIC_GENRE="GENRE" # Genre of the music
  31. B_SCORE_TITLE="TITLE" # Title of the music
  32. B_MUSIC_ARTIST="ARTIST" # Artist name
  33. B_MUSIC_BPM="BPM" # BPM (beats per minute / Tempo)
  34. B_SCORE_LEVEL="PLAYLEVEL" # Playing Difficulty
  35. B_BRIEF_RANK="RANK" # Judge Level
  36. B_PARAM_TOTAL="TOTAL" # Gauge Totals
  37. B_MIXLEVEL="DIFFICULTY" # Display difficulty name
  38. B_DET_RANK="DEFEXRANK" # Judge Rank (detailed)
  39. B_BGMFILE="MIDIFILE" # Name of Background Music file
  40. B_VOLUME="VOLWAV" # Sound Volume
  41.  
  42. # Maximum channel numbers
  43. SP5KEYS = 16 # Single Play (5 keys) / 11~15: Keys 1~5, 16: SC
  44. SP7KEYS = 19 # Single Play (7 keys) / 11~15: Keys 1~5, 16: SC, 18~19: Keys 6~7
  45. DP9KEYS = 25 # Double Play (9 keys) / 11~15: Keys 1~5, 22~25: Keys 6~9
  46. DP10KEYS = 26 # Double Play (10 keys) / 11~15: 1P Keys 1~5, 16: 1P SC, 21~25: 2P Keys 1~5, 26: 2P SC
  47. DP14KEYS = 29 # Double Play (14 keys) / 11~15: 1P Keys 1~5, 16: 1P SC, 18~19: 1P Keys 6~7, 21~25: 2P Keys 1~5, 26: 2P SC, 28~29: 2P Keys 6~7
  48. # for Long-notes
  49. LN_SP5KEYS = 56
  50. LN_SP7KEYS = 59
  51. LN_DP9KEYS = 65
  52. LN_DP10KEYS = 66
  53. LN_DP14KEYS = 69
  54. # Channel number for scratch notes
  55. SCR_LEFT = 16
  56. SCR_RIGHT = 26
  57. LN_SCR_LEFT = 56
  58. LN_SCR_RIGHT = 66
  59.  
  60.  
  61. mixlevels = {}
  62. mixlevels[1] = "Beginner"
  63. mixlevels[2] = "Normal"
  64. mixlevels[3] = "Hyper"
  65. mixlevels[4] = "Another"
  66. mixlevels[5] = "Insane"
  67.  
  68. playtypes = {}
  69. playtypes[1] = "Single"
  70. playtypes[2] = "Two"
  71. playtypes[3] = "Double or 9-key"
  72.  
  73. brief_ranks = ["Very Hard", "Hard", "Normal", "Easy"]
  74.  
  75. def __init__(self, path):
  76. self.path = path
  77. #self.charset = charset
  78. self.handle = open(path,"r",encoding = "sjis")
  79.  
  80. def parseMetadata(self):
  81. data = {}
  82. flagsfound = 0
  83. playtyped = False
  84. tpath = os.path.abspath(self.path)
  85. data["filepath"] = tpath.replace(os.pathsep,"/")
  86. data["path"] = os.path.dirname(data["filepath"])
  87. data["filename"] = os.path.basename(self.path)
  88. data["filesize"] = os.path.getsize(tpath)
  89. #data["filehash"] = md5_file(tpath)
  90. #parameter = ""
  91. self.handle.seek(0)
  92. for eachlines in self.handle.readlines():
  93. if eachlines[0] == "#":
  94. parameter = strstr(eachlines," ",True)
  95. print("b--"+str(eachlines[0]))
  96. parameter = parameter.replace("#","")
  97. print("a--"+str(parameter))
  98. parameter = parameter.upper()
  99.  
  100. value = strstr(eachlines," ")
  101. value = value.strip()
  102. else:
  103. continue
  104.  
  105. if parameter == self.B_PLAYTYPE:
  106. data["playtype_id"] = int(value)
  107. data["playtype_name"] = self.playtypes[int(value)]
  108. playtyped = True
  109. elif parameter == self.B_MUSIC_GENRE:
  110. data["genre"] = value
  111. elif parameter == self.B_SCORE_TITLE:
  112. data["title"] = value
  113. elif parameter == self.B_BGMFILE:
  114. data["bgmfile"] = value
  115. elif parameter == self.B_VOLUME:
  116. if value.isdight() == False or (value is None):
  117. data["volume"] = 100
  118. else:
  119. data["volume"] = float(value)
  120. elif parameter == self.B_MUSIC_BPM:
  121. data["bpm"] = float(value)
  122. if value.isdight() == False:
  123. data["bpm"] = 130
  124. elif parameter == self.B_SCORE_LEVEL:
  125. data["plevel"] = int(value)
  126. elif parameter == self.B_BRIEF_RANK:
  127. if (value is None) or value.isdight() == False:
  128. data["rank_id"] = 0
  129. data["rank_name"] = "Unknown"
  130. else:
  131. data["rank_id"] = int(value)
  132. data["rank_name"] = self.brief_ranks[int(value)]
  133. elif parameter == self.B_PARAM_TOTAL:
  134. data["total"] = int(value)
  135. elif parameter == self.B_MIXLEVEL:
  136. data["mixlevel"] = self.mixlevels[int(value)]
  137. elif parameter == self.B_DET_RANK:
  138. data["decrank"] = value
  139.  
  140. if playtyped == False:
  141. del data
  142. data = {"error_code": 0x0F, "error_desc": "This file is not a valid BMS file!"}
  143. return data
  144. return data
  145.  
  146.  
  147.  
  148. example = BMS_Reader(os.path.abspath("example.bme"))
  149. print(example.parseMetadata())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement