Skip to content

Commit

Permalink
sort subs by rank and preferred format
Browse files Browse the repository at this point in the history
  • Loading branch information
martin sarsale committed Jul 4, 2012
1 parent 517b9db commit 9d71775
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions bin/getsub
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def identify_by_imdb!(server, movie)
end

def select_sub(subs)
return subs.first if subs.length >= 1
if subs.length >= 1
return preferred_format(sort_by_rank(subs),["srt",'txt'])
end
movies = group_by_movie_name(subs)
return movies.values.first.max if movies.length == 1

Expand All @@ -105,7 +107,27 @@ def select_sub(subs)
selected_movie_subs = ask_user_to_identify_movie(movies)
selected_movie_subs.max
end

# sort subs by rank. uses uploader user rank, downloads count, bad sub reports and sub rating
# @param Array<OSDb::Sub> subs
# @return Array<OSDb::Sub> sorted array
def sort_by_rank(subs)
r = subs.sort_by{|s|
rank = ((s.raw_data['UserRank'].empty? ? 1 : 2) * (s.raw_data['SubDownloadsCnt'].to_i + 1) * (s.raw_data['SubRating'].to_f + 1)) - (s.raw_data['SubBad'].to_i / (s.raw_data['SubDownloadsCnt'].to_i+1))
rank
}.reverse
r
end
# select the sub with the preferred format
# @param Array<OSDb::Sub> subs An array of subs to choose from
# @param Array<string> formats_orig An ordered list of formats to choose from, eg ["srt","sub","txt"]
# @return OSDb::Sub
def preferred_format(subs, formats_orig)
formats = formats_orig.reverse
subs.sort_by{|sub|
formats.index(formats.find{|format|
sub.raw_data['SubFormat'].index(format)
}).to_s}.reverse.first
end
def select_and_download!(subs, movie)
sub = select_sub(subs)
sub_path = movie.sub_path(sub.format)
Expand Down

0 comments on commit 9d71775

Please sign in to comment.