Guest User

Untitled

a guest
Jul 8th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.93 KB | None | 0 0
  1. /* Type-ish class to hold youtube usernames, and passwords */
  2. public class clsCredentials
  3. {
  4. public clsCredentials(string Username, string Password) { this.Username = Username; this.Password = Password; }
  5. public clsCredentials() { }
  6. public string Username
  7. {
  8. get;
  9. set;
  10. }
  11. public string Password
  12. {
  13. get;
  14. set;
  15. }
  16. }
  17.  
  18. /* Type-ish class to hold data points (old/new value + data field) */
  19. public class clsDataPoint
  20. {
  21. public clsDataPoint()
  22. {
  23. this.Old = this.New = 0;
  24. this.Field = new clsDataPointField();
  25. this.Time = DateTime.Now;
  26. }
  27. public clsDataPoint(double OldValue, double NewValue, clsDataPointField DataField, string VideoID)
  28. {
  29. this.Old = OldValue;
  30. this.New = NewValue;
  31. this.Field = DataField;
  32. this.Time = DateTime.Now;
  33. this.VideoID = VideoID;
  34. }
  35.  
  36. public override string ToString()
  37. {
  38. string[] values = {Time.ToShortDateString(), Time.ToShortTimeString(), VideoID, Field.Field.ToString(), Old.ToString(), New.ToString() };
  39. return string.Join(",", values);
  40. }
  41.  
  42. public clsDataPointField Field { get; set; }
  43. public double Old { get; set; }
  44. public double New { get; set; }
  45. public double Delta { get { return New - Old; } }
  46. public DateTime Time { get; set; }
  47. public string VideoID { get; set; }
  48. }
  49.  
  50. /* Type-ish class to hold data field values and convert to string equivalents */
  51. public class clsDataPointField
  52. {
  53. public enum VideoDataFields
  54. {
  55. UNKNOWN = -1,
  56. VIEWS,
  57. RATERS,
  58. AVERAGE_RATING,
  59. COMMENT_COUNT,
  60. FAVORITED_COUNT
  61. }
  62. public clsDataPointField() { this.Field = VideoDataFields.UNKNOWN; }
  63. public clsDataPointField(VideoDataFields f) { this.Field = f; }
  64. public VideoDataFields Field { get; set; }
  65. public override string ToString()
  66. {
  67. switch (this.Field)
  68. {
  69. case VideoDataFields.AVERAGE_RATING:
  70. return "Average Rating";
  71. case VideoDataFields.COMMENT_COUNT:
  72. return "Comment Count";
  73. case VideoDataFields.FAVORITED_COUNT:
  74. return "Favorited Count";
  75. case VideoDataFields.RATERS:
  76. return "Raters";
  77. case VideoDataFields.UNKNOWN:
  78. return "Unknown";
  79. case VideoDataFields.VIEWS:
  80. return "Views";
  81. default:
  82. return "Error";
  83. }
  84. }
  85. }
  86.  
  87. /* Wrapper class to access the information needed from YouTubeEntry objects */
  88. public class clsVideoEntry
  89. {
  90. private YouTubeEntry _yt_entry = null;
  91.  
  92. // constructor
  93. public clsVideoEntry(YouTubeEntry e)
  94. {
  95. _yt_entry = e;
  96. }
  97.  
  98. public override string ToString()
  99. {
  100. string[] values = { Time.ToShortDateString(), Time.ToShortTimeString(), VideoID, Title, Raters.ToString(), AverageRating.ToString(), ViewsCount.ToString(), CommentCount.ToString(), FavoritedCount.ToString() };
  101. return string.Join(",", values);
  102. }
  103. // public properties
  104. public bool IsNull
  105. {
  106. get { return (_yt_entry == null); }
  107. }
  108. public int Raters
  109. {
  110. get { try { return _yt_entry.Rating.NumRaters; } catch { return 0; } }
  111. }
  112. public double AverageRating
  113. {
  114. get { try { return _yt_entry.Rating.Average; } catch { return 0; } }
  115. }
  116. public int ViewsCount
  117. {
  118. get { try { return int.Parse(_yt_entry.Statistics.ViewCount); } catch { return 0; } }
  119. }
  120. public int CommentCount
  121. {
  122. get { try { return _yt_entry.Comments.FeedLink.CountHint; } catch { return 0; } }
  123. }
  124. public int FavoritedCount
  125. {
  126. get { try { return int.Parse(_yt_entry.Statistics.FavoriteCount); } catch { return 0; } }
  127. }
  128. public string VideoID
  129. {
  130. get { try { return _yt_entry.VideoId; } catch { return string.Empty; } }
  131. }
  132. public string Title
  133. {
  134. get { try { return _yt_entry.Title.Text; } catch { return string.Empty; } }
  135. }
  136. public YouTubeEntry YouTubeEntry
  137. {
  138. get { return _yt_entry; }
  139. }
  140. public DateTime Time { get; set; }
  141. }
  142.  
  143. /* Bot-ish class that contantly maintains an updated dictionary of clsDataPoint objects */
  144. public class clsStatMonger
  145. {
  146. // private data members
  147. private List<clsCredentials> _accounts = new List<clsCredentials>();
  148. private List<clsVideoFeedReader> _feed_readers = new List<clsVideoFeedReader>();
  149. private List<clsVideoEntry> _initial_dataset = new List<clsVideoEntry>();
  150. private List<clsVideoEntry> _current_dataset = new List<clsVideoEntry>();
  151. private Dictionary<string, List<clsDataPoint>> _historical_data = new Dictionary<string, List<clsDataPoint>>();
  152. private System.Timers.Timer _update_timer;
  153. private clsFileLogger _file_logger = null;
  154.  
  155. // constructor
  156. public clsStatMonger(string DeveloperKey, string ApplicationName, List<clsCredentials> Credentials)
  157. {
  158. this.Enabled = false;
  159.  
  160. this.Interval = 20 * 60 * 1000; // default 20 minutes
  161. _update_timer = new System.Timers.Timer();
  162. _update_timer.Enabled = false;
  163. _update_timer.Interval = this.Interval;
  164. _update_timer.Elapsed += new ElapsedEventHandler(_update_timer_Elapsed);
  165.  
  166. _accounts = Credentials;
  167. foreach (clsCredentials c in _accounts)
  168. {
  169. clsVideoFeedReader new_feed = new clsVideoFeedReader(DeveloperKey, ApplicationName, c.Username);
  170. if (c.Password != string.Empty)
  171. new_feed.SetCredentials(c.Username, c.Password);
  172. new_feed.OnEntryFetched += new clsVideoFeedReader.EntryFetchedHandler(new_feed_OnEntryFetched);
  173. _feed_readers.Add(new_feed);
  174. }
  175. }
  176.  
  177. // public methods
  178. public void Enable()
  179. {
  180. if (this.Enabled)
  181. return;
  182. this.Enabled = true;
  183. _update_videos();
  184. _update_timer.Interval = this.Interval;
  185. _update_timer.Enabled = true;
  186. }
  187. public void Disable()
  188. {
  189. this.Enabled = false;
  190. _update_timer.Enabled = false;
  191. }
  192. public void Update()
  193. {
  194. _update_videos();
  195. }
  196.  
  197. // public events
  198. public delegate void EntryAddedEventHandler(object Sender, clsVideoEntry Entry);
  199. public event EntryAddedEventHandler OnEntryAdded;
  200. private void _entry_added(clsVideoEntry Entry)
  201. {
  202. if (_file_logger != null)
  203. _file_logger.appendFile(Entry.ToString());
  204. if (OnEntryAdded != null)
  205. OnEntryAdded(this, Entry);
  206. }
  207.  
  208. public delegate void EntryUpdatedEventHandler(object Sender, clsDataPoint DataPoint, clsVideoEntry Entry);
  209. public event EntryUpdatedEventHandler OnEntryUpdated;
  210. private void _entry_updated(clsDataPoint DataPoint, clsVideoEntry Entry)
  211. {
  212. if (_file_logger != null)
  213. _file_logger.appendFile(DataPoint.ToString());
  214. if (OnEntryUpdated != null)
  215. OnEntryUpdated(this, DataPoint, Entry);
  216. }
  217.  
  218. // private methods
  219. void _update_timer_Elapsed(object sender, ElapsedEventArgs e)
  220. {
  221. if (this.Enabled == false)
  222. return;
  223. _update_timer.Interval = this.Interval;
  224. _update_videos();
  225. }
  226. private void new_feed_OnEntryFetched(object Sender, clsVideoEntry Entry)
  227. {
  228. clsVideoEntry initial_entry;
  229. if ((initial_entry = _GetEntryByIdFromList(_initial_dataset, Entry.VideoID)) == null)
  230. {
  231. _initial_dataset.Add(Entry);
  232. _current_dataset.Add(Entry);
  233. _entry_added(Entry);
  234. }
  235. else
  236. {
  237. clsVideoEntry CurrentEntry = _GetEntryByIdFromList(_current_dataset, Entry.VideoID);
  238. if (CurrentEntry == null)
  239. {
  240. _current_dataset.Add(Entry);
  241. _compare_entries(initial_entry, Entry);
  242. }
  243. else
  244. {
  245. _current_dataset.Remove(CurrentEntry);
  246. _current_dataset.Add(Entry);
  247. _compare_entries(CurrentEntry, Entry);
  248. }
  249. }
  250. }
  251. private void _compare_entries(clsVideoEntry OldEntry, clsVideoEntry NewEntry)
  252. {
  253. if (OldEntry.VideoID != NewEntry.VideoID)
  254. return;
  255.  
  256. _compare_stat(OldEntry.AverageRating, NewEntry.AverageRating, clsDataPointField.VideoDataFields.AVERAGE_RATING, NewEntry);
  257. _compare_stat(OldEntry.CommentCount, NewEntry.CommentCount, clsDataPointField.VideoDataFields.COMMENT_COUNT, NewEntry);
  258. _compare_stat(OldEntry.FavoritedCount, NewEntry.FavoritedCount, clsDataPointField.VideoDataFields.FAVORITED_COUNT, NewEntry);
  259. _compare_stat(OldEntry.Raters, NewEntry.Raters, clsDataPointField.VideoDataFields.RATERS, NewEntry);
  260. _compare_stat(OldEntry.ViewsCount, NewEntry.ViewsCount, clsDataPointField.VideoDataFields.VIEWS, NewEntry);
  261.  
  262. }
  263. private void _compare_stat(double Old, double New, clsDataPointField.VideoDataFields Field, clsVideoEntry Entry)
  264. {
  265. if (Old == New)
  266. return;
  267. else
  268. {
  269. clsDataPoint new_datapoint = new clsDataPoint(Old, New, new clsDataPointField(Field), Entry.VideoID);
  270. if (_historical_data.ContainsKey(Entry.VideoID))
  271. _historical_data[Entry.VideoID].Add(new_datapoint);
  272. else
  273. {
  274. List<clsDataPoint> new_dp_list = new List<clsDataPoint>();
  275. new_dp_list.Add(new_datapoint);
  276. _historical_data.Add(Entry.VideoID, new_dp_list);
  277. }
  278. _entry_updated(new_datapoint, Entry);
  279. }
  280. }
  281. private clsVideoEntry _GetEntryByIdFromList(List<clsVideoEntry> Entries, string VideoID)
  282. {
  283. int index = Entries.FindIndex(delegate(clsVideoEntry e) { return e.VideoID.Equals(VideoID); });
  284. return (index == -1) ? null : Entries[index];
  285. }
  286. private void _update_videos()
  287. {
  288. foreach (clsVideoFeedReader r in _feed_readers)
  289. if (!r.IsBusy)
  290. r.GetVideosModifiedSince(r.Updated);
  291. }
  292.  
  293. // public properties
  294. public bool Enabled { get; private set; }
  295. public int Interval { get; set; }
  296. public List<clsVideoEntry> CurrentDataSet
  297. {
  298. get { return new List<clsVideoEntry>(_current_dataset); }
  299. }
  300. public List<clsVideoEntry> InitialDataSet
  301. {
  302. get { return new List<clsVideoEntry>(_initial_dataset); }
  303. }
  304. public Dictionary<string, List<clsDataPoint>> HistoricalDataPoints
  305. {
  306. get { return new Dictionary<string, List<clsDataPoint>>(_historical_data); }
  307. }
  308. public clsFileLogger FileLogger
  309. {
  310. get { return _file_logger; }
  311. set { _file_logger = value; }
  312. }
  313. }
Add Comment
Please, Sign In to add comment