Adding ability to specify `User-Agent` header

pull/214/head
James Luck 2 years ago
parent e92c762c11
commit 0e8453c202

@ -105,10 +105,13 @@
<input type="url" class="u-full-width" name="baseUrl" v-model="baseUrl"> <input type="url" class="u-full-width" name="baseUrl" v-model="baseUrl">
</label> </label>
<label for="maxDownloadConcurrency" style="display: inline-block;" > <label for="maxDownloadConcurrency" style="display: inline-block;" >
<span class="label-body">Limit the number of podcasts that can be downloaded simultaneously.</span> <span class="label-body">Limit the number of podcasts that can be downloaded simultaneously</span>
<input type="number" name="maxDownloadConcurrency" v-model.number="maxDownloadConcurrency" min="1"> <input type="number" name="maxDownloadConcurrency" v-model.number="maxDownloadConcurrency" min="1">
</label> </label>
<label for="userAgent" style="display: inline-block;" >
<span class="label-body">Specify the User-Agent header specified when downloading podcasts</span>
<input type="text" name="userAgent" v-model="userAgent">
</label>
<input type="submit" value="Save" class="button"> <input type="submit" value="Save" class="button">
</form> </form>
@ -187,6 +190,7 @@ var app = new Vue({
dontDownloadDeletedFromDisk:self.dontDownloadDeletedFromDisk, dontDownloadDeletedFromDisk:self.dontDownloadDeletedFromDisk,
baseUrl:self.baseUrl, baseUrl:self.baseUrl,
maxDownloadConcurrency:self.maxDownloadConcurrency, maxDownloadConcurrency:self.maxDownloadConcurrency,
userAgent:self.userAgent,
}) })
.then(function(response){ .then(function(response){
Vue.toasted.show('Settings saved successfully.' ,{ Vue.toasted.show('Settings saved successfully.' ,{
@ -232,6 +236,7 @@ var app = new Vue({
dontDownloadDeletedFromDisk:{{ .setting.DontDownloadDeletedFromDisk }}, dontDownloadDeletedFromDisk:{{ .setting.DontDownloadDeletedFromDisk }},
baseUrl: {{ .setting.BaseUrl }}, baseUrl: {{ .setting.BaseUrl }},
maxDownloadConcurrency:{{ .setting.MaxDownloadConcurrency }}, maxDownloadConcurrency:{{ .setting.MaxDownloadConcurrency }},
userAgent:{{ .setting.UserAgent}},
}, },
}) })

@ -32,6 +32,7 @@ type SettingModel struct {
DontDownloadDeletedFromDisk bool `form:"dontDownloadDeletedFromDisk" json:"dontDownloadDeletedFromDisk" query:"dontDownloadDeletedFromDisk"` DontDownloadDeletedFromDisk bool `form:"dontDownloadDeletedFromDisk" json:"dontDownloadDeletedFromDisk" query:"dontDownloadDeletedFromDisk"`
BaseUrl string `form:"baseUrl" json:"baseUrl" query:"baseUrl"` BaseUrl string `form:"baseUrl" json:"baseUrl" query:"baseUrl"`
MaxDownloadConcurrency int `form:"maxDownloadConcurrency" json:"maxDownloadConcurrency" query:"maxDownloadConcurrency"` MaxDownloadConcurrency int `form:"maxDownloadConcurrency" json:"maxDownloadConcurrency" query:"maxDownloadConcurrency"`
UserAgent string `form:"userAgent" json:"userAgent" query:"userAgent"`
} }
var searchOptions = map[string]string{ var searchOptions = map[string]string{

@ -629,7 +629,7 @@ func UpdateSetting(c *gin.Context) {
err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount, err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount,
model.AutoDownload, model.AppendDateToFileName, model.AppendEpisodeNumberToFileName, model.AutoDownload, model.AppendDateToFileName, model.AppendEpisodeNumberToFileName,
model.DarkMode, model.DownloadEpisodeImages, model.GenerateNFOFile, model.DontDownloadDeletedFromDisk, model.BaseUrl, model.DarkMode, model.DownloadEpisodeImages, model.GenerateNFOFile, model.DontDownloadDeletedFromDisk, model.BaseUrl,
model.MaxDownloadConcurrency, model.MaxDownloadConcurrency, model.UserAgent,
) )
if err == nil { if err == nil {
c.JSON(200, gin.H{"message": "Success"}) c.JSON(200, gin.H{"message": "Success"})

@ -88,6 +88,7 @@ type Setting struct {
DontDownloadDeletedFromDisk bool `gorm:"default:false"` DontDownloadDeletedFromDisk bool `gorm:"default:false"`
BaseUrl string BaseUrl string
MaxDownloadConcurrency int `gorm:"default:5"` MaxDownloadConcurrency int `gorm:"default:5"`
UserAgent string
} }
type Migration struct { type Migration struct {
Base Base

@ -27,7 +27,14 @@ func Download(link string, episodeTitle string, podcastName string, prefix strin
return "", errors.New("Download path empty") return "", errors.New("Download path empty")
} }
client := httpClient() client := httpClient()
resp, err := client.Get(link)
req, err := http.NewRequest("GET", link, nil)
req.Header.Add("User-Agent", "")
if err != nil {
Logger.Errorw("Error creating request: "+link, err)
}
resp, err := client.Do(req)
if err != nil { if err != nil {
Logger.Errorw("Error getting response: "+link, err) Logger.Errorw("Error getting response: "+link, err)
return "", err return "", err
@ -102,7 +109,13 @@ func DownloadPodcastCoverImage(link string, podcastName string) (string, error)
return "", errors.New("Download path empty") return "", errors.New("Download path empty")
} }
client := httpClient() client := httpClient()
resp, err := client.Get(link) req, err := getRequest(link)
if err != nil {
Logger.Errorw("Error creating request: "+link, err)
return "", err
}
resp, err := client.Do(req)
if err != nil { if err != nil {
Logger.Errorw("Error getting response: "+link, err) Logger.Errorw("Error getting response: "+link, err)
return "", err return "", err
@ -139,7 +152,13 @@ func DownloadImage(link string, episodeId string, podcastName string) (string, e
return "", errors.New("Download path empty") return "", errors.New("Download path empty")
} }
client := httpClient() client := httpClient()
resp, err := client.Get(link) req, err := getRequest(link)
if err != nil {
Logger.Errorw("Error creating request: "+link, err)
return "", err
}
resp, err := client.Do(req)
if err != nil { if err != nil {
Logger.Errorw("Error getting response: "+link, err) Logger.Errorw("Error getting response: "+link, err)
return "", err return "", err
@ -326,6 +345,20 @@ func httpClient() *http.Client {
return &client return &client
} }
func getRequest(url string) (*http.Request, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
setting := db.GetOrCreateSetting()
if len(setting.UserAgent) > 0 {
req.Header.Add("User-Agent", setting.UserAgent)
}
return req, nil
}
func createFolder(folder string, parent string) string { func createFolder(folder string, parent string) string {
folder = cleanFileName(folder) folder = cleanFileName(folder)
//str := stringy.New(folder) //str := stringy.New(folder)

@ -764,7 +764,7 @@ func GetSearchFromPodcastIndex(pod *podcastindex.Podcast) *model.CommonSearchRes
func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool, func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool,
appendDateToFileName bool, appendEpisodeNumberToFileName bool, darkMode bool, downloadEpisodeImages bool, appendDateToFileName bool, appendEpisodeNumberToFileName bool, darkMode bool, downloadEpisodeImages bool,
generateNFOFile bool, dontDownloadDeletedFromDisk bool, baseUrl string, maxDownloadConcurrency int) error { generateNFOFile bool, dontDownloadDeletedFromDisk bool, baseUrl string, maxDownloadConcurrency int, userAgent string) error {
setting := db.GetOrCreateSetting() setting := db.GetOrCreateSetting()
setting.AutoDownload = autoDownload setting.AutoDownload = autoDownload
@ -778,6 +778,7 @@ func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload b
setting.DontDownloadDeletedFromDisk = dontDownloadDeletedFromDisk setting.DontDownloadDeletedFromDisk = dontDownloadDeletedFromDisk
setting.BaseUrl = baseUrl setting.BaseUrl = baseUrl
setting.MaxDownloadConcurrency = maxDownloadConcurrency setting.MaxDownloadConcurrency = maxDownloadConcurrency
setting.UserAgent = userAgent
return db.UpdateSettings(setting) return db.UpdateSettings(setting)
} }

Loading…
Cancel
Save