fix time parsing for real

This commit is contained in:
Niko Storni 2020-08-06 02:12:05 +02:00
parent 775e4881cb
commit f0280b51b4
5 changed files with 36 additions and 16 deletions

View file

@ -73,7 +73,11 @@ GetTime:
} }
} else if !errors.Is(err, errNotScraped) && !errors.Is(err, errUploadTimeEmpty) { } else if !errors.Is(err, errNotScraped) && !errors.Is(err, errUploadTimeEmpty) {
//slack(":warning: Error while trying to get accurate upload time for %s: %v", videoID, err) //slack(":warning: Error while trying to get accurate upload time for %s: %v", videoID, err)
return nil, errors.Err(err) if t == "" {
return nil, errors.Err(err)
} else {
t = "" //TODO: get rid of the other piece below?
}
} }
// do fallback below // do fallback below
} }
@ -86,7 +90,7 @@ GetTime:
} }
//slack(":exclamation: Got an accurate time for %s", videoID) //slack(":exclamation: Got an accurate time for %s", videoID)
video.UploadDateForReal = parsed video.UploadDateForReal = parsed
} else { } else { //TODO: this is the piece that isn't needed!
slack(":warning: Could not get accurate time for %s. Falling back to time from upload ytdl: %s.", videoID, video.UploadDate) slack(":warning: Could not get accurate time for %s. Falling back to time from upload ytdl: %s.", videoID, video.UploadDate)
// fall back to UploadDate from youtube-dl // fall back to UploadDate from youtube-dl
video.UploadDateForReal, err = time.Parse("20060102", video.UploadDate) video.UploadDateForReal, err = time.Parse("20060102", video.UploadDate)
@ -166,29 +170,31 @@ func getUploadTime(config *sdk.APIConfig, videoID string, ip *net.TCPAddr, uploa
logrus.Error(err) logrus.Error(err)
} }
if release != nil { if release != nil {
const sqlTimeFormat = "2006-01-02 15:04:05" //const sqlTimeFormat = "2006-01-02 15:04:05"
sqlTime, err := time.ParseInLocation(sqlTimeFormat, release.ReleaseTime, time.UTC) sqlTime, err := time.ParseInLocation(time.RFC3339, release.ReleaseTime, time.UTC)
if err == nil { if err == nil {
return sqlTime.Format(releaseTimeFormat), nil return sqlTime.Format(releaseTimeFormat), nil
} else {
logrus.Error(err)
} }
} }
ytdlUploadDate, err := time.Parse("20060102", uploadDate) ytdlUploadDate, err := time.Parse("20060102", uploadDate)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
if time.Now().AddDate(0, 0, -5).After(ytdlUploadDate) { if time.Now().AddDate(0, 0, -2).After(ytdlUploadDate) {
return ytdlUploadDate.Format(releaseTimeFormat), nil return ytdlUploadDate.Format(releaseTimeFormat), nil
} }
client := getClient(ip) client := getClient(ip)
req, err := http.NewRequest(http.MethodGet, "https://caa.iti.gr/get_verificationV3?url=https://www.youtube.com/watch?v="+videoID, nil) req, err := http.NewRequest(http.MethodGet, "https://caa.iti.gr/get_verificationV3?url=https://www.youtube.com/watch?v="+videoID, nil)
if err != nil { if err != nil {
return "", errors.Err(err) return ytdlUploadDate.Format(releaseTimeFormat), errors.Err(err)
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36")
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
return "", errors.Err(err) return ytdlUploadDate.Format(releaseTimeFormat), errors.Err(err)
} }
defer res.Body.Close() defer res.Body.Close()
@ -199,19 +205,19 @@ func getUploadTime(config *sdk.APIConfig, videoID string, ip *net.TCPAddr, uploa
} }
err = json.NewDecoder(res.Body).Decode(&uploadTime) err = json.NewDecoder(res.Body).Decode(&uploadTime)
if err != nil { if err != nil {
return "", errors.Err(err) return ytdlUploadDate.Format(releaseTimeFormat), errors.Err(err)
} }
if uploadTime.Status == "ERROR1" { if uploadTime.Status == "ERROR1" {
return "", errNotScraped return ytdlUploadDate.Format(releaseTimeFormat), errNotScraped
} }
if uploadTime.Status == "" && strings.HasPrefix(uploadTime.Message, "CANNOT_RETRIEVE_REPORT_FOR_VIDEO_") { if uploadTime.Status == "" && strings.HasPrefix(uploadTime.Message, "CANNOT_RETRIEVE_REPORT_FOR_VIDEO_") {
return "", errors.Err("cannot retrieve report for video") return ytdlUploadDate.Format(releaseTimeFormat), errors.Err("cannot retrieve report for video")
} }
if uploadTime.Time == "" { if uploadTime.Time == "" {
return "", errUploadTimeEmpty return ytdlUploadDate.Format(releaseTimeFormat), errUploadTimeEmpty
} }
return uploadTime.Time, nil return uploadTime.Time, nil

