From 0347d05127245c4aa878f05ca40341935c23e1e0 Mon Sep 17 00:00:00 2001
From: k4yt3x <k4yt3x@k4yt3x.com>
Date: Fri, 16 Aug 2019 00:36:45 -0400
Subject: [PATCH] setup script 1.5.0: added support for Anime4K

---
 bin/video2x_setup.py | 41 ++++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/bin/video2x_setup.py b/bin/video2x_setup.py
index 63ab0ca..899be43 100644
--- a/bin/video2x_setup.py
+++ b/bin/video2x_setup.py
@@ -5,7 +5,7 @@ Name: Video2X Setup Script
 Author: K4YT3X
 Author: BrianPetkovsek
 Date Created: November 28, 2018
-Last Modified: July 30, 2019
+Last Modified: August 16, 2019
 
 Dev: SAT3LL
 
@@ -22,6 +22,7 @@ Installation Details:
 - waifu2x-caffe: %LOCALAPPDATA%\\video2x\\waifu2x-caffe
 - waifu2x-cpp-converter: %LOCALAPPDATA%\\video2x\\waifu2x-converter-cpp
 - waifu2x_ncnn_vulkan: %LOCALAPPDATA%\\video2x\\waifu2x-ncnn-vulkan
+- anime4k: %LOCALAPPDATA%\\video2x\\anime4k
 """
 
 # built-in imports
@@ -43,10 +44,11 @@ import zipfile
 # later in the script.
 # import requests
 
-VERSION = '1.4.0'
+VERSION = '1.5.0'
 
 # global static variables
 LOCALAPPDATA = pathlib.Path(os.getenv('localappdata'))
+DRIVER_OPTIONS = ['all', 'waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan', 'anime4k']
 
 
 def process_arguments():
@@ -56,7 +58,7 @@ def process_arguments():
 
     # video options
     general_options = parser.add_argument_group('General Options')
-    general_options.add_argument('-d', '--driver', help='driver to download and configure', action='store', choices=['all', 'waifu2x_caffe', 'waifu2x_converter', 'waifu2x_ncnn_vulkan'], default='all')
+    general_options.add_argument('-d', '--driver', help='driver to download and configure', action='store', choices=DRIVER_OPTIONS, default='all')
 
     # parse arguments
     return parser.parse_args()
@@ -92,6 +94,8 @@ class Video2xSetup:
             self._install_waifu2x_converter_cpp()
         elif self.driver == 'waifu2x_ncnn_vulkan':
             self._install_waifu2x_ncnn_vulkan()
+        elif self.driver == 'anime4k':
+            self._install_anime4k()
 
         print('\nGenerating Video2X configuration file')
         self._generate_config()
@@ -137,7 +141,7 @@ class Video2xSetup:
         import requests
 
         # Get latest release of waifu2x-caffe via GitHub API
-        latest_release = json.loads(requests.get('https://api.github.com/repos/lltcggie/waifu2x-caffe/releases/latest').content.decode('utf-8'))
+        latest_release = requests.get('https://api.github.com/repos/lltcggie/waifu2x-caffe/releases/latest').json()
 
         for a in latest_release['assets']:
             if 'waifu2x-caffe.zip' in a['browser_download_url']:
@@ -154,7 +158,7 @@ class Video2xSetup:
         import requests
 
         # Get latest release of waifu2x-caffe via GitHub API
-        latest_release = json.loads(requests.get('https://api.github.com/repos/DeadSix27/waifu2x-converter-cpp/releases/latest').content.decode('utf-8'))
+        latest_release = requests.get('https://api.github.com/repos/DeadSix27/waifu2x-converter-cpp/releases/latest').json()
 
         for a in latest_release['assets']:
             if re.search(r'waifu2x-DeadSix27-win64_v[0-9]*\.zip', a['browser_download_url']):
@@ -171,7 +175,7 @@ class Video2xSetup:
         import requests
 
         # Get latest release of waifu2x-ncnn-vulkan via Github API
-        latest_release = json.loads(requests.get('https://api.github.com/repos/nihui/waifu2x-ncnn-vulkan/releases/latest').content.decode('utf-8'))
+        latest_release = requests.get('https://api.github.com/repos/nihui/waifu2x-ncnn-vulkan/releases/latest').json()
 
         for a in latest_release['assets']:
             if re.search(r'waifu2x-ncnn-vulkan-\d*\.zip', a['browser_download_url']):
@@ -190,6 +194,26 @@ class Video2xSetup:
             # rename the newly extracted directory
             (LOCALAPPDATA / 'video2x' / zipf.namelist()[0]).rename(waifu2x_ncnn_vulkan_directory)
 
+    def _install_anime4k(self):
+        """ Install Anime4K
+        """
+        print('\nInstalling Anime4K')
+        import requests
+
+        # get latest release of Anime4K via Github API
+        # at the time of writing this portion, Anime4K doesn't yet have a stable release
+        # therefore releases/latest won't work
+        latest_release = requests.get('https://api.github.com/repos/bloc97/Anime4K/releases').json()[0]
+
+        for a in latest_release['assets']:
+            if 'Anime4K_Java.zip' in a['browser_download_url']:
+                anime4k_zip = download(a['browser_download_url'], tempfile.gettempdir())
+                self.trash.append(anime4k_zip)
+
+        # extract and rename
+        with zipfile.ZipFile(anime4k_zip) as zipf:
+            zipf.extractall(LOCALAPPDATA / 'video2x' / 'anime4k')
+
     def _generate_config(self):
         """ Generate video2x config
         """
@@ -203,12 +227,15 @@ class Video2xSetup:
             template_dict['waifu2x_caffe']['waifu2x_caffe_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-caffe' / 'waifu2x-caffe-cui.exe')
             template_dict['waifu2x_converter']['waifu2x_converter_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-converter-cpp')
             template_dict['waifu2x_ncnn_vulkan']['waifu2x_ncnn_vulkan_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-ncnn-vulkan' / 'waifu2x-ncnn-vulkan.exe')
+            template_dict['anime4k']['anime4k_path'] = str(LOCALAPPDATA / 'video2x' / 'anime4k' / 'Anime4K.jar')
         elif self.driver == 'waifu2x_caffe':
             template_dict['waifu2x_caffe']['waifu2x_caffe_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-caffe' / 'waifu2x-caffe-cui.exe')
         elif self.driver == 'waifu2x_converter':
             template_dict['waifu2x_converter']['waifu2x_converter_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-converter-cpp')
         elif self.driver == 'waifu2x_ncnn_vulkan':
             template_dict['waifu2x_ncnn_vulkan']['waifu2x_ncnn_vulkan_path'] = str(LOCALAPPDATA / 'video2x' / 'waifu2x-ncnn-vulkan' / 'waifu2x-ncnn-vulkan.exe')
+        elif self.driver == 'anime4k':
+            template_dict['anime4k']['anime4k_path'] = str(LOCALAPPDATA / 'video2x' / 'anime4k' / 'Anime4K.jar')
 
         template_dict['ffmpeg']['ffmpeg_path'] = str(LOCALAPPDATA / 'video2x' / 'ffmpeg-latest-win64-static' / 'bin')
         template_dict['video2x']['video2x_cache_directory'] = None
@@ -216,7 +243,7 @@ class Video2xSetup:
 
         # Write configuration into file
         with open('video2x.json', 'w') as config:
-            json.dump(template_dict, config, indent=4)
+            json.dump(template_dict, config, indent=2)
             config.close()