Browse Source

Add EnvVar for SQLite database file path; Add WebProxy support for fetch feed

HOME 3 years ago
parent
commit
73b621a70f

+ 3 - 1
BanTur.Core/BanTurProgram.cs

@@ -8,8 +8,10 @@ using System.Threading.Tasks;
 
 namespace BanTur.Core
 {
-    internal static class BanTurProgram
+    public static class BanTurProgram
     {
+        public static readonly string EnvVarDbPath = "BanTurDbPath";
+
         internal const string CmdComplete = "complete";
 
         private static void PrintLine(string line)

+ 1 - 1
BanTur.Core/EntityFramework/BanTurContext.cs

@@ -12,7 +12,7 @@ namespace BanTur.Core.EntityFramework
 
         protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         {
-            optionsBuilder.UseSqlite("Data Source=" + DbAccess.DbFileName);
+            optionsBuilder.UseSqlite("Data Source=" + PathHelper.DbFilePath);
         }
     }
 }

+ 2 - 11
BanTur.Core/Utility/DbAccess.cs

@@ -10,19 +10,9 @@ namespace BanTur.Core.Utility
 {
     public static class DbAccess
     {
-        internal const string DbFileName = "BanTur.db3";
-
         static DbAccess()
         {
-            var exeDir = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
-#if DEBUG
-            if (exeDir?.ToLower().Contains("testrunner") == true)
-            {
-                exeDir = AppDomain.CurrentDomain.BaseDirectory;
-            }
-#endif
-            var dbPath = Path.Combine(exeDir, DbFileName);
-            if (File.Exists(dbPath) == false)
+            if (File.Exists(PathHelper.DbFilePath) == false)
             {
                 using var ctx = GetContext();
                 ctx.Database.EnsureCreated();
@@ -106,6 +96,7 @@ namespace BanTur.Core.Utility
 
             public static string WorkingDirectory => GetConfig();
             public static string DownloadDirectory => GetConfig();
+            public static string WebProxy => GetConfig();
         }
     }
 }

+ 10 - 5
BanTur.Core/Utility/HttpAccess.cs

@@ -1,8 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Net;
+using System.Net;
 using System.Net.Http;
-using System.Text;
 
 namespace BanTur.Core.Utility
 {
@@ -10,7 +7,15 @@ namespace BanTur.Core.Utility
     {
         public static string Get(string url)
         {
-            using var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All });
+            var clientHandler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All };
+
+            var webProxy = DbAccess.Config.WebProxy;
+            if (!string.IsNullOrWhiteSpace(webProxy))
+            {
+                clientHandler.Proxy = new WebProxy(webProxy);
+            }
+
+            using var client = new HttpClient(clientHandler);
             var content = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
             return content;
         }

+ 27 - 0
BanTur.Core/Utility/PathHelper.cs

@@ -0,0 +1,27 @@
+using System;
+using System.IO;
+
+namespace BanTur.Core.Utility
+{
+    internal static class PathHelper
+    {
+        internal static string DbFilePath
+        {
+            get
+            {
+                var env = Environment.GetEnvironmentVariable(BanTurProgram.EnvVarDbPath);
+                if (!string.IsNullOrWhiteSpace(env))
+                {
+                    if (Directory.Exists(Path.GetDirectoryName(env))) return env;
+                }
+
+                var exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
+
+                var exeDir = Path.GetDirectoryName(exePath);
+                var fileName = Path.ChangeExtension(Path.GetFileName(exePath), "db3");
+
+                return Path.Combine(exeDir, fileName);
+            }
+        }
+    }
+}

+ 15 - 0
BanTur.Tests/Basic/TestBase.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using BanTur.Core;
+
+namespace BanTur.Tests.Basic
+{
+    public class TestBase
+    {
+        public TestBase()
+        {
+            Environment.SetEnvironmentVariable(BanTurProgram.EnvVarDbPath, "z:/BanTur-test.db3");
+        }
+    }
+}

+ 2 - 1
BanTur.Tests/DbAccessTests.cs

@@ -1,11 +1,12 @@
 using BanTur.Core.Entity;
 using BanTur.Core.Utility;
 using System;
+using BanTur.Tests.Basic;
 using Xunit;
 
 namespace BanTur.Tests
 {
-    public class DbAccessTests
+    public class DbAccessTests: TestBase
     {
         [Fact]
         public void EntryCreationTest()

+ 2 - 1
BanTur.Tests/MailingTests.cs

@@ -1,9 +1,10 @@
 using BanTur.Core.Utility;
+using BanTur.Tests.Basic;
 using Xunit;
 
 namespace BanTur.Tests
 {
-    public class MailingTests
+    public class MailingTests : TestBase
     {
         [Fact]
         public void SendMailTest()

+ 10 - 2
BanTur.Tests/RssTests.cs

@@ -1,17 +1,25 @@
 using System;
 using BanTur.Core.Rss;
 using BanTur.Core.Utility;
+using BanTur.Tests.Basic;
 using Xunit;
 
 namespace BanTur.Tests
 {
-    public class RssTests
+    public class RssTests : TestBase
     {
         [Fact]
-        public void DmhyTest()
+        public void DmhyMirrorTest()
         {
             var xml = HttpAccess.Get("https://dmhy.anoneko.com/topics/rss/rss.xml");
             var entries = RssParser.Parse(xml);
         }
+
+        [Fact]
+        public void DmhyTest()
+        {
+            var xml = HttpAccess.Get("http://dmhy.org/topics/rss/rss.xml");
+            var entries = RssParser.Parse(xml);
+        }
     }
 }

+ 27 - 15
README.md

@@ -4,25 +4,37 @@ Auto download Anime Bangumi by Aria2c from magnet RSS
 
 # Usage
 
-0) Install `Aria2` and setup path env var.
+## Prerequest
 
-1) Create database: Run wighout any command-line argument to create SQLite database
+ Install `Aria2` and setup path env var.
 
-2) Config: Use SQLite management tool to edit Config and add entry to FeedSource
+## Create database
 
-|Key|ExampleValue|
-|---|---|
-|EnableMail|True|
-|SendMailHost|mail.example.com|
-|SendMailPort|25|Int32|
-|SendMailUser|mailbot|
-|SendMailPass|passofbot|
-|SendMailTarget|john.d@example.com|
-|SendMailTargetName|john doe|
-|WorkingDirectory|C:\BTWD|
-|DownloadDirectory|C:\BTDL|
+1) Setup SQLite database file path by
 
-3) Run:
+  - Env var `BanTurDbPath` or
+  - Neighboring executable  with `.db3` extension name
+ 
+2) Run without any command-line argument to create SQLite database
+
+3) Config
+
+Use SQLite management tool to edit Config and add entry to FeedSource
+
+|Key                |ExampleValue            |Required
+|---                |---                     |---
+|EnableMail         |True                    | ✔
+|SendMailHost       |mail.example.com        | ⭕ EnableMail
+|SendMailPort       |25                      | ⭕ EnableMail
+|SendMailUser       |mailbot                 | ⭕ EnableMail
+|SendMailPass       |passofbot               | ⭕ EnableMail
+|SendMailTarget     |john.d@example.com      | ⭕ EnableMail
+|SendMailTargetName |john doe                | ⭕ EnableMail
+|WorkingDirectory   |C:\BTWD                 | ✔
+|DownloadDirectory  |C:\BTDL                 | ✔
+|WebProxy           |http://example.com:8080 | ✖
+
+## Run:
 
 - Empty command-line argument or `fetch` for external scheduler
 - Pass `daemon 30` for 30 min interval loop running