Skip to content

Commit

Permalink
Restructured project
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek2211 committed Aug 31, 2019
1 parent 56e96c9 commit 72f2920
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 74 deletions.
75 changes: 10 additions & 65 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,25 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"github.com/gocolly/colly"
"github.com/grafov/m3u8"
"github.com/prateek2211/musiload-go/services"
"github.com/prateek2211/musiload-go/utils"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"github.com/prateek2211/musiload-go/platforms/gaana"
"github.com/prateek2211/musiload-go/platforms/saavn"
"log"
"net/url"
)

func main() {
var songTitle string
c := colly.NewCollector()

c.OnHTML("ul.s_l.artworkload", func(e *colly.HTMLElement) {
link := e.ChildText("span[data-type=\"playSong\"]")
var data map[string]interface{}
err := json.Unmarshal([]byte(link), &data)
if err != nil {
log.Fatal(err.Error())
}
songTitle = data["title"].(string)
highQuality := data["path"].(map[string]interface{})["high"].([]interface{})[0].(map[string]interface{})
cip := highQuality["message"].(string)
decodedURL := utils.Decrypt([]byte(cip), "g@1n!(f1#r.0$)&%", "asd!@#!@#@!12312")
decodedURL = stripCtlAndExtFromUnicode(decodedURL)
err = c.Visit(decodedURL)
if err != nil {
log.Fatal(err.Error())
}
})

c.OnResponse(func(response *colly.Response) {
if (response.Headers.Get("content-type") == "application/vnd.apple.mpegurl") {
fmt.Println("Downloading ...")
playist, _, _ := m3u8.DecodeFrom(bytes.NewReader(response.Body), true)
mp := playist.(*m3u8.MasterPlaylist)
ts := make(chan string, 1024)
go services.ParsePlaylist(mp.Variants[0].URI, ts)
services.DownloadTS(ts, songTitle+".mp3")
//err := ioutil.WriteFile(songTitle+".m3u8", response.Body, 0644)
//if err != nil {
// log.Fatal(err.Error())
//}
}
})

c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})

fmt.Println("Enter the website url:")
var url string
_, err := fmt.Scanln(&url)
var urlIp string
_, err := fmt.Scanln(&urlIp)
if err != nil {
log.Fatal(err.Error())
}
err = c.Visit(url)
if err != nil {
log.Fatal(err.Error())
u, _ := url.Parse(urlIp)
if u.Hostname() == "gaana.com" {
gaana.ParseAndDownload(urlIp)
}
}

func stripCtlAndExtFromUnicode(str string) string {
isOk := func(r rune) bool {
return r < 32 || r >= 127
if u.Hostname() == "www.jiosaavn.com" {
saavn.ParseAndDownload(urlIp)
}
// The isOk filter is such that there is no need to chain to norm.NFC
t := transform.Chain(norm.NFKD, transform.RemoveFunc(isOk))
// This Transformer could also trivially be applied as an io.Reader
// or io.Writer filter to automatically do such filtering when reading
// or writing data anywhere.
str, _, _ = transform.String(t, str)
return str
}
59 changes: 59 additions & 0 deletions platforms/gaana/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gaana

import (
"bytes"
"encoding/json"
"fmt"
"github.com/gocolly/colly"
"github.com/grafov/m3u8"
"github.com/prateek2211/musiload-go/services"
"github.com/prateek2211/musiload-go/utils"
"log"
)

func ParseAndDownload(songUrl string) {
var songTitle string
c := colly.NewCollector()

c.OnHTML("ul.s_l.artworkload", func(e *colly.HTMLElement) {
link := e.ChildText("span[data-type=\"playSong\"]")
var data map[string]interface{}
err := json.Unmarshal([]byte(link), &data)
if err != nil {
log.Fatal(err.Error())
}
songTitle = data["title"].(string)
highQuality := data["path"].(map[string]interface{})["high"].([]interface{})[0].(map[string]interface{})
cip := highQuality["message"].(string)
decodedURL := utils.Decrypt([]byte(cip), "g@1n!(f1#r.0$)&%", "asd!@#!@#@!12312")
decodedURL = utils.StripCtlAndExtFromUnicode(decodedURL)
err = c.Visit(decodedURL)
if err != nil {
log.Fatal(err.Error())
}
})

c.OnResponse(func(response *colly.Response) {
if (response.Headers.Get("content-type") == "application/vnd.apple.mpegurl") {
fmt.Println("Downloading ...")
playist, _, _ := m3u8.DecodeFrom(bytes.NewReader(response.Body), true)
mp := playist.(*m3u8.MasterPlaylist)
ts := make(chan string, 1024)
go services.ParsePlaylist(mp.Variants[0].URI, ts)
services.DownloadTS(ts, songTitle+".mp3")
//err := ioutil.WriteFile(songTitle+".m3u8", response.Body, 0644)
//if err != nil {
// log.Fatal(err.Error())
//}
}
})

c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})

err := c.Visit(songUrl)
if err != nil {
log.Fatal(err.Error())
}
}
12 changes: 3 additions & 9 deletions saavn.go → platforms/saavn/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package saavn

import (
"encoding/json"
Expand All @@ -12,7 +12,7 @@ import (
"strings"
)

func main() {
func ParseAndDownload(songUrl string) {
var songTitle string
c := colly.NewCollector()
c.UserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
Expand Down Expand Up @@ -48,13 +48,7 @@ func main() {
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL.String())
})
fmt.Println("Enter the website url:")
var url string
_, err := fmt.Scanln(&url)
if err != nil {
log.Fatal(err.Error())
}
err = c.Visit(url)
err := c.Visit(songUrl)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
19 changes: 19 additions & 0 deletions utils/text_normalize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import (
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)

func StripCtlAndExtFromUnicode(str string) string {
isOk := func(r rune) bool {
return r < 32 || r >= 127
}
// The isOk filter is such that there is no need to chain to norm.NFC
t := transform.Chain(norm.NFKD, transform.RemoveFunc(isOk))
// This Transformer could also trivially be applied as an io.Reader
// or io.Writer filter to automatically do such filtering when reading
// or writing data anywhere.
str, _, _ = transform.String(t, str)
return str
}

0 comments on commit 72f2920

Please sign in to comment.