2017-12-28 18:14:33 +01:00
|
|
|
package sources
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-03-09 17:47:38 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2017-12-28 18:14:33 +01:00
|
|
|
"github.com/lbryio/lbry.go/jsonrpc"
|
|
|
|
|
2018-03-13 22:48:01 +01:00
|
|
|
"github.com/nikooo777/ytdl"
|
2017-12-28 18:14:33 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/api/youtube/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type YoutubeVideo struct {
|
|
|
|
id string
|
|
|
|
channelTitle string
|
|
|
|
title string
|
|
|
|
description string
|
|
|
|
playlistPosition int64
|
|
|
|
publishedAt time.Time
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewYoutubeVideo(directory string, snippet *youtube.PlaylistItemSnippet) YoutubeVideo {
|
|
|
|
publishedAt, _ := time.Parse(time.RFC3339Nano, snippet.PublishedAt) // ignore parse errors
|
|
|
|
return YoutubeVideo{
|
|
|
|
id: snippet.ResourceId.VideoId,
|
|
|
|
title: snippet.Title,
|
|
|
|
description: snippet.Description,
|
|
|
|
channelTitle: snippet.ChannelTitle,
|
|
|
|
playlistPosition: snippet.Position,
|
|
|
|
publishedAt: publishedAt,
|
|
|
|
dir: directory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v YoutubeVideo) ID() string {
|
|
|
|
return v.id
|
|
|
|
}
|
|
|
|
|
2018-04-25 20:56:26 +02:00
|
|
|
func (v YoutubeVideo) PlaylistPosition() int {
|
|
|
|
return int(v.playlistPosition)
|
|
|
|
}
|
|
|
|
|
2017-12-28 18:14:33 +01:00
|
|
|
func (v YoutubeVideo) IDAndNum() string {
|
|
|
|
return v.ID() + " (" + strconv.Itoa(int(v.playlistPosition)) + " in channel)"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v YoutubeVideo) PublishedAt() time.Time {
|
|
|
|
return v.publishedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v YoutubeVideo) getFilename() string {
|
2018-05-05 13:22:33 +02:00
|
|
|
maxLen := 30
|
2017-12-28 18:14:33 +01:00
|
|
|
reg := regexp.MustCompile(`[^a-zA-Z0-9]+`)
|
|
|
|
|
|
|
|
chunks := strings.Split(strings.ToLower(strings.Trim(reg.ReplaceAllString(v.title, "-"), "-")), "-")
|
|
|
|
|
|
|
|
name := chunks[0]
|
|
|
|
if len(name) > maxLen {
|
|
|
|
return name[:maxLen]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chunk := range chunks[1:] {
|
|
|
|
tmpName := name + "-" + chunk
|
|
|
|
if len(tmpName) > maxLen {
|
|
|
|
if len(name) < 20 {
|
|
|
|
name = tmpName[:maxLen]
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name = tmpName
|
|
|
|
}
|
2018-06-04 16:35:35 +02:00
|
|
|
if len(name) < 1 {
|
|
|
|
name = v.id
|
|
|
|
}
|
2018-06-09 01:14:55 +02:00
|
|
|
return v.dir + "/" + v.id + "/" + name + ".mp4"
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v YoutubeVideo) getAbbrevDescription() string {
|
|
|
|
maxLines := 10
|
|
|
|
description := strings.TrimSpace(v.description)
|
|
|
|
if strings.Count(description, "\n") < maxLines {
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
return strings.Join(strings.Split(description, "\n")[:maxLines], "\n") + "\n..."
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
func (v YoutubeVideo) download() error {
|
2017-12-28 18:14:33 +01:00
|
|
|
videoPath := v.getFilename()
|
|
|
|
|
2018-06-09 01:14:55 +02:00
|
|
|
err := os.Mkdir(v.dir+"/"+v.id, 0750)
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "file exists") {
|
|
|
|
return errors.Wrap(err, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(videoPath)
|
2017-12-28 18:14:33 +01:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
log.Debugln(v.id + " already exists at " + videoPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 22:48:01 +01:00
|
|
|
videoUrl := "https://www.youtube.com/watch?v=" + v.id
|
|
|
|
videoInfo, err := ytdl.GetVideoInfo(videoUrl)
|
2017-12-28 18:14:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-13 22:48:01 +01:00
|
|
|
|
|
|
|
var downloadedFile *os.File
|
2018-06-09 01:14:55 +02:00
|
|
|
downloadedFile, err = os.Create(videoPath)
|
2017-12-28 18:14:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-13 22:48:01 +01:00
|
|
|
|
|
|
|
defer downloadedFile.Close()
|
|
|
|
|
|
|
|
return videoInfo.Download(videoInfo.Formats.Best(ytdl.FormatAudioEncodingKey)[0], downloadedFile)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
2018-04-25 20:56:26 +02:00
|
|
|
func (v YoutubeVideo) delete() error {
|
|
|
|
videoPath := v.getFilename()
|
|
|
|
err := os.Remove(videoPath)
|
|
|
|
if err != nil {
|
2018-06-06 23:47:28 +02:00
|
|
|
log.Debugln(errors.Prefix("delete error", err))
|
2018-04-25 20:56:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugln(v.id + " deleted from disk (" + videoPath + ")")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
func (v YoutubeVideo) triggerThumbnailSave() error {
|
2017-12-28 18:14:33 +01:00
|
|
|
client := &http.Client{Timeout: 30 * time.Second}
|
|
|
|
|
|
|
|
params, err := json.Marshal(map[string]string{"videoid": v.id})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPut, "https://jgp4g1qoud.execute-api.us-east-1.amazonaws.com/prod/thumbnail", bytes.NewBuffer(params))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var decoded struct {
|
|
|
|
error int `json:"error"`
|
|
|
|
url string `json:"url,omitempty"`
|
|
|
|
message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(contents, &decoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if decoded.error != 0 {
|
2018-03-09 17:47:38 +01:00
|
|
|
return errors.Err("error creating thumbnail: " + decoded.message)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func strPtr(s string) *string { return &s }
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
func (v YoutubeVideo) publish(daemon *jsonrpc.Client, claimAddress string, amount float64, channelName string) error {
|
2017-12-28 18:14:33 +01:00
|
|
|
options := jsonrpc.PublishOptions{
|
2018-05-26 02:43:16 +02:00
|
|
|
Title: &v.title,
|
|
|
|
Author: &v.channelTitle,
|
|
|
|
Description: strPtr(v.getAbbrevDescription() + "\nhttps://www.youtube.com/watch?v=" + v.id),
|
|
|
|
Language: strPtr("en"),
|
|
|
|
ClaimAddress: &claimAddress,
|
|
|
|
Thumbnail: strPtr("https://berk.ninja/thumbnails/" + v.id),
|
|
|
|
License: strPtr("Copyrighted (contact author)"),
|
|
|
|
ChangeAddress: &claimAddress,
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
if channelName != "" {
|
|
|
|
options.ChannelName = &channelName
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
return publishAndRetryExistingNames(daemon, v.title, v.getFilename(), amount, options)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v YoutubeVideo) Sync(daemon *jsonrpc.Client, claimAddress string, amount float64, channelName string) error {
|
|
|
|
//download and thumbnail can be done in parallel
|
2018-02-13 18:47:05 +01:00
|
|
|
err := v.download()
|
2017-12-28 18:14:33 +01:00
|
|
|
if err != nil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return errors.Prefix("download error", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
log.Debugln("Downloaded " + v.id)
|
|
|
|
|
2018-06-06 23:47:28 +02:00
|
|
|
fi, err := os.Stat(v.getFilename())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Size() > 1*1024*1024*1024 {
|
|
|
|
//delete the video and ignore the error
|
|
|
|
_ = v.delete()
|
|
|
|
return errors.Err("video is bigger than 1GB, skipping for now")
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
err = v.triggerThumbnailSave()
|
2017-12-28 18:14:33 +01:00
|
|
|
if err != nil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return errors.Prefix("thumbnail error", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
log.Debugln("Created thumbnail for " + v.id)
|
|
|
|
|
2018-02-13 18:47:05 +01:00
|
|
|
err = v.publish(daemon, claimAddress, amount, channelName)
|
2018-06-06 23:47:28 +02:00
|
|
|
//delete the video in all cases (and ignore the error)
|
|
|
|
_ = v.delete()
|
2018-05-05 13:22:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Prefix("publish error", err)
|
|
|
|
}
|
2018-04-25 20:56:26 +02:00
|
|
|
|
2017-12-28 18:14:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sorting videos
|
|
|
|
//type ByPublishedAt []YoutubeVideo
|
|
|
|
//
|
|
|
|
//func (a ByPublishedAt) Len() int { return len(a) }
|
|
|
|
//func (a ByPublishedAt) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
//func (a ByPublishedAt) Less(i, j int) bool { return a[i].publishedAt.Before(a[j].publishedAt) }
|
|
|
|
//
|
|
|
|
//type ByPlaylistPosition []YoutubeVideo
|
|
|
|
//
|
|
|
|
//func (a ByPlaylistPosition) Len() int { return len(a) }
|
|
|
|
//func (a ByPlaylistPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
//func (a ByPlaylistPosition) Less(i, j int) bool { return a[i].playlistPosition < a[j].playlistPosition }
|