84 lines
2.1 KiB
Go
Raw Normal View History

package scraper
import (
"net/url"
2022-03-13 17:09:43 -07:00
"path"
"sort"
)
type VideoID int64
// TODO video-source-user: extract source user information (e.g., someone shares a video
// from someone else).
2021-08-01 15:52:04 -07:00
type Video struct {
ID VideoID `db:"id"`
TweetID TweetID `db:"tweet_id"`
Width int `db:"width"`
Height int `db:"height"`
RemoteURL string `db:"remote_url"`
LocalFilename string `db:"local_filename"`
ThumbnailRemoteUrl string `db:"thumbnail_remote_url"`
2022-03-13 17:09:43 -07:00
ThumbnailLocalPath string `db:"thumbnail_local_filename"`
Duration int `db:"duration"` // milliseconds
ViewCount int `db:"view_count"`
2021-12-23 15:12:01 -05:00
IsDownloaded bool `db:"is_downloaded"`
IsBlockedByDMCA bool `db:"is_blocked_by_dmca"`
IsGif bool `db:"is_gif"`
}
func get_filename(remote_url string) string {
u, err := url.Parse(remote_url)
if err != nil {
panic(err)
}
return path.Base(u.Path)
}
func ParseAPIVideo(apiVideo APIExtendedMedia, tweet_id TweetID) Video {
2022-03-13 17:09:43 -07:00
variants := apiVideo.VideoInfo.Variants
sort.Sort(variants)
video_remote_url := variants[0].URL
2022-03-13 17:09:43 -07:00
var view_count int
2021-12-24 16:26:34 -05:00
2022-03-13 17:09:43 -07:00
r := apiVideo.Ext.MediaStats.R
2021-12-24 16:26:34 -05:00
2022-03-13 17:09:43 -07:00
switch r.(type) {
case string:
view_count = 0
case map[string]interface{}:
OK_entry, ok := r.(map[string]interface{})["ok"]
if !ok {
panic("No 'ok' value found in the R!")
}
view_count_str, ok := OK_entry.(map[string]interface{})["viewCount"]
view_count = int_or_panic(view_count_str.(string))
if !ok {
panic("No 'viewCount' value found in the OK!")
}
}
2021-12-24 16:26:34 -05:00
local_filename := get_prefixed_path(get_filename(video_remote_url))
2022-03-13 17:09:43 -07:00
return Video{
ID: VideoID(apiVideo.ID),
TweetID: tweet_id,
Width: apiVideo.OriginalInfo.Width,
Height: apiVideo.OriginalInfo.Height,
RemoteURL: video_remote_url,
2022-03-13 17:09:43 -07:00
LocalFilename: local_filename,
2021-12-23 15:12:01 -05:00
2022-03-13 17:09:43 -07:00
ThumbnailRemoteUrl: apiVideo.MediaURLHttps,
ThumbnailLocalPath: get_prefixed_path(path.Base(apiVideo.MediaURLHttps)),
2022-03-13 17:09:43 -07:00
Duration: apiVideo.VideoInfo.Duration,
ViewCount: view_count,
2021-12-23 15:12:01 -05:00
IsDownloaded: false,
IsBlockedByDMCA: false,
IsGif: apiVideo.Type == "animated_gif",
2022-03-13 17:09:43 -07:00
}
}