Advertisement
ar4ebald

Untitled

May 1st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. public class VKClient
  2. {
  3.     private const int RequestDelay = 350;
  4.  
  5.     private static readonly Random Random = new Random();
  6.  
  7.     private static readonly HashSet<string> ImageFiles = new HashSet<string>
  8.     {
  9.         ".jpg",
  10.         ".jpeg",
  11.         ".gif",
  12.         ".bmp",
  13.         ".png"
  14.     };
  15.     private static readonly HashSet<string> BlockedExtensions = new HashSet<string>
  16.     {
  17.         ".exe",
  18.         ".jar",
  19.         ".zip",
  20.         ".reg",
  21.         ".ps1",
  22.         ".bat"
  23.     };
  24.  
  25.     private readonly string _accessToken;
  26.     private readonly Stopwatch _callTimer;
  27.  
  28.     private string _docsUploadUrl;
  29.  
  30.     public VKClient(string accessToken)
  31.     {
  32.         _accessToken = accessToken;
  33.         _callTimer = Stopwatch.StartNew();
  34.     }
  35.  
  36.     public JToken Run(string method, params object[] args)
  37.     {
  38.         var wait = RequestDelay - _callTimer.ElapsedMilliseconds;
  39.         if (wait > 0) Thread.Sleep((int)wait);
  40.         _callTimer.Restart();
  41.  
  42.         return VK.Run(method, args.Concat(new[] { "access_token", _accessToken }).ToArray());
  43.     }
  44.  
  45.     public void SendMessage(long id, string message, params Attachment[] attachments)
  46.     {
  47.         Run("messages.send",
  48.             "peer_id", id,
  49.             "random_id", Random.Next(),
  50.             "message", message ?? "...",
  51.             "attachment", string.Join<Attachment>(",", attachments));
  52.     }
  53.  
  54.     private void GetDocsUploadServer()
  55.     {
  56.         _docsUploadUrl = Run("docs.getUploadServer")?["response"]?["upload_url"]?.AsString;
  57.     }
  58.  
  59.     public JToken UploadDoc(string file)
  60.     {
  61.         string directory = null;
  62.         if (BlockedExtensions.Contains(Path.GetExtension(file)))
  63.         {
  64.             directory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
  65.             Directory.CreateDirectory(directory);
  66.             var fileCopy = $"{directory}\\{Path.GetFileName(file)}.txt";
  67.             File.Copy(file, fileCopy);
  68.             file = fileCopy;
  69.         }
  70.  
  71.         string uploadResult = null;
  72.  
  73.         try
  74.         {
  75.             if (_docsUploadUrl == null)
  76.                 GetDocsUploadServer();
  77.  
  78.             try
  79.             {
  80.                 using (var client = new WebClient())
  81.                     uploadResult = JToken.Parse(Encoding.UTF8.GetString(client.UploadFile(_docsUploadUrl, file)))?["file"]?.AsString;
  82.             }
  83.             catch (WebException)
  84.             {
  85.                 GetDocsUploadServer();
  86.  
  87.                 using (var client = new WebClient())
  88.                     uploadResult = JToken.Parse(Encoding.UTF8.GetString(client.UploadFile(_docsUploadUrl, file)))?["file"]?.AsString;
  89.             }
  90.         }
  91.         finally
  92.         {
  93.             if (directory != null && Directory.Exists(directory))
  94.                 Directory.Delete(directory, true);
  95.         }
  96.  
  97.         return Run("docs.save", "file", uploadResult)?["response"]?.AsArray.First();
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement