mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-01-13 17:22:48 -05:00
Add support for per-feed user agent
This commit is contained in:
@@ -1,22 +1,31 @@
|
|||||||
using FeedCenter.Data;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using FeedCenter.Data;
|
||||||
using FeedCenter.Options;
|
using FeedCenter.Options;
|
||||||
using Realms;
|
using Realms;
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace FeedCenter;
|
namespace FeedCenter;
|
||||||
|
|
||||||
public class FeedCenterEntities
|
public class FeedCenterEntities
|
||||||
{
|
{
|
||||||
public Realm RealmInstance { get; }
|
|
||||||
|
|
||||||
public RealmObservableCollection<Category> Categories { get; }
|
|
||||||
public RealmObservableCollection<Feed> Feeds { get; private set; }
|
|
||||||
public RealmObservableCollection<Setting> Settings { get; private set; }
|
|
||||||
|
|
||||||
public FeedCenterEntities()
|
public FeedCenterEntities()
|
||||||
{
|
{
|
||||||
var realmConfiguration = new RealmConfiguration($"{Database.DatabaseFile}");
|
var realmConfiguration = new RealmConfiguration($"{Database.DatabaseFile}")
|
||||||
|
{
|
||||||
|
SchemaVersion = 1,
|
||||||
|
MigrationCallback = (migration, oldSchemaVersion) =>
|
||||||
|
{
|
||||||
|
var newVersionFeeds = migration.NewRealm.All<Feed>();
|
||||||
|
|
||||||
|
foreach (var newVersionFeed in newVersionFeeds)
|
||||||
|
{
|
||||||
|
if (oldSchemaVersion == 0)
|
||||||
|
{
|
||||||
|
newVersionFeed.UserAgent = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
RealmInstance = Realm.GetInstance(realmConfiguration);
|
RealmInstance = Realm.GetInstance(realmConfiguration);
|
||||||
|
|
||||||
@@ -30,6 +39,17 @@ public class FeedCenterEntities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RealmObservableCollection<Category> Categories { get; }
|
||||||
|
|
||||||
|
public Category DefaultCategory
|
||||||
|
{
|
||||||
|
get { return Categories.First(c => c.IsDefault); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public RealmObservableCollection<Feed> Feeds { get; private set; }
|
||||||
|
private Realm RealmInstance { get; }
|
||||||
|
public RealmObservableCollection<Setting> Settings { get; private set; }
|
||||||
|
|
||||||
public void Refresh()
|
public void Refresh()
|
||||||
{
|
{
|
||||||
RealmInstance.Refresh();
|
RealmInstance.Refresh();
|
||||||
@@ -44,9 +64,4 @@ public class FeedCenterEntities
|
|||||||
{
|
{
|
||||||
return RealmInstance.BeginWrite();
|
return RealmInstance.BeginWrite();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Category DefaultCategory
|
|
||||||
{
|
|
||||||
get { return Categories.First(c => c.IsDefault); }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,4 @@
|
|||||||
using ChrisKaczor.ApplicationUpdate;
|
using System;
|
||||||
using FeedCenter.Data;
|
|
||||||
using FeedCenter.FeedParsers;
|
|
||||||
using FeedCenter.Properties;
|
|
||||||
using FeedCenter.Xml;
|
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Realms;
|
|
||||||
using Serilog;
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
@@ -19,6 +11,14 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using ChrisKaczor.ApplicationUpdate;
|
||||||
|
using FeedCenter.Data;
|
||||||
|
using FeedCenter.FeedParsers;
|
||||||
|
using FeedCenter.Properties;
|
||||||
|
using FeedCenter.Xml;
|
||||||
|
using JetBrains.Annotations;
|
||||||
|
using Realms;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace FeedCenter;
|
namespace FeedCenter;
|
||||||
|
|
||||||
@@ -98,9 +98,6 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[MapTo("Password")]
|
|
||||||
public string RawPassword { get; set; }
|
|
||||||
|
|
||||||
public string Password
|
public string Password
|
||||||
{
|
{
|
||||||
get => RawPassword;
|
get => RawPassword;
|
||||||
@@ -122,9 +119,15 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo
|
|||||||
[MapTo("Name")]
|
[MapTo("Name")]
|
||||||
private string RawName { get; set; } = string.Empty;
|
private string RawName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[MapTo("Password")]
|
||||||
|
public string RawPassword { get; set; }
|
||||||
|
|
||||||
[MapTo("Source")]
|
[MapTo("Source")]
|
||||||
private string RawSource { get; set; } = string.Empty;
|
private string RawSource { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
[MapTo("Username")]
|
||||||
|
public string RawUsername { get; set; }
|
||||||
|
|
||||||
public string Source
|
public string Source
|
||||||
{
|
{
|
||||||
get => RawSource;
|
get => RawSource;
|
||||||
@@ -139,8 +142,7 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo
|
|||||||
|
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
|
||||||
[MapTo("Username")]
|
public string UserAgent { get; set; }
|
||||||
public string RawUsername { get; set; }
|
|
||||||
|
|
||||||
public string Username
|
public string Username
|
||||||
{
|
{
|
||||||
@@ -242,6 +244,17 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo
|
|||||||
return new Tuple<FeedType, string>(feedType, retrieveResult.Item2);
|
return new Tuple<FeedType, string>(feedType, retrieveResult.Item2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string GetUserAgent()
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(UserAgent))
|
||||||
|
return UserAgent;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent))
|
||||||
|
return Settings.Default.DefaultUserAgent;
|
||||||
|
|
||||||
|
return "FeedCenter/" + UpdateCheck.LocalVersion;
|
||||||
|
}
|
||||||
|
|
||||||
private Tuple<FeedReadResult, string> RetrieveFeed()
|
private Tuple<FeedReadResult, string> RetrieveFeed()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -258,14 +271,15 @@ public partial class Feed : RealmObject, INotifyDataErrorInfo
|
|||||||
|
|
||||||
_httpClient = new HttpClient(clientHandler);
|
_httpClient = new HttpClient(clientHandler);
|
||||||
|
|
||||||
// Set a user agent string
|
|
||||||
var userAgent = string.IsNullOrWhiteSpace(Settings.Default.DefaultUserAgent) ? "FeedCenter/" + UpdateCheck.LocalVersion : Settings.Default.DefaultUserAgent;
|
|
||||||
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
|
|
||||||
|
|
||||||
// Set a timeout
|
// Set a timeout
|
||||||
_httpClient.Timeout = TimeSpan.FromSeconds(10);
|
_httpClient.Timeout = TimeSpan.FromSeconds(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set a user agent string
|
||||||
|
var userAgent = GetUserAgent();
|
||||||
|
_httpClient.DefaultRequestHeaders.UserAgent.Clear();
|
||||||
|
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
|
||||||
|
|
||||||
// If we need to authenticate then set the credentials
|
// If we need to authenticate then set the credentials
|
||||||
_httpClient.DefaultRequestHeaders.Authorization = Authenticate ? new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))) : null;
|
_httpClient.DefaultRequestHeaders.Authorization = Authenticate ? new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Username}:{Password}"))) : null;
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,13 @@
|
|||||||
<ComboBoxItem Content="{x:Static properties:Resources.openAllMultipleToolbarButton}"
|
<ComboBoxItem Content="{x:Static properties:Resources.openAllMultipleToolbarButton}"
|
||||||
Tag="{x:Static feedCenter:MultipleOpenAction.IndividualPages}" />
|
Tag="{x:Static feedCenter:MultipleOpenAction.IndividualPages}" />
|
||||||
</ComboBox>
|
</ComboBox>
|
||||||
|
<ComboBox Name="UserAgentComboBox"
|
||||||
|
mah:TextBoxHelper.UseFloatingWatermark="True"
|
||||||
|
mah:TextBoxHelper.Watermark="{x:Static properties:Resources.userAgentLabel}"
|
||||||
|
DisplayMemberPath="Caption"
|
||||||
|
ItemsSource="{Binding Source={x:Static options:UserAgentItem.UserAgents}}"
|
||||||
|
SelectedValuePath="UserAgent"
|
||||||
|
SelectedValue="{Binding Path=UserAgent, UpdateSourceTrigger=Explicit, ValidatesOnExceptions=true}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</TabItem>
|
</TabItem>
|
||||||
<TabItem Header="{x:Static properties:Resources.authenticationTab}">
|
<TabItem Header="{x:Static properties:Resources.authenticationTab}">
|
||||||
|
|||||||
@@ -12,11 +12,9 @@
|
|||||||
d:DesignWidth="300">
|
d:DesignWidth="300">
|
||||||
<StackPanel options:Spacing.Vertical="10">
|
<StackPanel options:Spacing.Vertical="10">
|
||||||
<CheckBox Content="{x:Static properties:Resources.startWithWindowsCheckBox}"
|
<CheckBox Content="{x:Static properties:Resources.startWithWindowsCheckBox}"
|
||||||
Name="StartWithWindowsCheckBox"
|
|
||||||
IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=StartWithWindows}"
|
IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=StartWithWindows}"
|
||||||
Click="OnSaveSettings" />
|
Click="OnSaveSettings" />
|
||||||
<ComboBox Name="BrowserComboBox"
|
<ComboBox mah:TextBoxHelper.UseFloatingWatermark="True"
|
||||||
mah:TextBoxHelper.UseFloatingWatermark="True"
|
|
||||||
mah:TextBoxHelper.Watermark="{x:Static properties:Resources.defaultBrowserLabel}"
|
mah:TextBoxHelper.Watermark="{x:Static properties:Resources.defaultBrowserLabel}"
|
||||||
d:DataContext="{d:DesignInstance Type=installedBrowsers:InstalledBrowser}"
|
d:DataContext="{d:DesignInstance Type=installedBrowsers:InstalledBrowser}"
|
||||||
DisplayMemberPath="Name"
|
DisplayMemberPath="Name"
|
||||||
@@ -24,12 +22,11 @@
|
|||||||
SelectedValuePath="Key"
|
SelectedValuePath="Key"
|
||||||
SelectedValue="{Binding Source={x:Static properties:Settings.Default}, Path=Browser}"
|
SelectedValue="{Binding Source={x:Static properties:Settings.Default}, Path=Browser}"
|
||||||
SelectionChanged="OnSaveSettings" />
|
SelectionChanged="OnSaveSettings" />
|
||||||
<ComboBox Name="UserAgentComboBox"
|
<ComboBox mah:TextBoxHelper.UseFloatingWatermark="True"
|
||||||
mah:TextBoxHelper.UseFloatingWatermark="True"
|
|
||||||
mah:TextBoxHelper.Watermark="{x:Static properties:Resources.defaultUserAgentLabel}"
|
mah:TextBoxHelper.Watermark="{x:Static properties:Resources.defaultUserAgentLabel}"
|
||||||
d:DataContext="{d:DesignInstance Type=options:UserAgentItem}"
|
d:DataContext="{d:DesignInstance Type=options:UserAgentItem}"
|
||||||
DisplayMemberPath="Caption"
|
DisplayMemberPath="Caption"
|
||||||
ItemsSource="{Binding Source={x:Static options:UserAgentItem.UserAgents}}"
|
ItemsSource="{Binding Source={x:Static options:UserAgentItem.DefaultUserAgents}}"
|
||||||
SelectedValuePath="UserAgent"
|
SelectedValuePath="UserAgent"
|
||||||
SelectedValue="{Binding Source={x:Static properties:Settings.Default}, Path=DefaultUserAgent}"
|
SelectedValue="{Binding Source={x:Static properties:Settings.Default}, Path=DefaultUserAgent}"
|
||||||
SelectionChanged="OnSaveSettings" />
|
SelectionChanged="OnSaveSettings" />
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using FeedCenter.Properties;
|
||||||
|
|
||||||
namespace FeedCenter.Options;
|
namespace FeedCenter.Options;
|
||||||
|
|
||||||
public class UserAgentItem
|
public class UserAgentItem
|
||||||
{
|
{
|
||||||
public string Caption { get; set; }
|
public string Caption { get; set; }
|
||||||
public string UserAgent { get; set; }
|
|
||||||
|
|
||||||
public static List<UserAgentItem> UserAgents => new()
|
public static List<UserAgentItem> DefaultUserAgents => new()
|
||||||
{
|
{
|
||||||
new UserAgentItem
|
new UserAgentItem
|
||||||
{
|
{
|
||||||
Caption = Properties.Resources.DefaultUserAgentCaption,
|
Caption = Properties.Resources.ApplicationUserAgentCaption,
|
||||||
UserAgent = string.Empty
|
UserAgent = string.Empty
|
||||||
},
|
},
|
||||||
new UserAgentItem
|
new UserAgentItem
|
||||||
@@ -30,4 +31,29 @@ public class UserAgentItem
|
|||||||
UserAgent = "curl/7.47.0"
|
UserAgent = "curl/7.47.0"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public string UserAgent { get; set; }
|
||||||
|
|
||||||
|
public static List<UserAgentItem> UserAgents
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var defaultUserAgents = DefaultUserAgents;
|
||||||
|
|
||||||
|
var applicationDefaultUserAgent = defaultUserAgents.First(dua => dua.UserAgent == Settings.Default.DefaultUserAgent);
|
||||||
|
|
||||||
|
var userAgents = new List<UserAgentItem>
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Caption = string.Format(Resources.DefaultUserAgentCaption, applicationDefaultUserAgent.Caption),
|
||||||
|
UserAgent = null
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
userAgents.AddRange(defaultUserAgents);
|
||||||
|
|
||||||
|
return userAgents;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
20
Application/Properties/Resources.Designer.cs
generated
20
Application/Properties/Resources.Designer.cs
generated
@@ -133,6 +133,15 @@ namespace FeedCenter.Properties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Feed Center.
|
||||||
|
/// </summary>
|
||||||
|
public static string ApplicationUserAgentCaption {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("ApplicationUserAgentCaption", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to Password.
|
/// Looks up a localized string similar to Password.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -517,7 +526,7 @@ namespace FeedCenter.Properties {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to Feed Center.
|
/// Looks up a localized string similar to Default ({0}).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string DefaultUserAgentCaption {
|
public static string DefaultUserAgentCaption {
|
||||||
get {
|
get {
|
||||||
@@ -1461,6 +1470,15 @@ namespace FeedCenter.Properties {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to User agent.
|
||||||
|
/// </summary>
|
||||||
|
public static string userAgentLabel {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("userAgentLabel", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to Version {0}.
|
/// Looks up a localized string similar to Version {0}.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -524,6 +524,9 @@
|
|||||||
<value>Category: {0}</value>
|
<value>Category: {0}</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="DefaultUserAgentCaption" xml:space="preserve">
|
<data name="DefaultUserAgentCaption" xml:space="preserve">
|
||||||
|
<value>Default ({0})</value>
|
||||||
|
</data>
|
||||||
|
<data name="ApplicationUserAgentCaption" xml:space="preserve">
|
||||||
<value>Feed Center</value>
|
<value>Feed Center</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="defaultUserAgentLabel" xml:space="preserve">
|
<data name="defaultUserAgentLabel" xml:space="preserve">
|
||||||
@@ -549,4 +552,7 @@ All feeds currently in category "{0}" will be moved to the default category.</va
|
|||||||
<data name="ConfirmDeleteFeeds" xml:space="preserve">
|
<data name="ConfirmDeleteFeeds" xml:space="preserve">
|
||||||
<value>Are you sure you want to delete the selected feeds?</value>
|
<value>Are you sure you want to delete the selected feeds?</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="userAgentLabel" xml:space="preserve">
|
||||||
|
<value>User agent</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
||||||
Reference in New Issue
Block a user