Based off JPS-AU
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

816 řádky
33 KiB

  1. # Standard library packages
  2. import re
  3. import os
  4. import sys
  5. import shutil
  6. import string
  7. import argparse
  8. import html
  9. from urllib.parse import urlparse
  10. import json
  11. import ftplib
  12. # Third-party packages
  13. import requests
  14. from bs4 import BeautifulSoup
  15. from mutagen.flac import FLAC
  16. from mutagen.mp3 import MP3
  17. from torf import Torrent
  18. from tqdm import tqdm
  19. from langdetect import detect
  20. # JPS-AU files
  21. import smpy
  22. def asciiart ():
  23. print("""
  24. ███████╗███╗ ███╗ █████╗ ██╗ ██╗
  25. ██╔════╝████╗ ████║ ██╔══██╗██║ ██║
  26. ███████╗██╔████╔██║█████╗███████║██║ ██║
  27. ╚════██║██║╚██╔╝██║╚════╝██╔══██║██║ ██║
  28. ███████║██║ ╚═╝ ██║ ██║ ██║╚██████╔╝
  29. ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝
  30. """)
  31. # Get arguments using argparse
  32. def getargs():
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument('-dir', '--directory', help='Initiate upload on directory.', nargs='?', required=True)
  35. parser.add_argument("-f", "--freeleech", help="Enables freeleech.", action="store_true")
  36. parser.add_argument("-t", "--tags", help="Add additional tags to the upload.", nargs='?')
  37. parser.add_argument('-n', '--debug', help='Enable debug mode.', action='store_true')
  38. parser.add_argument("-d", "--dryrun", help="Dryrun will carry out all actions other than the actual upload to JPS.", action="store_true")
  39. parser.add_argument("-im", "--imageURL", help='Set the torrent cover URL.', nargs='?')
  40. parser.add_argument("-a", "--artists", help='Set the artists. (Romaji\English)', nargs='?')
  41. parser.add_argument("-ca", "--contributingartists", help='Set the contributing artists. (Romaji\English)', nargs='?')
  42. parser.add_argument("-rt", "--releasetype", help='Set the release type.', nargs='?')
  43. parser.add_argument("-ti", "--title", help='Set the title. (Romaji\English)', nargs='?')
  44. parser.add_argument("-eti", "--editiontitle", help='Set the edition title', nargs='?')
  45. parser.add_argument("-ey", "--editionyear", help='Set the torrent edition year (YYYYMMDD or YYYY).', nargs='?')
  46. parser.add_argument("-ms", "--mediasource", help='Set the media source.', nargs='?')
  47. return parser.parse_args()
  48. # Acquire the authkey used for torrent files from upload.php
  49. def getauthkey():
  50. """
  51. Get SM session authkey for use by uploadtorrent() data dict.
  52. Uses SM login data
  53. :return: authkey
  54. """
  55. smpage = sm.retrieveContent("https://sugoimusic.me/torrents.php?id=118") # Arbitrary page on JPS that has authkey
  56. soup = BeautifulSoup(smpage.text, 'html5lib')
  57. rel2 = str(soup.select('#content .thin .main_column .torrent_table tbody'))
  58. authkey = re.findall('authkey=(.*)&torrent_pass=', rel2)
  59. return authkey
  60. def copytree(src, dst, symlinks=False, ignore=None):
  61. for item in os.listdir(src):
  62. s = os.path.join(src, item)
  63. d = os.path.join(dst, item)
  64. if os.path.isdir(s):
  65. shutil.copytree(s, d, symlinks, ignore)
  66. else:
  67. shutil.copy2(s, d)
  68. # Creates torrent file using torf module.
  69. def createtorrent(authkey, directory, filename, releasedata):
  70. t = Torrent(path=directory,
  71. trackers=[authkey]) # Torf requires we store authkeys in a list object. This makes it easier to add multiple announce urls.
  72. # Set torrent to private as standard practice for private trackers
  73. t.private = True
  74. t.source = "SugoiMusic"
  75. t.generate()
  76. ## Format releasedata to bring a suitable torrent name.
  77. # The reason we don't just use the directory name is because of an error in POSTING.
  78. # POSTS do not seem to POST hangul/jp characters alongside files.
  79. # filename = f"{releasedata['idols[]']} - {releasedata['title']} [{releasedata['media']}-{releasedata['audioformat']}].torrent"
  80. filename = f"{releasedata['title']} [{releasedata['media']}-{releasedata['audioformat']}].torrent"
  81. filename = filename.replace("/","/")
  82. filename = filename.replace(":",":")
  83. filename = filename.replace("?","")
  84. try:
  85. t.write(filename)
  86. print("_" * 100)
  87. print("Torrent creation:\n")
  88. print(f"{filename} has been created.")
  89. except:
  90. print("_" * 100)
  91. print("Torrent creation:\n")
  92. os.remove(filename)
  93. print(f"{filename} already exists, existing torrent will be replaced.")
  94. t.write(filename)
  95. print(f"{filename} has been created.")
  96. return filename
  97. # Reads FLAC file and returns metadata.
  98. def readflac(filename):
  99. read = FLAC(filename)
  100. # get some audio info
  101. audio_info={
  102. "SAMPLE_RATE": read.info.sample_rate,
  103. "BIT_DEPTH": read.info.bits_per_sample
  104. }
  105. # Create dict containing all meta fields we'll be using.
  106. tags={
  107. "ALBUM": read.get('album'),
  108. "ALBUMARTIST": read.get('albumartist'),
  109. "ARTIST": read.get('artist'),
  110. "DATE": read.get('date')[0],
  111. "GENRE": "",#read.get('genre'),
  112. "TITLE": read.get('title'),
  113. "COMMENT": read.get('comment'),
  114. "TRACKNUMBER": read.get('tracknumber')[0].zfill(2),
  115. "DISCNUMBER": read.get('discnumber')}
  116. # Not further looked into this but some FLACs hold a grouping key of contentgroup instead of grouping.
  117. tags['GROUPING'] = read.get('grouping')
  118. ## If grouping returns None we check contentgroup.
  119. # If it still returns none we will ignore it and handle on final checks
  120. if tags['GROUPING'] == None:
  121. tags['GROUPING'] = read.get('contentgroup')
  122. required_tags = ['ALBUM', 'ALBUMARTIST','DATE','TRACKNUMBER']
  123. for k,v in tags.items():
  124. if v == None:
  125. if k in required_tags:
  126. print(f"{k} has returned {v}, this is a required tag")
  127. sys.exit()
  128. return tags, audio_info
  129. # Reads MP3 file and returns metadata.
  130. def readmp3(filename):
  131. read = MP3(filename)
  132. # Create dict containing all meta fields we'll be using.
  133. tags={
  134. "ALBUM": read.get('TALB'), # Album Title
  135. "ALBUMARTIST": read.get('TPE2'), # Album Artist
  136. "ARTIST": read.get('TPE1'), # Track Artist
  137. "DATE": str(read.get('TDRC')), # Date YYYYMMDD (Will need to add a try/except for other possible identifiers)
  138. "GENRE": read.get('TCON').text, # Genre
  139. "TITLE": read.get('TIT2'), # Track Title
  140. "COMMENT": read.get('COMM::eng'), # Track Comment
  141. "GROUPING": read.get('TIT1'), # Grouping
  142. "TRACKNUMBER": re.sub(r"\/.*", "", str(read.get('TRCK'))).zfill(2), # Tracknumber (Format #/Total) Re.sub removes /#
  143. "DISCNUMBER": re.sub(r"\/.*", "", str(read.get('TPOS')))} # Discnumber (Format #/Total) Re.sub removes /#
  144. required_tags = ['ALBUM', 'ALBUMARTIST','DATE','TRACKNUMBER']
  145. for k,v in tags.items():
  146. if v == None:
  147. if k in required_tags:
  148. print(f"{k} has returned {v}, this is a required tag")
  149. sys.exit()
  150. return tags
  151. # Generates new log file based on directory contents
  152. def generatelog(track_titles, log_filename, log_directory):
  153. # Seperate each tracklist entry in the list with a newline
  154. track_titles = '\n'.join([str(x) for x in track_titles])
  155. # Format tracklist layout
  156. log_contents = f"""[size=5][b]Tracklist[/b][/size]\n{track_titles}
  157. """
  158. # If we have chosen to save the tracklist then we write log_contents to a .log file within the log directory specified
  159. if cfg['local_prefs']['save_tracklist']:
  160. # Write to {album_name}.log
  161. with open(f"{log_directory}/{log_filename}.log", "w+") as f:
  162. f.write(log_contents)
  163. # Reset position to first line and read
  164. f.seek(0)
  165. log_contents = f.read()
  166. f.close()
  167. # If debug mode is enabled we will print the log contents.
  168. if debug:
  169. print("_" * 100)
  170. print(f"Log Contents/Tracklisting: {log_contents}")
  171. return log_contents
  172. def readlog(log_name, log_directory):
  173. with open(f"{log_directory}/{log_name}.log", "r+") as f:
  174. log_contents = f.read()
  175. f.close()
  176. return log_contents
  177. def add_to_hangul_dict(hangul , english , category):
  178. hangul = str(hangul)
  179. english = str(english)
  180. categories = ['version','general','artist','genres', 'label', 'distr']
  181. file = f"json_data/dictionary.json"
  182. json_file = open(file, 'r', encoding='utf-8', errors='ignore')
  183. dictionary = json.load(json_file)
  184. json_file.close()
  185. new = dict()
  186. for cats in dictionary:
  187. #== Create the categories in the new temp file
  188. new[cats] = dict()
  189. for key,value in dictionary[cats].items():
  190. #== List all the old items into the new dict
  191. new[cats][key] = value
  192. if hangul in new[category].keys():
  193. if new[category].get(hangul) is None:
  194. if english != 'None':
  195. new[category][hangul] = english
  196. else:
  197. #== Only update if English word has been supplied ==#
  198. if english != 'None':
  199. new[category][hangul] = english
  200. else:
  201. if english == 'None':
  202. new[category][hangul] = None
  203. else:
  204. new[category][hangul] = english
  205. json_write = open(file, 'w+', encoding='utf-8')
  206. json_write.write(json.dumps(new, indent=4, ensure_ascii=False))
  207. json_write.close()
  208. def translate(string, category, result=None, output=None):
  209. file = "json_data/dictionary.json"
  210. with open(file, encoding='utf-8', errors='ignore') as f:
  211. dictionary = json.load(f, strict=False)
  212. category = str(category)
  213. string = str(string)
  214. search = dictionary[category]
  215. string = string.strip()
  216. if string == 'Various Artists':
  217. output = ['Various Artists',None]
  218. else:
  219. #== NO NEED TO SEARCH - STRING HAS HANGUL+ENGLISH or HANGUL+HANGUL ==#
  220. if re.search("\((?P<inside>.*)\)", string):
  221. #== Complete translation, add to dictionary with both values ==#
  222. #== Contains parentheses, need to split
  223. parenthesis = string.split("(")
  224. pre_parenthesis = parenthesis[0].strip()
  225. in_parenthesis = parenthesis[1].replace(")","").strip()
  226. #== Check the order of the parentheses ==#
  227. if re.search("[^\u0000-\u007F]+",pre_parenthesis) and re.search("[^\u0000-\u007F]+",in_parenthesis):
  228. #== Both hangul
  229. first = 'kr'
  230. second = 'kr'
  231. else:
  232. if re.search("[^\u0000-\u007F]+",pre_parenthesis):
  233. first = 'kr'
  234. second = 'eng'
  235. else:
  236. first = 'eng'
  237. second = 'kr'
  238. if first == 'kr' and second == 'eng':
  239. #== Hangul first ==#
  240. hangul = pre_parenthesis
  241. english = in_parenthesis
  242. add_to_hangul_dict(hangul,english,category)
  243. elif first == 'eng' and second == 'kr':
  244. #== English first ==#
  245. hangul = in_parenthesis
  246. english = pre_parenthesis
  247. add_to_hangul_dict(hangul,english,category)
  248. elif first == 'kr' and second == 'kr':
  249. #== Both Hangul ==#
  250. hangul = pre_parenthesis
  251. english = None
  252. add_to_hangul_dict(pre_parenthesis,None,category)
  253. add_to_hangul_dict(hangul,None,category)
  254. else:
  255. #== Both English
  256. hangul = None
  257. english = pre_parenthesis
  258. output = [hangul,english]
  259. #== No parentheses - HANGUL
  260. else:
  261. #== If the input string is a full Hangul word - check dictionary and then add if necessary)
  262. if re.search("[^\u0000-\u007F]+", string):
  263. if string in search.keys():
  264. #== yes
  265. if search.get(string) is None:
  266. #== If the keyword does not have a translation, add it to the dictionary ==#
  267. output = [string,None]
  268. else:
  269. #== Translation already exists, output the result in a list ==#
  270. output = [string,search.get(string)]
  271. else:
  272. output = [string,None]
  273. add_to_hangul_dict(string, None, category)
  274. #== Full English name -- leave it
  275. else:
  276. for key,value in search.items():
  277. if key == string:
  278. output = [value,string]
  279. break
  280. else:
  281. output = [string,string]
  282. return output
  283. def determine_flac_bitdepth_and_samplerate(audio_info):
  284. if audio_info['BIT_DEPTH'] == 16:
  285. return "Lossless"
  286. elif audio_info['BIT_DEPTH'] == 24 and audio_info['SAMPLE_RATE'] == 96000:
  287. return "24bit Lossless 96kHz"
  288. elif audio_info['BIT_DEPTH'] == 24 and audio_info['SAMPLE_RATE'] == 48000:
  289. return "24bit Lossless 48kHz"
  290. else:
  291. return "24bit Lossless"
  292. def gatherdata(directory):
  293. # Lists for storing some
  294. list_album_artists = []
  295. list_track_artists = []
  296. list_album = []
  297. list_genre = []
  298. translated_genre = []
  299. translated_album_artists = []
  300. tracklist_entries = []
  301. # Creation of releasedata dict, this will store formatted meta used for the POST.
  302. releasedata = {}
  303. ## Set no log as default value.
  304. # This will be set to True is a .log file is found, in turn this will allow us to determine if WEB or CD.
  305. log_available = False
  306. flac_present = False
  307. mp3_present = False
  308. # Read directory contents, grab metadata of .FLAC files.
  309. for file in os.listdir(directory):
  310. file_location = os.path.join(directory, file)
  311. if file.endswith(".flac"):
  312. # Read FLAC file to grab meta
  313. tags, audio_info = readflac(file_location)
  314. flac_present = True
  315. # If Discnumber isn't present then we omit it from the tracklist entry
  316. if tags['DISCNUMBER'] == None:
  317. tracklist_entry = f"[b]{tags['TRACKNUMBER']}[/b]. {tags['TITLE'][0]}"
  318. else:
  319. tracklist_entry = f"[b]{tags['DISCNUMBER'][0]}-{tags['TRACKNUMBER']}[/b]. {tags['TITLE'][0]}"
  320. tracklist_entries.append(tracklist_entry)
  321. if debug:
  322. print ("_" * 100)
  323. print(f"Tags for {file}:\n{tags}")
  324. if file.endswith(".mp3"):
  325. # Read MP3 file to grab meta
  326. tags = readmp3(file_location)
  327. mp3_present = True
  328. # If Discnumber isn't present then we omit it from the tracklist entry
  329. if tags['DISCNUMBER'] == "None":
  330. tracklist_entry = f"[b]{tags['TRACKNUMBER']}[/b]. {tags['TITLE'][0]}"
  331. else:
  332. tracklist_entry = f"[b]{tags['DISCNUMBER']}-{tags['TRACKNUMBER']}[/b]. {tags['TITLE'][0]}"
  333. tracklist_entries.append(tracklist_entry)
  334. if debug:
  335. print ("_" * 100)
  336. print(f"Tags for {file}:\n{tags}")
  337. # If only one genre in list attempt to split as there's likely more.
  338. if len(tags['GENRE']) == 1:
  339. tags['GENRE'] = tags['GENRE'][0].split(";")
  340. for aa in tags['ALBUMARTIST']:
  341. list_album_artists.append(aa)
  342. for a in tags['ARTIST']:
  343. list_track_artists.append(a)
  344. list_album.append(tags['ALBUM'][0])
  345. # for g in tags['GENRE']:
  346. # list_genre.append(g)
  347. # Check files to make sure there's no multi-format.
  348. if flac_present:
  349. format = 'FLAC'
  350. bitrate = determine_flac_bitdepth_and_samplerate(audio_info)
  351. if mp3_present:
  352. format = 'MP3'
  353. bitrate = '320'
  354. if flac_present and mp3_present:
  355. print("Mutt detected, exiting.")
  356. sys.exit()
  357. if file.endswith(".log"):
  358. log_available = True
  359. if log_available == True:
  360. media = 'CD'
  361. else:
  362. media = 'Web'
  363. # Load Dict.json for translations
  364. file = "json_data/dictionary.json"
  365. with open(file, encoding='utf-8', errors='ignore') as f:
  366. dictionary = json.load(f, strict=False)
  367. # Split additional genre's at comma and append to existing genre tags
  368. if additional_tags != None:
  369. split_tags = additional_tags.split(",")
  370. for s in split_tags:
  371. list_genre.append(s)
  372. # Translate genre's using dict and append to translated_genre
  373. for g in set(list_genre):
  374. translation = translate(g, "genres")[0]
  375. translated_genre.append(translation)
  376. # Translate artist's using dict and append to translated_album_artists
  377. for a in set(list_album_artists):
  378. if tags['ALBUMARTIST'][0] == 'Various Artists':
  379. translated_artist_name = 'V.A.'
  380. translated_album_artists.append("V.A.")
  381. else:
  382. translated_artist_name = translate(string=tags['ALBUMARTIST'][0], category="artist")
  383. translated_album_artists.append(translated_artist_name[0])
  384. ## Identify unique values using sets.
  385. unique_album_artists = ','.join(set(translated_album_artists))
  386. unique_track_artists = ','.join(set(list_track_artists))
  387. unique_genre = ','.join(set(translated_genre))
  388. unique_album = set(list_album)
  389. ## Acquire contents of our log file to be used for album description
  390. # Comments store the album id which matches our log names, so we can use the comment tag to find our album descriptions.
  391. log_directory = cfg['local_prefs']['log_directory']
  392. # Album description taken from log file.
  393. if cfg['local_prefs']['generate_tracklist']:
  394. log_filename = f"{unique_album_artists} - {tags['ALBUM'][0]}"
  395. album_description = generatelog(tracklist_entries, log_filename, log_directory)
  396. else:
  397. log_filename = tags['COMMENT'][0]
  398. album_description = readlog(log_filename, log_directory)
  399. ## If release description is enabled we apply comments to the bugs album url
  400. # Note that this is dependant on the album being sourced from bugs so should be changed per user.
  401. if cfg['local_prefs']['enable_release_description']:
  402. try:
  403. release_description = f"Sourced from [url=https://music.bugs.co.kr/album/{tags['COMMENT'][0]}]Bugs[/url]"
  404. # If any exceptions occur we will return to no release description
  405. except:
  406. release_description = ""
  407. # If release description is not enabled we will use no release description
  408. else:
  409. release_description = ""
  410. ## Assign all our unique values into releasedata{}. We'll use this later down the line for POSTING.
  411. # POST values can be found by inspecting JPS HTML
  412. releasedata['submit'] = 'true'
  413. # List of accepted upload types
  414. accepted_types = ['Album', 'Single', 'EP']
  415. # Get type from args if present
  416. if releasetype:
  417. releasedata['type'] = releasetype
  418. else:
  419. # If type errors then we ask for user input
  420. try:
  421. releasedata['type'] = translate(tags['GROUPING'][0], "release_types")[0]
  422. except TypeError:
  423. releasedata['type'] = input("\n" + "_" * 100 + "\nGrouping is empty or has received an error, please enter manually (Album/Single/EP)\n")
  424. # If type is still not in accepted_types we ask for user input again and do not break loop until correct
  425. if releasedata['type'] not in accepted_types:
  426. while True:
  427. releasedata['type'] = input("\n" + "_" * 100 + "\nGrouping tag or release type argument did not return an album type, please enter manually (Album/Single/EP)\n")
  428. if releasedata['type'] not in accepted_types:
  429. continue
  430. else:
  431. break
  432. # SM uses numbers for it's types
  433. if releasedata['type'] == "Album":
  434. releasedata['type'] = 0
  435. elif releasedata['type'] == "Single":
  436. releasedata['type'] = 2
  437. else: # EP type
  438. releasedata['type'] = 1
  439. releasedata['title'] = tags['ALBUM'][0]
  440. releasedata['idols[]'] = unique_album_artists
  441. # If the value of album artist and artist is the same, we don't need to POST original artist.
  442. if unique_album_artists != unique_track_artists:
  443. releasedata['artist_jp'] = unique_track_artists
  444. #re.sub removes any date separators, jps doesn't accept them
  445. releasedata['year'] = re.sub(r"[^0-9]", "", tags['DATE'])
  446. releasedata['audioformat'] = format
  447. releasedata['bitrate'] = bitrate
  448. releasedata['media'] = media
  449. releasedata['album_desc'] = album_description
  450. releasedata['release_desc'] = release_description
  451. releasedata['tags'] = unique_genre
  452. # Enable freeleech if arg is passed
  453. if freeleech:
  454. releasedata['freeleech'] = "true"
  455. ## Language Checks
  456. # This is a required check as we don't want to enter non-english/romaji characters into the title/artist field.
  457. en = detectlanguage(releasedata['title'])
  458. if debug:
  459. print("_" * 100)
  460. print("Title/Artist Language:\n")
  461. print(f"{releasedata['title']} < English = {en}")
  462. if en == False:
  463. if title:
  464. input_english_title = title
  465. else:
  466. input_english_title = input("\n" + "_" * 100 + "\nKorean/Japanese Detected. Please enter the romaji/english title:\n")
  467. # Create new key called title_jp and assign the old title to it
  468. releasedata['title_jp'] = releasedata['title']
  469. # Replace title with the user input.
  470. releasedata['title'] = input_english_title
  471. en = detectlanguage(releasedata['idols[]'])
  472. if debug:
  473. print(f"{releasedata['idols[]']} < English = {en}")
  474. if en == False:
  475. if artists:
  476. input_english_artist = artists
  477. else:
  478. input_english_artist = input("\n" + "_" * 100 + "\nKorean/Japanese Detected. Separate multiple main artists with \",\". Please enter the romaji/english artist name:\n")
  479. input_english_artist = [x.strip() for x in input_english_artist.split(',')]
  480. releasedata['idols[]'] = input_english_artist
  481. if contributingartists:
  482. input_english_contributing_artist = contributingartists
  483. else:
  484. input_english_contributing_artist = input("\n" + "_" * 100 + "\nSeparate multiple contributing artists with \",\". Press enter to skip. Please enter the romaji/english artist name:\n")
  485. if input_english_contributing_artist != "":
  486. input_english_contributing_artist = [x.strip() for x in input_english_contributing_artist.split(',')]
  487. releasedata['contrib_artists[]'] = input_english_contributing_artist
  488. if mediasource:
  489. releasedata['media'] = mediasource
  490. else:
  491. while(True):
  492. input_mediasource = input("\n" + "_" * 100 + "\nEnter a number to choose the media source. \n1=CD\n2=Web\n3=Vinyl\n")
  493. if input_mediasource == "1":
  494. releasedata["media"] = "CD"
  495. break
  496. elif input_mediasource == "2":
  497. releasedata["media"] = "Web"
  498. break
  499. elif input_mediasource == "3":
  500. releasedata["media"] = "Vinyl"
  501. break
  502. print("Invalid choice.")
  503. if editiontitle:
  504. releasedata['remastertitle'] = editiontitle
  505. else:
  506. input_editiontitle = input("\n" + "_" * 100 + "\nEnter the edition TITLE. Press enter to skip.\n")
  507. print(input_editiontitle)
  508. if input_editiontitle != "":
  509. if editionyear:
  510. releasedata["remasteryear"] = editionyear
  511. else:
  512. input_editionyear = input("\n" + "_" * 100 + "\nEnter the edition year as YYYYMMDD or YYYY.\n")
  513. releasedata["remasteryear"] = input_editionyear
  514. releasedata['remastertitle'] = input_editiontitle
  515. return releasedata
  516. # Simple function to split a string up into characters
  517. def split(word):
  518. return [char for char in word]
  519. def detectlanguage(string):
  520. ## Language Detect
  521. # This is a required check as we don't want to enter non-english/romaji characters into the title field.
  522. characters = split(string)
  523. language_list = []
  524. for c in characters:
  525. try:
  526. language = detect(c)
  527. language_list.append(language)
  528. except:
  529. langauge = "error"
  530. if 'ko' or 'ja' in language_list:
  531. en = False
  532. else:
  533. en = True
  534. return en
  535. def uploadtorrent(torrent, imageURL, releasedata):
  536. # POST url.
  537. uploadurl = "https://sugoimusic.me/upload.php"
  538. # Dataset containing all of the information obtained from our FLAC files.
  539. data = releasedata
  540. data['image'] = imageURL
  541. if not dryrun:
  542. data['auth'] = authkey
  543. if debug:
  544. print('_' * 100)
  545. print('Release Data:\n')
  546. print(releasedata)
  547. try:
  548. postDataFiles = {
  549. 'file_input': open(torrent, 'rb')
  550. #'userfile': open(cover, 'rb')
  551. }
  552. except FileNotFoundError:
  553. print("_" * 100)
  554. print('File not found!\nPlease confirm file locations and names. Cover image or .torrent file could not be found')
  555. sys.exit()
  556. # If dryrun argument has not ben passed we will POST the results to JPopSuki.
  557. if dryrun != True:
  558. SMres = sm.retrieveContent(uploadurl, "post", data, postDataFiles)
  559. SMerrorTorrent = re.findall('red; text-align: center;">(.*)</p>', SMres.text)
  560. # SMerrorLogon = re.findall('<p>Invalid (.*)</p>', SMres.text)
  561. if len(SMerrorTorrent)!=0:
  562. print("Upload failed. Torrent error")
  563. print(SMerrorTorrent)
  564. # if len(SMerrorTorrent)!=0:
  565. # print("Upload failed. Logon error")
  566. # print(SMerrorLogon)
  567. if dryrun != True:
  568. print('\nUpload POSTED. It may take a moment for this upload to appear on SugoiMusic.')
  569. ## TODO Filter through JPSres.text and create error handling based on responses
  570. #print(JPSres.text)
  571. # Function for transferring the contents of the torrent as well as the torrent.
  572. def ftp_transfer(fileSource, fileDestination, directory, folder_name, watch_folder):
  573. # Create session
  574. session = ftplib.FTP(cfg['ftp_prefs']['ftp_server'],cfg['ftp_prefs']['ftp_username'],cfg['ftp_prefs']['ftp_password'])
  575. # Set session encoding to utf-8 so we can properly handle hangul/other special characters
  576. session.encoding='utf-8'
  577. # Successful FTP Login Print
  578. print("_" * 100)
  579. print("FTP Login Successful")
  580. print(f"Server Name: {cfg['ftp_prefs']['ftp_server']} : Username: {cfg['ftp_prefs']['ftp_username']}\n")
  581. if cfg['ftp_prefs']['add_to_downloads_folder']:
  582. # Create folder based on the directory name of the folder within the torrent.
  583. try:
  584. session.mkd(f"{fileDestination}/{folder_name}")
  585. print(f'Created directory {fileDestination}/{folder_name}')
  586. except ftplib.error_perm:
  587. pass
  588. # Notify user we are beginning the transfer.
  589. print(f"Beginning transfer...")
  590. # Set current folder to the users preferred destination
  591. session.cwd(f"{fileDestination}/{folder_name}")
  592. # Transfer each file in the chosen directory
  593. for file in os.listdir(directory):
  594. with open(f"{directory}/{file}",'rb') as f:
  595. filesize = os.path.getsize(f"{directory}/{file}")
  596. ## Transfer file
  597. # tqdm used for better user feedback.
  598. with tqdm(unit = 'blocks', unit_scale = True, leave = False, miniters = 1, desc = f'Uploading [{file}]', total = filesize) as tqdm_instance:
  599. session.storbinary('STOR ' + file, f, 2048, callback = lambda sent: tqdm_instance.update(len(sent)))
  600. print(f"{file} | Complete!")
  601. f.close()
  602. if cfg['ftp_prefs']['add_to_watch_folder']:
  603. with open(fileSource,'rb') as t:
  604. # Set current folder to watch directory
  605. session.cwd(watch_folder)
  606. ## Transfer file
  607. # We avoid tqdm here due to the filesize of torrent files.
  608. # Most connections will upload these within 1-3s, resulting in near useless progress bars.
  609. session.storbinary(f"STOR {torrentfile}", t)
  610. print(f"{torrentfile} | Sent to watch folder!")
  611. t.close()
  612. # Quit session when complete.
  613. session.quit()
  614. def localfileorganization(torrent, directory, watch_folder, downloads_folder):
  615. # Move torrent directory to downloads_folder
  616. if cfg['local_prefs']['add_to_downloads_folder']:
  617. try:
  618. os.mkdir(os.path.join(downloads_folder, os.path.basename(directory)))
  619. except FileExistsError:
  620. pass
  621. copytree(directory, os.path.join(downloads_folder, os.path.basename(directory)))
  622. shutil.rmtree(directory)
  623. if cfg['local_prefs']['add_to_watch_folder']:
  624. os.rename(torrent, f"{watch_folder}/{torrent}")
  625. if __name__ == "__main__":
  626. asciiart()
  627. args = getargs()
  628. with open(f'json_data/config.json') as f:
  629. cfg = json.load(f)
  630. # TODO consider calling args[] directly, we will then not need this line
  631. dryrun = freeleech = tags = directory = debug = imageURL = artists = contributingartists = releasetype = title = editiontitle = editionyear = mediasource = audio_info = None
  632. directory = args.directory
  633. additional_tags = args.tags
  634. if args.dryrun:
  635. dryrun = True
  636. if args.debug:
  637. debug = True
  638. if args.freeleech:
  639. freeleech = True
  640. if args.imageURL:
  641. imageURL = args.imageURL
  642. if args.releasetype:
  643. releasetype = args.releasetype
  644. if args.title:
  645. title = args.title
  646. if args.artists:
  647. artists = args.artists
  648. if args.contributingartists:
  649. contributingartists = args.contributingartists
  650. if args.editiontitle:
  651. editiontitle = args.editiontitle
  652. if args.editionyear:
  653. editionyear = args.editionyear
  654. if args.mediasource:
  655. mediatype = args.mediasource
  656. # Load login credentials from JSON and use them to create a login session.
  657. loginData = {'username': cfg['credentials']['username'], 'password': cfg['credentials']['password']}
  658. loginUrl = "https://sugoimusic.me/login.php"
  659. loginTestUrl = "https://sugoimusic.me"
  660. successStr = "Enabled users"
  661. passkey = cfg['credentials']['passkey']
  662. annouceurl = "https://tracker.sugoimusic.me:24601/"+passkey+"/announce"
  663. # j is an object which can be used to make requests with respect to the loginsession
  664. sm = smpy.MyLoginSession(loginUrl, loginData, loginTestUrl, successStr, debug=args.debug)
  665. # Acquire authkey
  666. authkey = getauthkey()
  667. # Gather data of FLAC file
  668. releasedata = gatherdata(directory)
  669. # Folder_name equals the last folder in the path, this is used to rename .torrent files to something relevant.
  670. folder_name = os.path.basename(os.path.normpath(directory))
  671. # Identifying cover.jpg path
  672. # cover_path = directory + "/" + cfg['local_prefs']['cover_name']
  673. # Create torrent file.
  674. #torrentfile = createtorrent(authkey, directory, folder_name, releasedata)
  675. torrentfile = createtorrent(annouceurl, directory, folder_name, releasedata)
  676. # Upload torrent to SugoiMusic
  677. uploadtorrent(torrentfile, imageURL, releasedata)
  678. # Setting variable for watch/download folders
  679. ftp_watch_folder = cfg['ftp_prefs']['ftp_watch_folder']
  680. ftp_downloads_folder = cfg['ftp_prefs']['ftp_downloads_folder']
  681. local_watch_folder = cfg['local_prefs']['local_watch_folder']
  682. local_downloads_folder = cfg['local_prefs']['local_downloads_folder']
  683. if not dryrun:
  684. if cfg['ftp_prefs']['enable_ftp']:
  685. ftp_transfer(fileSource=torrentfile, fileDestination=ftp_downloads_folder, directory=directory, folder_name=folder_name, watch_folder=ftp_watch_folder)
  686. if cfg['local_prefs']['add_to_watch_folder'] or cfg['local_prefs']['add_to_downloads_folder']:
  687. localfileorganization(torrent=torrentfile, directory=directory, watch_folder=local_watch_folder, downloads_folder=local_downloads_folder)