Skip to content

Commit

Permalink
main: Support universal download
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Mar 6, 2018
1 parent b933bad commit 863d20b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ Title: 好冷 逢考必过

You can also use the `-i` option to view video information, skip download.

### Download anything else

If you already got the URL of the exact resource you want, you can download it directly:

```console
$ annie https://img9.bcyimg.com/drawer/15294/post/1799t/1f5a87801a0711e898b12b640777720f.jpg

annie doesn't support this URL by now, but it will try to download it directly

Site: Universal
Title: 1f5a87801a0711e898b12b640777720f
Type: image/jpeg
Size: 1.00 MiB (1051042 Bytes)

1.00 MiB / 1.00 MiB [===================================] 100.00% 3.35 MiB/s 0s
```

### Resume a download

You may use <kbd>Ctrl</kbd>+<kbd>C</kbd> to interrupt a download.
Expand Down
32 changes: 32 additions & 0 deletions extractors/universal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package extractors

import (
"fmt"

"github.com/iawia002/annie/downloader"
"github.com/iawia002/annie/request"
"github.com/iawia002/annie/utils"
)

// Universal download function
func Universal(url string) downloader.VideoData {
fmt.Println()
fmt.Println("annie doesn't support this URL by now, but it will try to download it directly")

filename, ext := utils.GetNameAndExt(url)
size := request.Size(url, url)
urlData := downloader.URLData{
URL: url,
Size: size,
Ext: ext,
}
data := downloader.VideoData{
Site: "Universal",
Title: utils.FileName(filename),
Type: request.ContentType(url, url),
URLs: []downloader.URLData{urlData},
Size: size,
}
data.Download(url)
return data
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ func main() {
case "pixivision":
extractors.Pixivision(videoURL)
default:
fmt.Println("unsupported URL")
extractors.Universal(videoURL)
}
}
21 changes: 18 additions & 3 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/iawia002/annie/config"
Expand Down Expand Up @@ -77,14 +78,28 @@ func Get(url string) string {
return string(body)
}

// Size get size of the url
func Size(url, refer string) int64 {
// Headers return the HTTP Headers of the url
func Headers(url, refer string) http.Header {
headers := map[string]string{
"Referer": refer,
}
res := Request("GET", url, nil, headers)
defer res.Body.Close()
s := res.Header.Get("Content-Length")
return res.Header
}

// Size get size of the url
func Size(url, refer string) int64 {
h := Headers(url, refer)
s := h.Get("Content-Length")
size, _ := strconv.ParseInt(s, 10, 64)
return size
}

// ContentType get Content-Type of the url
func ContentType(url, refer string) string {
h := Headers(url, refer)
s := h.Get("Content-Type")
// handle Content-Type like this: "text/html; charset=utf-8"
return strings.Split(s, ";")[0]
}
12 changes: 11 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"regexp"
"strings"

"github.com/iawia002/annie/request"
)

// Match1 return result of first match
Expand Down Expand Up @@ -68,5 +70,13 @@ func GetNameAndExt(uri string) (string, string) {
u, _ := url.ParseRequestURI(uri)
s := strings.Split(u.Path, "/")
filename := strings.Split(s[len(s)-1], ".")
return filename[0], filename[1]
if len(filename) > 1 {
return filename[0], filename[1]
} else {
// Image url like this
// https://img9.bcyimg.com/drawer/15294/post/1799t/1f5a87801a0711e898b12b640777720f.jpg/w650
// has no suffix
contentType := request.ContentType(uri, uri)
return filename[0], strings.Split(contentType, "/")[1]
}
}

0 comments on commit 863d20b

Please sign in to comment.