Exemplo n.º 1
0
        public async Task <Response.SearchResult> Search(string name, int count, CancellationToken cancellationToken)
        {
            await _locker.WaitAsync(cancellationToken);

            // Change UserAgent for every search section.
            _userAgent = UserAgents[_random.Next(UserAgents.Length)];
            ResetCounter();

            try
            {
                _logger.LogInformation($"Start to Search by name: {name}, count: {count}");

                await Task.Delay(_random.Next(4000, 10000), cancellationToken);

                const string path = "/api/v2/search/movie";
                Dictionary <string, string> queryParams = new Dictionary <string, string>
                {
                    { "q", name },
                    { "count", count.ToString() }
                };
                var contentStream = await GetResponse(path, queryParams, cancellationToken);

                Response.SearchResult result = await _jsonSerializer.DeserializeFromStreamAsync <Response.SearchResult>(contentStream);

                _logger.LogTrace($"Finish doing Search by name: {name}, count: {count}");

                return(result);
            }
            finally
            {
                _locker.Release();
            }
        }
        public async Task <Response.SearchResult> Search(string name, int count, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Start to Search by name: {name}, count: {count}");

            const string path = "/api/v2/search";
            Dictionary <string, string> queryParams = new Dictionary <string, string>
            {
                { "q", name },
                { "count", count.ToString() }
            };
            var contentStream = await GetResponse(path, queryParams, cancellationToken);

            Response.SearchResult result = await _jsonSerializer.DeserializeFromStreamAsync <Response.SearchResult>(contentStream);

            _logger.LogTrace($"Finish doing Search by name: {name}, count: {count}");
            return(result);
        }
Exemplo n.º 3
0
        public async Task <List <Response.SearchTarget> > SearchFrodoByName(string name, string type,
                                                                            CancellationToken cancellationToken)
        {
            _logger.LogInformation($"[DOUBAN FRODO INFO] Searching for sid of {type} named \"{name}\"");

            var searchResults = new List <Response.SearchTarget>();

            if (string.IsNullOrWhiteSpace(name))
            {
                _logger.LogWarning($"[DOUBAN FRODO WARN] Search name is empty.");
                return(searchResults);
            }

            name = string.Join(" ", name.Split("."));

            SearchCache searchCache = SearchCache.Instance;
            string      searchId    = $"{name}-{type}";

            if (searchCache.Has(searchId))
            {
                _logger.LogInformation($"[DOUBAN FRODO INFO] Found search cache.");
                return(searchCache.searchResult);
            }

            Dictionary <string, string> queryParams = new Dictionary <string, string>();

            queryParams.Add("q", name);
            queryParams.Add("count", $"{FrodoUtils.MaxSearchCount}");

            try
            {
                var response = await _doubanAccessor.RequestFrodo(FrodoUtils.SearchApi, queryParams,
                                                                  cancellationToken);

                Response.SearchResult result = _jsonSerializer.DeserializeFromString <Response.SearchResult>(response);
                if (result.Total > 0)
                {
                    foreach (Response.SearchSubject subject in result.Items)
                    {
                        if (subject.Target_Type == type)
                        {
                            searchResults.Add(subject.Target);
                        }
                    }
                    if (searchResults.Count == 0)
                    {
                        _logger.LogWarning($"[DOUBAN FRODO WARN] Seems like \"{name}\" genre is not {type}.");
                    }
                }
                else
                {
                    _logger.LogError($"[DOUBAN FRODO ERR] No results found for \"{name}\".");
                }
            }
            catch (HttpException e)
            {
                _logger.LogError($"[DOUBAN FRODO ERR] Search \"{name}\" error, got {e.StatusCode}.");
                throw e;
            }

            searchCache.SetSearchCache(searchId, searchResults);

            return(searchResults);
        }