Skip to content

Commit

Permalink
fix: parse filenames with urls // fix #50
Browse files Browse the repository at this point in the history
  • Loading branch information
tympanix committed Jan 2, 2019
1 parent 5fbe12f commit 5bf2f67
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions media/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ func TestForeignMovie(t *testing.T) {
assert.Equal(t, 2003, m.Year())
}

func TestMovieWebsite(t *testing.T) {
m, err := NewMovie("[www.example.com] Inception (2010) x264 1080p")
require.NoError(t, err)
assert.Equal(t, "Inception", m.MovieName())
assert.Equal(t, 2010, m.Year())
}

func TestMovieJSON(t *testing.T) {
m, err := NewMovie("Inception.2010.720p")
require.NoError(t, err)
Expand Down
11 changes: 10 additions & 1 deletion parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ func isAbbreviation(str string) bool {
var abbreviationRegexp = regexp.MustCompile(`\b[A-Za-z]([\s\.][A-Za-z])+\b`)
var illegalcharsRegexp = regexp.MustCompile(`[^\p{L}0-9\s&'_\(\)\-,:]`)
var spaceReplaceRegexp = regexp.MustCompile(`[\.\s_]+`)
var websiteRegexp = regexp.MustCompile(`((https?|ftp|smtp):\/\/)?(www.)[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*`)
var trimRegexp = regexp.MustCompile(`^[^\p{L}0-9]*(.+?[\.\)]?)[^\p{L}0-9]*$`)

// CleanName returns the media name cleaned from punctuation
func CleanName(name string) string {
name = websiteRegexp.ReplaceAllString(name, "")
name = spaceReplaceRegexp.ReplaceAllString(name, " ")
name = illegalcharsRegexp.ReplaceAllString(name, "")

Expand All @@ -103,7 +106,13 @@ func CleanName(name string) string {

name = Capitalize(name)

return strings.TrimSpace(name)
match := trimRegexp.FindStringSubmatch(name)

if len(match) > 1 {
return match[1]
} else {
return ""
}
}

var illegalIdentity = regexp.MustCompile(`[^\p{L}0-9]`)
Expand Down
5 changes: 5 additions & 0 deletions parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func TestCleanNameMovieTitles(t *testing.T) {
}
}

func TestCleanNameWebsites(t *testing.T) {
assert.Equal(t, "Inception", CleanName("www.example.com - Inception"))
assert.Equal(t, "Inception", CleanName("[www.example.com].Inception"))
}

func TestIdentity(t *testing.T) {
assert.Equal(t, "thisisatest", Identity("thìs is â tést"))
assert.Equal(t, "vyzkousejtetentoretezec", Identity("vyzkoušejte tento řetězec"))
Expand Down

0 comments on commit 5bf2f67

Please sign in to comment.