Пример #1
0
 public static List <Result> NoSearchQuery(WikiTypeConfig config)
 {
     return(new List <Result>
     {
         new Result
         {
             Title = $"Search the {config.WikiName}",
             IcoPath = config.IcoPath
         }
     });
 }
Пример #2
0
 public static List <Result> Error(string error, string description, WikiTypeConfig config)
 {
     return(new List <Result>
     {
         new Result
         {
             Title = error,
             SubTitle = description,
             IcoPath = config.IcoPath,
             Action = a =>
             {
                 return false;
             }
         }
     });
 }
Пример #3
0
 public static List <Result> WithSearchResults(List <MwSearchResult> results, WikiTypeConfig config, PluginInitContext context)
 {
     return(results.Select(x => new Result
     {
         Title = x.Title,
         SubTitle = CleanExtract(x.Extract),
         IcoPath = config.IcoPath,
         //IcoPath = MwThumbnails.GetIcoPath(x, config, context), // Removed image thumbnail functionality, not stable
         Action = a =>
         {
             // Open the URL in your default browser via some Windows magic
             Process.Start(x.CanonicalUrl);
             return true;
         }
     }).ToList());
 }
        public static string GetIcoPath(MwSearchResult result, WikiTypeConfig config, PluginInitContext context)
        {
            var dir       = context?.CurrentPluginMetadata?.PluginDirectory; // Where cached images will be stored
            var filename  = result.PageImage;                                // The filename of the cached image
            var sourceUrl = result.Thumbnail?.Source;                        // Where to download the image from the wiki

            if (string.IsNullOrEmpty(dir) || string.IsNullOrEmpty(filename) || string.IsNullOrEmpty(sourceUrl))
            {
                // If any required data is unavailable, use the default image for this wiki
                // This is expected, as not all pages will have thumbnails
                return(config.IcoPath);
            }

            var cacheDir = Path.Combine(dir, config.ImageCacheFolder);

            if (!Directory.Exists(cacheDir))
            {
                Directory.CreateDirectory(cacheDir);
            }

            var cachedImage = Path.Combine(cacheDir, filename);

            if (File.Exists(cachedImage))
            {
                // If the image is cached, check when it was cached
                var lastUpdate = File.GetLastWriteTimeUtc(cachedImage);
                if (DateTime.UtcNow.Subtract(lastUpdate) <= CacheTimeout)
                {
                    // If the cached image is recent enough, return this path
                    return(cachedImage);
                }
            }

            // If the cached image is too old or does not exist, download and save it to the cache folder
            Task.Run(() => DownloadAndSaveThumbnail(cachedImage, sourceUrl));

            // The download will happen in the background. Use the default image in the meantime
            return(config.IcoPath);
        }
        public List <Result> Query(Query query)
        {
            var keyword = query.ActionKeyword.ToLowerInvariant();
            var search  = query.Terms.Length > 1 ? string.Join(" ", query.Terms.Skip(1)) : string.Empty;

            // Use OSRS config if specified, fall back on RS config otherwise.
            WikiTypeConfig config = keyword == Keywords.Osw ? WikiTypeConfig.Osrs : WikiTypeConfig.Rs;

            if (string.IsNullOrEmpty(search))
            {
                return(WoxResults.NoSearchQuery(config));
            }

            List <MwSearchResult> mwSearchResults;

            try
            {
                mwSearchResults = MwApi.QuerySearchAsync(search, config).Result;
            }
            catch (Exception e)
            {
                return(WoxResults.Error("Network Error", e.Message, config));
            }

            List <Result> results;

            if (!mwSearchResults.Any())
            {
                results = WoxResults.NoSearchResults(search, config);
            }
            else
            {
                results = WoxResults.WithSearchResults(mwSearchResults, config, this._context);
            }

            return(results);
        }
Пример #6
0
 public static List <Result> NoSearchResults(string search, WikiTypeConfig config)
 {
     return(new List <Result>
     {
         new Result
         {
             Title = "No results",
             SubTitle = $"No results found for search term: '{search}'",
             IcoPath = config.IcoPath
         },
         new Result
         {
             Title = "Create page",
             SubTitle = $"Create a new page for '{search}'",
             IcoPath = config.IcoPath,
             Action = a =>
             {
                 var url = $"{config.BaseUrl}/w/{HttpUtility.UrlEncode(search)}?action=edit";
                 Process.Start(url);
                 return true;
             }
         }
     });
 }
Пример #7
0
        public static async Task <List <MwSearchResult> > QuerySearchAsync(string search, WikiTypeConfig config)
        {
            var url = $"{config.BaseUrl}/api.php?action=query&format=json" +
                      "&generator=search" +
                      $"&gsrsearch={HttpUtility.UrlEncode(search)}" +
                      "&gsrlimit=6" +                                                              // Limit the number of pages returned by the query
                      "&prop=extracts|info" +                                                      // Include these fields in each search result, add |pageimages if thumbnails are restored
                      "&redirects=1" +                                                             // Do not return redirect pages; instead, return the pages that are redirected-to
                      "&exsentences=2&exlimit=max&exintro=1&explaintext=1&exsectionformat=plain" + // Describe how extracts should be returned
                      "&inprop=url";                                                               // Within info, include URLs that point to the page for each search result

            var response = await Client.GetAsync(url);

            var content = await response.Content.ReadAsStringAsync();

            try
            {
                var queryResult = JsonConvert.DeserializeObject <MwQueryResponse>(content);

                // Sort results by search relevance
                var orderedResults = queryResult?.Query?.Pages?
                                     .Select(x => x.Value)
                                     .OrderBy(x => x.Index)
                                     .ToList();

                return(orderedResults ?? new List <MwSearchResult>(0));
            }
            catch (Exception e)
            {
                // If anything messes up on the transform, just return no results
                return(new List <MwSearchResult>(0));
            }
        }