Exemple #1
0
        /// <summary>
        /// Executes the linked in position search for the user
        /// </summary>
        /// <param name="procParams"></param>
        /// <returns></returns>
        public PositionSearchResultsViewModel Execute(LinkedInPositionSearchParams procParams)
        {
            const int resultsPageSize = 10;

            // Get the user's access token
            var user = _context.Users
                       .Where(x => x.Id == procParams.RequestingUserId)
                       .Include(x => x.LinkedInOAuthData)
                       .SingleOrDefault();

            if (user == null)
            {
                throw new MJLEntityNotFoundException(typeof(User), procParams.RequestingUserId);
            }

            if (!_verifyLiTokenProcess.Execute(new VerifyUserLinkedInAccessTokenParams {
                UserId = user.Id
            }).AccessTokenValid)
            {
                throw new UserHasNoValidOAuthAccessTokenException(user.Id, TokenProvider.LinkedIn);
            }

            // Form the API Url based on the criteria
            string apiUrl = string.Format("http://api.linkedin.com/v1/job-search?country-code={0}&keywords={1}&start={2}&count={3}",
                                          HttpUtility.UrlEncode(procParams.CountryCode),
                                          HttpUtility.UrlEncode(procParams.Keywords),
                                          procParams.ResultsPageNum * resultsPageSize,
                                          resultsPageSize);

            if (!string.IsNullOrWhiteSpace(procParams.ZipCode))
            {
                apiUrl += "&postal-code=" + procParams.ZipCode;
            }

            // Perform the search
            var consumer = new WebConsumer(LinkedInOAuthProcesses.GetLiDescription(), new LinkedInTokenManager(_context, procParams.RequestingUserId));
            var endpoint = new MessageReceivingEndpoint(apiUrl, HttpDeliveryMethods.GetRequest);
            var request  = consumer.PrepareAuthorizedRequest(endpoint, user.LinkedInOAuthData.Token);
            var response = request.GetResponse();

            // Get the results from the respones
            var xmlResponse = XDocument.Load(response.GetResponseStream());
            var resultsVm   = new PositionSearchResultsViewModel
            {
                PageNum    = procParams.ResultsPageNum,
                DataSource = ExternalDataSource.LinkedIn,
                PageSize   = resultsPageSize
            };

            resultsVm.Results = (from job in xmlResponse.Descendants("job")
                                 select new PositionSearchResultsViewModel.PositionSearchResultViewModel
            {
                JobId = job.Element("id").Value,
                Company = job.Element("company").Element("name").Value,
                Location = job.Element("location-description").Value,
                Description = job.Element("description-snippet").Value
            }).ToList();

            var searchStats = xmlResponse.Descendants("jobs").First();

            resultsVm.TotalCount = Convert.ToInt32(searchStats.Attribute("total").Value);

            if (searchStats.Attribute("start") != null)
            {
                resultsVm.PageNum = Convert.ToInt32(searchStats.Attribute("start").Value) / resultsPageSize;
            }
            else
            {
                resultsVm.PageNum = 0;
            }

            //resultsVm.Results = xmlResponse.wh
            return(resultsVm);
        }
Exemple #2
0
        protected ExternalPositionDetailsViewModel GetPositionDetails(string positionId, string userOAuthToken, int userId)
        {
            // Form the API Url based on the criteria
            string apiUrl =
                string.Format(
                    "http://api.linkedin.com/v1/jobs/{0}:(id,company:(id,name),position:({1}),description,posting-date,active,job-poster:(id,first-name,last-name,headline))",
                    positionId,
                    "title,location,job-functions,industries,job-type,experience-level");

            // Perform the search
            var consumer = new WebConsumer(LinkedInOAuthProcesses.GetLiDescription(), new LinkedInTokenManager(_context, userId));
            var endpoint = new MessageReceivingEndpoint(apiUrl, HttpDeliveryMethods.GetRequest);
            var request  = consumer.PrepareAuthorizedRequest(endpoint, userOAuthToken);

            WebResponse response;

            try { response = request.GetResponse(); }
            catch (WebException ex)
            {
                // If the response is a bad request, that means no id exists for the specified job id
                if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.BadRequest)
                {
                    return(null);
                }

                throw;
            }

            // Get the results
            var result = new ExternalPositionDetailsViewModel {
                DataSource = ExternalDataSource.LinkedIn
            };
            var xmlResponse = XDocument.Load(response.GetResponseStream());
            var job         = xmlResponse.Descendants("job").First();
            var position    = job.Element("position");

            result.Id = job.Element("id").Value;
            //result.CompanyId = job.Element("company").Element("id").Value;
            result.CompanyName     = job.Element("company").Element("name").Value;
            result.Description     = job.Element("description").Value;
            result.ExperienceLevel = position.Element("experience-level").Element("name").Value;
            result.JobType         = position.Element("job-type").Element("name").Value;
            result.Location        = position.Element("location").Element("name").Value;
            result.Title           = position.Element("title").Value;
            result.IsActive        = Convert.ToBoolean(job.Element("active").Value);

            result.Industries = position.Descendants("industries")
                                .Select(x => x.Element("industry").Element("name").Value)
                                .Aggregate((cur, next) => cur + ", " + next);

            result.JobFunctions = position.Descendants("job-functions")
                                  .Select(x => x.Element("job-function").Element("name").Value)
                                  .Aggregate((current, next) => current + ", " + next);

            var postingDate = job.Element("posting-date");
            int year        = Convert.ToInt32(postingDate.Element("year").Value);
            int month       = Convert.ToInt32(postingDate.Element("month").Value);
            int day         = Convert.ToInt32(postingDate.Element("day").Value);

            result.PostedDate = new DateTime(year, month, day);

            var poster = job.Element("job-poster");

            result.JobPosterId       = poster.Element("id").Value;
            result.JobPosterName     = string.Format("{0} {1}", poster.Element("first-name").Value, poster.Element("last-name").Value);
            result.JobPosterHeadline = poster.Element("headline").Value;

            return(result);
        }