Based off JPS-AU
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

autoupload.py 29 KiB

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