Skip to content

[add] deluge - Rename top-level torrent folder#1790

Merged
liiight merged 11 commits intoFlexget:developfrom
andocromn:develop
Apr 18, 2017
Merged

[add] deluge - Rename top-level torrent folder#1790
liiight merged 11 commits intoFlexget:developfrom
andocromn:develop

Conversation

@andocromn
Copy link
Copy Markdown
Contributor

@andocromn andocromn commented Apr 13, 2017

Motivation for changes:

Currently there is no way of doing this. content_filename will rename the folder only if a main file can be identified. This does not work well with Season Packs which support has recently been added for.

Detailed changes:

  • Adds the option container_directory to the Deluge plugin (the container_directory verbiage was adopted from a deluge_mod.py shared to me by tubedogg)
  • container_directory is a jinja2 template used by client.core.rename_folder to rename the top level folder - which is effectively the torrent name in deluge
  • added 'name' to status_keys array to support this
  • rename_folder function has been added after code for content_filename to prevent interference

Config usage if relevant (new plugin or updated schema):

tasks:
  season-packs:
    rss: http://www.****.com/followed.rss
    deluge:
      label: seasonpacks
      addpaused: yes
      path: /media/tv/{{series_name}}/
      movedone: /media/tv/{{series_name}}/
      container_directory: '{{series_name}} - Season {{series_season}}'
    all_series:
      target: 720p
      timeframe: 72 hours
      upgrade: yes
      season_packs: only

To Do:

  • Verify conflicts do not occur when container_directory is used in conjunction with content_filename

Adds the option container_directory to the Deluge plugin
container_directory is a jinja2 template used by client.core.rename_folder to rename the top level folder - which is effectively the torrent name in deluge 
added 'name' to status_keys array to support this
opts.get('main_file_ratio') * 100))

if config.get('container_directory'):
log.info('Renaming Folder %s to %s' % (status['name'], pathscrub(entry.render(config.get('container_directory')))))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the logger do its own string interpolation, pass it the args directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the pathscrub. better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just render it once into a variable and then pass that variable to both the logger and the Deluge function? Saves a bit of processing.

config.setdefault('magnetization_timeout', 0)
config.setdefault('keep_subs', True) # does nothing without 'content_filename' or 'main_file_only' enabled
config.setdefault('hide_sparse_files', False) # does nothing without 'main_file_only' enabled
config.setdefault('container_directory', '')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting an empty default seems redundant to me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, line removed

@tubedogg
Copy link
Copy Markdown
Contributor

I haven't worked with Deluge's rename_folder before, but I wonder if it's smart enough to know if the something passed to it is not actually a folder, given that you can rename a complete folder structure by calling rename_file. If not, and the user sets container_directory with a single-file torrent that has no directory structure, it's going to end up renaming the file itself, since that's the torrent's name.

I don't think I'd rely on torrent name in either case. I'd split the (big?) file by os.sep and then call rename_folder on whichever portions you want, in your case just the top one I guess.

(I'd suggest revising the PR's name to "[add] deluge - Rename top-level torrent folder" so the changelog can pick it up easily. :))

@andocromn andocromn changed the title New Feature - Deluge plugin / Rename torrent (top level folder) [add] deluge - Rename top-level torrent folder Apr 13, 2017
added code to check that there is more than 1 file and abort if the torrent is a single file

if config.get('container_directory'):
if len(status['files']) > 1:
log.info('Renaming Folder %s to %s', status['name'], entry.render(config.get('container_directory')))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think info is a bit much for this. Maybe verbose?

log.info('Renaming Folder %s to %s', status['name'], entry.render(config.get('container_directory')))
main_file_dlist.append(client.core.rename_folder(torrent_id, status['name'], pathscrub(entry.render(config.get('container_directory')))))
else:
log.info('container_directory specified however the number of files is not greater than 1 - skipping rename')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, info is too much, maybe even debug for this one

changed verification from file count greater than 1 to check for a folder structure
changed to folder_rename to use name extracted from folder structure instead of torrent name
removed 'name' from status_keys
 - thanks tubedogg for the suggestions and assistance
log.info('Renaming Folder %s to %s', status['name'], entry.render(config.get('container_directory')))
main_file_dlist.append(client.core.rename_folder(torrent_id, status['name'], pathscrub(entry.render(config.get('container_directory')))))
if opts.get('content_filename') or opts.get('main_file_only'):
folder_structure = big_file_name.split(os.sep)
Copy link
Copy Markdown
Contributor

@tubedogg tubedogg Apr 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw the error local variable 'big_file_name' referenced before assignment if content_filename or main_file_only are set but there isn't one file above the "big" (main) file threshold, or if content_filename is not set (line 626). I'd add big_file_name = '' above line 562 to ensure it's always assigned to something, and then just do if big_file_name: here.

changed logging to verbose / debug per liiight's suggestion
removed condition on main_file_only because big_file_name is only generated if content_filename is valid
changed container_directory to variable retrieved from config or set entry
 - container_directory option did not work if configured by set
per tubedogg's recommendation
@andocromn
Copy link
Copy Markdown
Contributor Author

I've tested as many scenarios as i can think of. I tested to make sure it doesn't break anything if used in conjunction with content_filename and main_file_only. It's a bit kludgy because it ends up replacing the top level folder in content_filename with the container_directory, but it works. Probably would put a note in the wiki recommending against using both, I can't really think of a real world scenario where both would be used

I also also tested magnet links, there was an issue because the magnet has no files. I added code to check that the array 'files' is populated and cancel the rename_folder if there are no files. container_directory can be used with magnet links if convert_magnet: yes is used

@liiight
Copy link
Copy Markdown
Member

liiight commented Apr 18, 2017

You guys tested the shit out of this right? Is it ready to be merged?

@tubedogg
Copy link
Copy Markdown
Contributor

Yeah it should be good to go.

@liiight liiight merged commit 267ffd4 into Flexget:develop Apr 18, 2017
@andocromn
Copy link
Copy Markdown
Contributor Author

Yes, I did a lot of testing. there are some notes in my last comment about kludgy behaviour when using both container_directory and content_filename, it doesn't cause a problem or any errors. I could change the way this is handled, but I don't think anyone would need to use both. Just let me know if you'd like any further updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants