Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 2.18 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using Dulcet.Twitter;
  7. using Inscribe.Storage;
  8. using Supervisor;
  9.  
  10. namespace Inscribe.Communication.CruiseControl.DefaultTasks
  11. {
  12.     public abstract class ReceiveTaskBase : ScheduledTask
  13.     {
  14.         private double newbiesRate = 1.0;
  15.  
  16.         private double timesPerTweet = 0.0;
  17.  
  18.         private DateTime previousReceived = DateTime.MinValue;
  19.  
  20.         public override double Rate
  21.         {
  22.             get
  23.             {
  24.                 var t = DateTime.Now.Subtract(previousReceived).TotalMilliseconds;
  25.                 var tp = t * newbiesRate;
  26.                 return tp / (tp + TwitterDefine.HomeTimelineReceiveMaxCount * timesPerTweet);
  27.             }
  28.         }
  29.  
  30.         public override void DoWork()
  31.         {
  32.             try
  33.             {
  34.                 var received = GetTweets().ToArray();
  35.                 previousReceived = DateTime.Now;
  36.                 var newbiesCount = received.Count(s => TweetStorage.Contains(s.Id) == TweetExistState.Unreceived);
  37.                 received.ForEach(s => TweetStorage.Register(s));
  38.                 var oldest = received.LastOrDefault();
  39.                 var newest = received.FirstOrDefault();
  40.                 if (oldest != null && newest != null)
  41.                 {
  42.                     newbiesRate = (double)newbiesCount / received.Length;
  43.                     timesPerTweet = (double)(newest.CreatedAt.Subtract(oldest.CreatedAt)).TotalMilliseconds / received.Length;
  44.                 }
  45.             }
  46.             catch (WebException ex)
  47.             {
  48.                 ExceptionStorage.Register(ex, ExceptionCategory.TwitterError, "Twitterとの通信に失敗しました。");
  49.             }
  50.             catch (IOException ioex)
  51.             {
  52.                 ExceptionStorage.Register(ioex, ExceptionCategory.TwitterError, "ネットワークでエラーが発生しました。");
  53.             }
  54.             catch (Exception ex)
  55.             {
  56.                 ExceptionStorage.Register(ex, ExceptionCategory.InternalError, "ステータス受信時に内部エラーが発生しました。");
  57.             }
  58.         }
  59.  
  60.         protected abstract IEnumerable<TwitterStatusBase> GetTweets();
  61.     }
  62. }