Originally created by slyyxp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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