Class: Showfix::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/showfix/cleaner.rb

Constant Summary collapse

AUDIO_FLAGS =
%w(DD51 AC3 Dubbed DD20 Synced AAC DTS MP3).freeze
VIDEO_FLAGS =
%w(1080p 720p XviD x264 h264 HDTV euHD PDTV HD Divx5).freeze
SOURCE_FLAGS =
%w(
  DVDRip HDTVRip HDRip SATRip BDRip iTunesHD WEBRiP BLURAYRiP BluRay 
  iTunesHDRip RiP WEB DL PublicHD CAMRip WP TC Telecine CAM TS PDVD
  Telesync PPV PPVRip SCR SCREENER DVDSCR DDC R5 HDDVD DVDRips
).freeze
AUTHOR_FLAGS =
%w(
  inspired AMBiTiOUS SiGHT RiP iNTERNAL CRoW c0nFuSed UTOPiA scum EXPiRED
  CRiSP ZZGtv ARCHiV Prim3time Nfo Repack SiMPTY DELiCiOUS UNDELiCiOUS
  fBi CiD RedSeven OiNK dxva FraMeSToR CtrlHD LOL drngr PARADOX RELOADED
  DIMENSION anoXmous YIFY KILLERS HiDt Z3R0K BiTT ViSiON KiNGDOM SPARKS
  MVGroup JIVE RKSTR ESiR
).freeze
FLAGS =
[AUDIO_FLAGS, VIDEO_FLAGS, SOURCE_FLAGS, AUTHOR_FLAGS].flatten.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cleaner

Returns a new instance of Cleaner.



25
26
27
28
29
30
# File 'lib/showfix/cleaner.rb', line 25

def initialize(options={})
  @options = {
    flags: true,
    year: true
    }.merge(options)
end

Instance Method Details

#clean(string) ⇒ Object

Perform some cleanup on the filename



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/showfix/cleaner.rb', line 33

def clean(string)

  string = self.unix_friendly(string)

  # Clean trailing/leading periods
  string = self.trim(string)

  if @options[:flags]
    string = self.remove_flags(string)
  end

  if @options[:year]
    string = self.strip_year(string)
  end

  # Clean trailing/leading periods
  string = self.trim(string)

  string
end

#remove_flags(string) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/showfix/cleaner.rb', line 68

def remove_flags(string)
  flags = FLAGS.join('|')
  
  string.gsub(/(\W(#{flags})){2,}/i, '')
    .gsub(/((#{flags})\W){2,}/i, '')
    .gsub(/^((#{flags})\W?){2,}$/i, '')
    .gsub(/\W(#{flags}){1,}$/i, '')
end

#strip_year(string) ⇒ Object



77
78
79
80
# File 'lib/showfix/cleaner.rb', line 77

def strip_year(string)
  string.gsub(/^(?:19|20)\d{2}\D/, '\1')
  string.gsub(/(?:19|20)\d{2}$/, '\1')
end

#trim(string) ⇒ Object



54
55
56
# File 'lib/showfix/cleaner.rb', line 54

def trim(string)
  string.strip.gsub(/\A\.+|\.+\Z/, '')
end

#unix_friendly(string) ⇒ Object

Make unix friendly



59
60
61
62
63
64
65
66
# File 'lib/showfix/cleaner.rb', line 59

def unix_friendly(string)
  string.gsub(/\s+-\s+/, '.')
    .gsub(/[- \(\)]+/, '.')
    .gsub(/'/,'')
    .gsub(/[\[\]]+/,'')
    .gsub(/&/,'.and.')
    .gsub(/\.{2,}/,'.')
end