View file

@ -3,11 +3,13 @@ package downloader
import ( import (
"testing" "testing"
"github.com/lbryio/ytsync/v5/sdk"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
) )
func TestGetPlaylistVideoIDs(t *testing.T) { func TestGetPlaylistVideoIDs(t *testing.T) {
videoIDs, err := GetPlaylistVideoIDs("UCJ0-OtVpF0wOKEqT2Z1HEtA", 50) videoIDs, err := GetPlaylistVideoIDs("UCJ0-OtVpF0wOKEqT2Z1HEtA", 50, nil, nil)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
@ -17,7 +19,7 @@ func TestGetPlaylistVideoIDs(t *testing.T) {
} }
func TestGetVideoInformation(t *testing.T) { func TestGetVideoInformation(t *testing.T) {
video, err := GetVideoInformation("zj7pXM9gE5M", nil) video, err := GetVideoInformation(nil, "zj7pXM9gE5M", nil, nil, nil)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
@ -25,3 +27,16 @@ func TestGetVideoInformation(t *testing.T) {
logrus.Info(video.ID) logrus.Info(video.ID)
} }
} }
func Test_getUploadTime(t *testing.T) {
configs := sdk.APIConfig{
YoutubeAPIKey: "",
ApiURL: "https://api.lbry.com",
ApiToken: "Ht4NETrL5oWKyAaZkuSV68BKhtXkiLh5",
HostName: "test",
}
got, err := getUploadTime(&configs, "kDGOHNpRjzc", nil, "20060102")
assert.NoError(t, err)
t.Log(got)
}

View file

@ -413,7 +413,7 @@ type VideoRelease struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
YoutubeDataID uint64 `json:"youtube_data_id"` YoutubeDataID uint64 `json:"youtube_data_id"`
VideoID string `json:"video_id"` VideoID string `json:"video_id"`
ReleaseTime string `json:"release_time""` ReleaseTime string `json:"release_time"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
} }

View file

@ -452,7 +452,6 @@ func (v *YoutubeVideo) downloadAndPublish(daemon *jsonrpc.Client, params SyncPar
dur := time.Duration(v.youtubeInfo.Duration) * time.Second dur := time.Duration(v.youtubeInfo.Duration) * time.Second
if dur > v.maxVideoLength { if dur > v.maxVideoLength {
log.Infof("%s is %d long and the limit is %s", v.id, dur.String(), v.maxVideoLength.String())
logUtils.SendErrorToSlack("%s is %s long and the limit is %s", v.id, dur.String(), v.maxVideoLength.String()) logUtils.SendErrorToSlack("%s is %s long and the limit is %s", v.id, dur.String(), v.maxVideoLength.String())
return nil, errors.Err("video is too long to process") return nil, errors.Err("video is too long to process")
} }

View file

@ -206,7 +206,7 @@ func getVideos(config *sdk.APIConfig, channelID string, videoIDs []string, stopC
Status: "failed", Status: "failed",
FailureReason: err.Error(), FailureReason: err.Error(),
}) })
util.SendErrorToSlack("Skipping video: " + err.Error()) util.SendErrorToSlack("Skipping video: " + errors.FullTrace(err))
if errSDK != nil { if errSDK != nil {
return nil, errors.Err(errSDK) return nil, errors.Err(errSDK)
} }