示例#1
0
        public static ScopusResponse ToScopusResponse(string json)
        {
            ScopusResponse        response            = JsonConvert.DeserializeObject <ScopusResponse>(json);
            List <List <string> > responseAuthorsList = new List <List <string> >();

            if (response?.entry != null)
            {
                for (int i = 0; i < response.entry.Count; ++i)
                {
                    ScopusArticle article         = response.entry[i];
                    List <string> responseAuthors = new List <string>();
                    JArray        authorsList     = article?.authorsList?.author as JArray;
                    if (authorsList != null)
                    {
                        List <ScopusArticleAuthorElement> authors = JsonConvert.DeserializeObject <List <ScopusArticleAuthorElement> >(authorsList.ToString());
                        response.entry[i].authors = authors.Select(x => x.author).ToList();
                    }
                    else if (article.authorsList != null && article.authorsList.author != null)
                    {
                        response.entry[i].authors = new List <string>
                        {
                            article.authorsList.author.ToString()
                        };
                    }
                    else
                    {
                        response.entry[i].authors = new List <string>();
                    }
                }
            }
            return(response);
        }
        public static BaseResponse GetArticlesScopus(BaseRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException($"{nameof(BaseRequest)} is null");
            }

            string doi   = string.Empty;
            string query = string.Empty;

            ScopusRequest scopusRequest = RequestHelper.ToScopusRequest(request);

            if (request.filter != null)
            {
                scopusRequest.query = new FilterService(request.filter).FilterScopusRequest(scopusRequest.query);
            }
            else
            {
                scopusRequest.query = $"all( {scopusRequest.query} )";
            }

            scopusRequest.query = scopusRequest.query
                                  .Replace("(", "%28")
                                  .Replace(")", "%29");

            BaseResponse         articlesResponse = new BaseResponse();
            List <ScopusArticle> data             = new List <ScopusArticle>();

            int    previousDataCount;
            int    currentDataCount;
            string responseJson = string.Empty;

            do
            {
                string queryJson = $"&start={(scopusRequest.start - 1) * scopusRequest.count}&count={scopusRequest.count}&query={scopusRequest.query}";
                responseJson = ApiService.ExecuteScopusSearchApiRequest(queryJson);

                if (responseJson != string.Empty)
                {
                    ScopusResponse scopusResponse = ResponseHelper.ToScopusResponse(responseJson);

                    previousDataCount = data.Count;
                    currentDataCount  = scopusResponse?.entry?.Count(x => string.IsNullOrEmpty(x.error)) ?? 0;
                    if (scopusResponse != null)
                    {
                        scopusResponse.entry = scopusResponse.entry?.Where(x => !string.IsNullOrEmpty(x?.doi))?.ToList();
                    }

                    data.AddRange(scopusResponse?.entry ?? new List <ScopusArticle>());
                    data = data?.GroupBy(x => x.doi)?.Select(x => x.First())?.ToList() ?? new List <ScopusArticle>();

                    if (request.limit.Value <= data.Count || currentDataCount == 0)
                    {
                        scopusResponse.entry = currentDataCount == 0 ? data : data.GetRange(0, request.limit.Value);

                        for (int i = 0; i < scopusResponse.entry.Count; i++)
                        {
                            ScopusArticle article = scopusResponse.entry[i];
                            scopusResponse.entry[i].downloadUrl = ApiService.BuildScopusDownloadRequestUrl(article.doi);
                        }

                        articlesResponse = ResponseHelper.ToBaseResponse(scopusResponse);
                        break;
                    }
                    else
                    {
                        scopusRequest.start++;
                    }
                }
            }while (responseJson != string.Empty);

            articlesResponse.request = request;
            return(articlesResponse);
        }