Skip to content

Commit

Permalink
Fix lectureVideo to accept multiple formats
Browse files Browse the repository at this point in the history
  • Loading branch information
nishanthvijayan committed Mar 23, 2019
1 parent ee3ce58 commit 12d2753
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
18 changes: 14 additions & 4 deletions nptel/lectureVideo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package nptel

import (
"fmt"
"log"
"net/url"
"strings"
)

Expand All @@ -13,21 +15,29 @@ type lectureVideo struct {
}

func newLectureVideo(downloadURL string) *lectureVideo {
urlParts := strings.Split(downloadURL, "=")
videoID := strings.TrimSuffix(strings.Split(urlParts[2], "&")[0], ".mp4")
topicName := urlParts[len(urlParts)-1]
queryParams, err := url.ParseQuery(downloadURL)
if err != nil {
log.Fatal("Malformed url found. Aborting")
}

filename := queryParams.Get("filename")
videoID := strings.Split(filename, ".")[0]
format := strings.Split(filename, ".")[1]
topicName := queryParams.Get("subjectName")

return &lectureVideo{
topicName: topicName,
videoID: videoID,
downloadURL: downloadURL,
format: format,
}
}

func (lecture lectureVideo) generateFileName(outputDirectory string) string {
return fmt.Sprintf("%s/%s-%s.mp4",
return fmt.Sprintf("%s/%s-%s.%s",
strings.TrimRight(outputDirectory, "/"),
lecture.videoID,
lecture.topicName,
lecture.format,
)
}
6 changes: 3 additions & 3 deletions nptel/lectureVideo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func Test_NewLectureVideoParses3GPLink(t *testing.T) {
t.Errorf("VideoID was not parsed properly")
}

if lectureVideo.format != "3GP" {
if lectureVideo.format != "3gp" {
t.Errorf("Format was not parsed properly")
}

if lectureVideo.topicName != "Basic%20Operations" {
if lectureVideo.topicName != "Basic Operations" {
t.Errorf("VideoID was not parsed properly")
}
}
Expand All @@ -44,7 +44,7 @@ func Test_NewLectureVideoParsesFLVLink(t *testing.T) {
t.Errorf("VideoID was not parsed properly")
}

if lectureVideo.format != "FLV" {
if lectureVideo.format != "flv" {
t.Errorf("Format was not parsed properly")
}

Expand Down

0 comments on commit 12d2753

Please sign in to comment.