Guest User

Untitled

a guest
Mar 1st, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. import re
  2. import requests
  3. import sys
  4. import os
  5.  
  6. # Header required for downloading audio files
  7. HEADERS = {
  8. "X-Access": "please don't steal these files"
  9. }
  10.  
  11. # Function to fetch HTML from jpdb.io
  12. def fetch_html(query):
  13. url = f"https://jpdb.io/search?q={query}"
  14. response = requests.get(url)
  15. response.raise_for_status()
  16. response.encoding = response.apparent_encoding # Ensure correct encoding
  17. return response.text
  18.  
  19. # Function to extract spellings and audio IDs
  20. def extract_spellings(html):
  21. pattern = re.compile(
  22. r'<ruby class="v(?: nf)?">(.*?)</ruby>.*?data-audio="m1/([a-f0-9]+)"',
  23. re.DOTALL
  24. )
  25. matches = pattern.findall(html)
  26.  
  27. spellings = []
  28. for ruby_content, audio_id in matches:
  29. # Extract kanji and furigana parts separately
  30. kanji_parts = re.findall(r'([^<rt>]+)(?:<rt>(.*?)</rt>)?', ruby_content)
  31.  
  32. # Build kanji and reading properly
  33. kanji_result = ""
  34. reading_result = ""
  35.  
  36. for kanji, furigana in kanji_parts:
  37. kanji_result += kanji.strip() # Keep kanji as is
  38. if furigana:
  39. reading_result += furigana.strip() # Add furigana if present
  40. else:
  41. reading_result += kanji.strip() # If no furigana, use kanji (kana case)
  42.  
  43. filename = f"{kanji_result}-{reading_result}-{audio_id}.ogg"
  44. spellings.append((filename, audio_id))
  45.  
  46. return spellings
  47.  
  48. # Function to download and decode audio files
  49. def download_and_decode(audio_id, filename):
  50. url = f"https://jpdb.io/static/v/m1/{audio_id}"
  51. response = requests.get(url, headers=HEADERS)
  52.  
  53. if response.status_code != 200:
  54. print(f"Failed to download {filename} ({response.status_code})")
  55. return
  56.  
  57. # Save the raw encoded file first (optional)
  58. encoded_path = f"encoded_{audio_id}.ogg"
  59. with open(encoded_path, "wb") as f:
  60. f.write(response.content)
  61.  
  62. # Decode the file
  63. with open(encoded_path, "rb") as f:
  64. data = bytearray(f.read())
  65.  
  66. # XOR the first 4 bytes
  67. key = [0x06, 0x23, 0x54, 0x0f]
  68. for i in range(4):
  69. data[i] ^= key[i]
  70.  
  71. # Save the decoded file
  72. with open(filename, "wb") as f:
  73. f.write(data)
  74.  
  75. # Clean up the encoded file
  76. os.remove(encoded_path)
  77. print(f"Saved: {filename}")
  78.  
  79. def main():
  80. if len(sys.argv) != 2:
  81. print("Usage: python jpdb.py <query>")
  82. sys.exit(1)
  83.  
  84. query = sys.argv[1]
  85. html = fetch_html(query)
  86. spellings = extract_spellings(html)
  87.  
  88. for filename, audio_id in spellings:
  89. download_and_decode(audio_id, filename)
  90.  
  91. if __name__ == "__main__":
  92. main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment