Advertisement
Guest User

Untitled

a guest
May 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.IO.Packaging;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Xml.Linq;
  12.  
  13. namespace Samples {
  14.     class Program {
  15.         const string CandidatesFilePath = "D:\\Candidates.csv";
  16.         const string employeesDataFilePath = "D:\\e3s.epam.com-getData.har";
  17.         const string notDataOutputFilePath = "D:\\NotFindedEmploees.csv";
  18.  
  19.         static string employeesDataContent;
  20.         static Dictionary<string, string> tags = new Dictionary<string, string>{
  21.             { "M&E", "M&E CV" },
  22.             { "Corporate", "Corporate CV" },
  23.             { "Bulgaria", "Bulgaria CV" },
  24.             { "Google", "Google" }
  25.         };
  26.  
  27.         static void Main(string[] args) {
  28.             ReadEmploeesData();
  29.  
  30.             var notFindedEmployees = new List<string>();
  31.             foreach (var condidate in ReadCandidates().Skip(2)) {
  32.                 var fields = condidate.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  33.                 if (fields.Length != 4) continue;
  34.  
  35.                 fields[0] = Regex.Replace(fields[0], "[^a-zA-Z]", " ");
  36.                 var upsaId = GetUpsaId(fields[0]);
  37.                 if (upsaId == null) { notFindedEmployees.Add(condidate); continue; }
  38.  
  39.                 var tag = tags[fields[2]];
  40.                 var postData = CreatePostData(upsaId, tag);
  41.                 var coockie = GetCockie();
  42.  
  43.                 MakeRequest(postData, coockie);
  44.             }
  45.  
  46.             WriteNotFinded(notFindedEmployees);
  47.         }
  48.  
  49.         static void ReadEmploeesData() {
  50.             using (var reader = new StreamReader(employeesDataFilePath)) {
  51.                 employeesDataContent = reader.ReadToEnd();
  52.             }
  53.         }
  54.  
  55.         static IEnumerable<string> ReadCandidates() {
  56.             using (var reader = new StreamReader(CandidatesFilePath)) {
  57.                 string line;
  58.                 while ((line = reader.ReadLine()) != null) {
  59.                     yield return line;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         static string GetUpsaId(string name) {
  65.             string pattern = "{\\\\\"1\\\\\":\\\\\"(?<upsaId>\\d+)\\\\\",\\\\\"2\\\\\":\\\\\"" + name + "\\\\\"";
  66.             var regex = new Regex(pattern);
  67.             var match = regex.Match(employeesDataContent);
  68.             return (string.IsNullOrEmpty(match.Value)) ? null : match.Groups["upsaId"].Value;
  69.         }
  70.  
  71.         static string GetCockie() {
  72.             string pattern = "\"name\": \"Cookie\",\\s+?\"value\": \"(?<coockie>.*?)\"";
  73.             var regex = new Regex(pattern);
  74.             var match = regex.Match(employeesDataContent);
  75.             return match.Groups["coockie"].Value;
  76.         }
  77.  
  78.         static string CreatePostData(string upsaId, string tag) {
  79.             var timeSpan = GetTimestamp(DateTime.Now);
  80.             var data = "{\"deleteIds\":[],\"update\":[],\"assign\":[{\"type\":1,\"entityId\":\"" + upsaId +
  81.                 "\",\"author\":\"Aliaksandr Orgish\",\"authorId\":\"4000741400003807640\",\"isPrivate\":false,\"name\":\"" + tag +
  82.                 "\"}],\"timestamp\":" + timeSpan + "}";
  83.             return data;
  84.         }
  85.  
  86.         static string GetTimestamp(DateTime value) {
  87.             return value.ToString("yyyyMMddHHmmssfff");
  88.         }
  89.  
  90.         static void MakeRequest(string data, string coockie) {
  91.             throw new NotImplementedException();
  92.         }
  93.  
  94.         static void WriteNotFinded(List<string> notFindedEmployees) {
  95.             using (var writer = new StreamWriter(notDataOutputFilePath)) {
  96.                 foreach (var employee in notFindedEmployees) {
  97.                     writer.WriteLine(employee);
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement