Guest User

Untitled

a guest
Jul 9th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Google.GData.YouTube;
  6. using Google.GData.Extensions;
  7. using Google.YouTube;
  8. using System.Threading;
  9. using System.IO;
  10. using System.Xml;
  11. using System.Runtime.InteropServices;
  12. using System.Timers;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using System.Web;
  16. using mshtml;
  17.  
  18. namespace TubeGuardian
  19. {
  20. /* Bot-ish class that contantly maintains an updated dictionary of clsDataPoint objects */
  21. public class clsStatMonger
  22. {
  23. // private data members
  24. private List<clsCredentials> _accounts = new List<clsCredentials>();
  25. private List<clsVideoFeedReader> _feed_readers = new List<clsVideoFeedReader>();
  26. private List<clsVideoEntry> _initial_dataset = new List<clsVideoEntry>();
  27. private List<clsVideoEntry> _current_dataset = new List<clsVideoEntry>();
  28. private Dictionary<string, List<clsDataPoint>> _historical_data = new Dictionary<string, List<clsDataPoint>>();
  29. private System.Timers.Timer _update_timer;
  30. private clsFileLogger _file_logger = null;
  31. private string _dev_key = string.Empty;
  32. private string _app_name = string.Empty;
  33. private clsSettings _settings;
  34.  
  35. // constructor
  36. public clsStatMonger(string DeveloperKey, string ApplicationName, List<clsCredentials> Credentials, clsSettings settings)
  37. {
  38. _file_logger = new clsFileLogger("CollectorData.log");
  39. _settings = settings;
  40. _settings.OnAccountAdded += new clsSettings.AccountAddedHandler(_settings_OnAccountAdded);
  41. _settings.OnAccountRemoved += new clsSettings.AccountRemovedHandler(_settings_OnAccountRemoved);
  42.  
  43. _dev_key = DeveloperKey;
  44. _app_name = ApplicationName;
  45.  
  46. this.Enabled = false;
  47.  
  48. _update_timer = new System.Timers.Timer();
  49. _update_timer.Enabled = false;
  50. _update_timer.Interval = _settings.Collect_Interval * 1000 * 60;
  51. _update_timer.Elapsed += new ElapsedEventHandler(_update_timer_Elapsed);
  52.  
  53. _accounts = Credentials;
  54. foreach (clsCredentials c in _accounts)
  55. {
  56. clsVideoFeedReader new_feed = new clsVideoFeedReader(DeveloperKey, ApplicationName, c.Username);
  57. if (c.Password != string.Empty && c.Password != "-")
  58. new_feed.SetCredentials(c.Username, c.Password);
  59. new_feed.OnEntryFetched += new clsVideoFeedReader.EntryFetchedHandler(new_feed_OnEntryFetched);
  60. new_feed.OnStatusChange += new clsVideoFeedReader.StatusChangeHandler(new_feed_OnStatusChange);
  61. _feed_readers.Add(new_feed);
  62. }
  63. }
  64.  
  65. void _settings_OnAccountRemoved(object sender, clsCredentials Account)
  66. {
  67. RemoveAccount(Account);
  68. }
  69. void _settings_OnAccountAdded(object sender, clsCredentials Account)
  70. {
  71. AddAccount(Account);
  72. }
  73.  
  74. // public methods
  75. public void Enable()
  76. {
  77. if (this.Enabled)
  78. return;
  79. this.Enabled = true;
  80. _update_videos();
  81. _update_timer.Interval = _settings.Collect_Interval;
  82. _update_timer.Enabled = true;
  83. }
  84. public void Disable()
  85. {
  86. this.Enabled = false;
  87. _update_timer.Enabled = false;
  88. }
  89. public void Update()
  90. {
  91. _update_videos();
  92. }
  93. public void AddAccount(clsCredentials Account)
  94. {
  95. foreach (clsVideoFeedReader r in _feed_readers)
  96. if (r.Username == Account.Username)
  97. return;
  98.  
  99. clsVideoFeedReader new_feed = new clsVideoFeedReader(_dev_key, _app_name, Account.Username);
  100. if (Account.Password != "-")
  101. new_feed.SetCredentials(Account.Username, Account.Password);
  102.  
  103. new_feed.OnEntryFetched += new clsVideoFeedReader.EntryFetchedHandler(new_feed_OnEntryFetched);
  104. new_feed.OnStatusChange += new clsVideoFeedReader.StatusChangeHandler(new_feed_OnStatusChange);
  105. new_feed.OnException += new clsVideoFeedReader.ExceptionHandler(new_feed_OnException);
  106. _feed_readers.Add(new_feed);
  107. }
  108. public void RemoveAccount(clsCredentials Account)
  109. {
  110. int i = 0;
  111. while (i < _feed_readers.Count)
  112. {
  113. if (_feed_readers[i].Username == Account.Username)
  114. {
  115. _feed_readers[i].Dispose();
  116. _feed_readers.RemoveAt(i);
  117. }
  118. else
  119. i++;
  120. }
  121. }
  122.  
  123. // public events
  124. public delegate void EntryAddedEventHandler(object Sender, clsVideoEntry Entry);
  125. public event EntryAddedEventHandler OnEntryAdded;
  126. private void _entry_added(clsVideoEntry Entry)
  127. {
  128. if (_file_logger != null)
  129. _file_logger.appendFile(Entry.ToString());
  130. if (OnEntryAdded != null)
  131. OnEntryAdded(this, Entry);
  132. }
  133.  
  134. public delegate void EntryUpdatedEventHandler(object Sender, clsDataPoint DataPoint, clsVideoEntry Entry);
  135. public event EntryUpdatedEventHandler OnEntryUpdated;
  136. private void _entry_updated(clsDataPoint DataPoint, clsVideoEntry Entry)
  137. {
  138. if (_file_logger != null)
  139. _file_logger.appendFile(DataPoint.ToString());
  140. if (OnEntryUpdated != null)
  141. OnEntryUpdated(this, DataPoint, Entry);
  142. }
  143.  
  144. public delegate void FeedReaderUpdatedEventHandler(object Sender, clsVideoFeedReader.enumFeedReaderState status);
  145. public event FeedReaderUpdatedEventHandler OnFeedReaderUpdated;
  146. private void _feed_reader_updated(object sender, clsVideoFeedReader.enumFeedReaderState status)
  147. {
  148. if (OnFeedReaderUpdated != null)
  149. OnFeedReaderUpdated(sender, status);
  150. }
  151.  
  152. public delegate void FeedReaderExceptionEventHandler(object Sender, string exception);
  153. public event FeedReaderExceptionEventHandler OnFeedReaderException;
  154. private void _feed_reader_exception(object sender, string exception)
  155. {
  156. if (OnFeedReaderException != null)
  157. OnFeedReaderException(sender, exception);
  158. }
  159.  
  160. // private methods
  161. private void _update_timer_Elapsed(object sender, ElapsedEventArgs e)
  162. {
  163. if (this.Enabled == false)
  164. return;
  165. _update_timer.Enabled = false;
  166. _update_timer.Interval = _settings.Collect_Interval * 1000 * 60;
  167. _update_timer.Enabled = true;
  168. _update_videos();
  169. }
  170. private void new_feed_OnStatusChange(object Sender, clsVideoFeedReader.enumFeedReaderState NewState)
  171. {
  172. _feed_reader_updated(Sender, NewState);
  173. }
  174. private void new_feed_OnEntryFetched(object Sender, clsVideoEntry Entry)
  175. {
  176. clsVideoEntry initial_entry;
  177. if ((initial_entry = _GetEntryByIdFromList(_initial_dataset, Entry.VideoID)) == null)
  178. {
  179. _initial_dataset.Add(Entry);
  180. _current_dataset.Add(Entry);
  181. _entry_added(Entry);
  182. }
  183. else
  184. {
  185. clsVideoEntry CurrentEntry = _GetEntryByIdFromList(_current_dataset, Entry.VideoID);
  186. if (CurrentEntry == null)
  187. {
  188. _current_dataset.Add(Entry);
  189. _compare_entries(initial_entry, Entry);
  190. }
  191. else
  192. {
  193. _current_dataset.Remove(CurrentEntry);
  194. _current_dataset.Add(Entry);
  195. _compare_entries(CurrentEntry, Entry);
  196. }
  197. }
  198. }
  199. private void new_feed_OnException(object Sender, Exception e)
  200. {
  201. _feed_reader_exception(Sender, e.Message);
  202. }
  203. private void _compare_entries(clsVideoEntry OldEntry, clsVideoEntry NewEntry)
  204. {
  205. if (OldEntry.VideoID != NewEntry.VideoID)
  206. return;
  207.  
  208. _compare_stat(OldEntry.AverageRating, NewEntry.AverageRating, clsDataPointField.VideoDataFields.AVERAGE_RATING, NewEntry);
  209. _compare_stat(OldEntry.CommentCount, NewEntry.CommentCount, clsDataPointField.VideoDataFields.COMMENT_COUNT, NewEntry);
  210. _compare_stat(OldEntry.FavoritedCount, NewEntry.FavoritedCount, clsDataPointField.VideoDataFields.FAVORITED_COUNT, NewEntry);
  211. _compare_stat(OldEntry.Raters, NewEntry.Raters, clsDataPointField.VideoDataFields.RATERS, NewEntry);
  212. _compare_stat(OldEntry.ViewsCount, NewEntry.ViewsCount, clsDataPointField.VideoDataFields.VIEWS, NewEntry);
  213.  
  214. }
  215. private void _compare_stat(double Old, double New, clsDataPointField.VideoDataFields Field, clsVideoEntry Entry)
  216. {
  217. if (Old == New)
  218. return;
  219. else
  220. {
  221. clsDataPoint new_datapoint = new clsDataPoint(Old, New, new clsDataPointField(Field), Entry.VideoID);
  222. if (_historical_data.ContainsKey(Entry.VideoID))
  223. _historical_data[Entry.VideoID].Add(new_datapoint);
  224. else
  225. {
  226. List<clsDataPoint> new_dp_list = new List<clsDataPoint>();
  227. new_dp_list.Add(new_datapoint);
  228. _historical_data.Add(Entry.VideoID, new_dp_list);
  229. }
  230. _entry_updated(new_datapoint, Entry);
  231. }
  232. }
  233. private clsVideoEntry _GetEntryByIdFromList(List<clsVideoEntry> Entries, string VideoID)
  234. {
  235. int index = Entries.FindIndex(delegate(clsVideoEntry e) { return e.VideoID.Equals(VideoID); });
  236. return (index == -1) ? null : Entries[index];
  237. }
  238. private void _update_videos()
  239. {
  240. foreach (clsVideoFeedReader r in _feed_readers)
  241. if (!r.IsBusy)
  242. r.GetVideosModifiedSince(r.Updated);
  243. }
  244.  
  245. // public properties
  246. public bool Enabled { get; private set; }
  247. public List<clsVideoEntry> CurrentDataSet
  248. {
  249. get { return new List<clsVideoEntry>(_current_dataset); }
  250. }
  251. public List<clsVideoEntry> InitialDataSet
  252. {
  253. get { return _initial_dataset; }
  254. }
  255. public Dictionary<string, List<clsDataPoint>> HistoricalDataPoints
  256. {
  257. get { return _historical_data; }
  258. }
  259. public clsFileLogger FileLogger
  260. {
  261. get { return _file_logger; }
  262. set { _file_logger = value; }
  263. }
  264. public List<clsVideoEntry> GetVideosByUsername(string username)
  265. {
  266. List<clsVideoEntry> ret = new List<clsVideoEntry>();
  267. foreach (clsVideoEntry e in _current_dataset)
  268. if (e.Account.Username == username)
  269. ret.Add(e);
  270. return ret;
  271. }
  272. public List<clsVideoFeedReader> FeedReaders { get { return _feed_readers; } }
  273. }
  274.  
  275. /* Type-ish class to hold youtube usernames, and passwords */
  276. public class clsCredentials
  277. {
  278. public clsCredentials(string Username, string Password) { this.Username = Username; this.Password = Password; }
  279. public clsCredentials() { }
  280. public string Username
  281. {
  282. get;
  283. set;
  284. }
  285. public string Password
  286. {
  287. get;
  288. set;
  289. }
  290. }
  291.  
  292. /* Type-ish class to hold data points (old/new value + data field) */
  293. public class clsDataPoint
  294. {
  295. public clsDataPoint()
  296. {
  297. this.Old = this.New = 0;
  298. this.Field = new clsDataPointField();
  299. this.Time = DateTime.Now;
  300. }
  301. public clsDataPoint(double OldValue, double NewValue, clsDataPointField DataField, string VideoID)
  302. {
  303. this.Old = OldValue;
  304. this.New = NewValue;
  305. this.Field = DataField;
  306. this.Time = DateTime.Now;
  307. this.VideoID = VideoID;
  308. }
  309.  
  310. public override string ToString()
  311. {
  312. string[] values = {"upd : d{" + Time.ToShortDateString() + "}", "t{" + Time.ToShortTimeString() + "}", "vId{" + VideoID, Field.Field.ToString() + "}", "old{" + Old.ToString() + "}", "new{" + New.ToString() + "}" };
  313. return string.Join(",", values);
  314. }
  315.  
  316. public clsDataPointField Field { get; set; }
  317. public double Old { get; set; }
  318. public double New { get; set; }
  319. public double Delta { get { return New - Old; } }
  320. public DateTime Time { get; set; }
  321. public string VideoID { get; set; }
  322. }
  323.  
  324. /* Type-ish class to hold data field values and convert to string equivalents */
  325. public class clsDataPointField
  326. {
  327. public enum VideoDataFields
  328. {
  329. UNKNOWN = -1,
  330. VIEWS,
  331. RATERS,
  332. AVERAGE_RATING,
  333. COMMENT_COUNT,
  334. FAVORITED_COUNT
  335. }
  336. public clsDataPointField() { this.Field = VideoDataFields.UNKNOWN; }
  337. public clsDataPointField(VideoDataFields f) { this.Field = f; }
  338. public VideoDataFields Field { get; set; }
  339. public override string ToString()
  340. {
  341. switch (this.Field)
  342. {
  343. case VideoDataFields.AVERAGE_RATING:
  344. return "Average Rating";
  345. case VideoDataFields.COMMENT_COUNT:
  346. return "Comment Count";
  347. case VideoDataFields.FAVORITED_COUNT:
  348. return "Favorited Count";
  349. case VideoDataFields.RATERS:
  350. return "Raters";
  351. case VideoDataFields.UNKNOWN:
  352. return "Unknown";
  353. case VideoDataFields.VIEWS:
  354. return "Views";
  355. default:
  356. return "Error";
  357. }
  358. }
  359. }
  360.  
  361. /* Wrapper class to access the information needed from YouTubeEntry objects */
  362. public class clsVideoEntry
  363. {
  364. private YouTubeEntry _yt_entry = null;
  365.  
  366. // constructor
  367. public clsVideoEntry(YouTubeEntry e)
  368. {
  369. _yt_entry = e;
  370. }
  371.  
  372. public override string ToString()
  373. {
  374. string[] values = { "init : t{" + Time.ToShortDateString() + "}", "d{" + Time.ToShortTimeString() + "}","vIf{" + VideoID + "}","ti{" + Title + "}", "r#{" + Raters.ToString() +"}", "ar{" + AverageRating.ToString() + "}", "v#{" + ViewsCount.ToString() + "}", "c#{" + CommentCount.ToString() + "}", "f#{" + FavoritedCount.ToString() + "}" };
  375. return string.Join(",", values);
  376. }
  377. public override bool Equals(object obj)
  378. {
  379. clsVideoEntry e = (clsVideoEntry)obj;
  380. return this.VideoID.Equals(e.VideoID);
  381. }
  382. public override int GetHashCode()
  383. {
  384. return this.Account.Username.GetHashCode();
  385. }
  386. // public properties
  387. public bool IsNull
  388. {
  389. get { return (_yt_entry == null); }
  390. }
  391. public int Raters
  392. {
  393. get { try { return _yt_entry.Rating.NumRaters; } catch { return 0; } }
  394. }
  395. public double AverageRating
  396. {
  397. get { try { return _yt_entry.Rating.Average; } catch { return 0; } }
  398. }
  399. public int ViewsCount
  400. {
  401. get { try { return int.Parse(_yt_entry.Statistics.ViewCount); } catch { return 0; } }
  402. }
  403. public int CommentCount
  404. {
  405. get { try { return _yt_entry.Comments.FeedLink.CountHint; } catch { return 0; } }
  406. }
  407. public int FavoritedCount
  408. {
  409. get { try { return int.Parse(_yt_entry.Statistics.FavoriteCount); } catch { return 0; } }
  410. }
  411. public string VideoID
  412. {
  413. get { try { return _yt_entry.VideoId; } catch { return string.Empty; } }
  414. }
  415. public string Title
  416. {
  417. get { try { return _yt_entry.Title.Text; } catch { return string.Empty; } }
  418. }
  419. public YouTubeEntry YouTubeEntry
  420. {
  421. get { return _yt_entry; }
  422. }
  423. public DateTime Time { get; set; }
  424. public clsCredentials Account { get; set; }
  425. }
  426.  
  427. /* Utility class for reading video feeds asynchronously for a single account */
  428. public class clsVideoFeedReader
  429. {
  430. private class QueryArgs
  431. {
  432. public YouTubeService Service { get; set; }
  433. public YouTubeQuery Query { get; set; }
  434. }
  435. private class QueryReturn
  436. {
  437. public YouTubeFeed Feed { get; set; }
  438. public Exception Exception { get; set; }
  439. public YouTubeQuery NextQuery { get; set; }
  440. }
  441. public enum enumFeedReaderState
  442. {
  443. ERROR = -1,
  444. IDLE,
  445. GETTING_FIRST_CHUNK,
  446. GOT_FIRST_CHUNK,
  447. GETTING_ANOTHER_CHUNK,
  448. GOT_ANOTHER_CHUUNK
  449. }
  450.  
  451. private enumFeedReaderState _state = enumFeedReaderState.IDLE;
  452. private YouTubeService _yt_service = null;
  453. private string _username = string.Empty;
  454. private string _password = string.Empty;
  455. private string _dev_key = string.Empty;
  456. private string _app_name = string.Empty;
  457. private int _retrieved = 0;
  458. private int _max_retry_query = 3;
  459. private int _current_retry_query = 1;
  460. private System.ComponentModel.BackgroundWorker _query_thread = new System.ComponentModel.BackgroundWorker();
  461. private DateTime _last_updated = DateTime.MinValue;
  462.  
  463. public clsVideoFeedReader(string DevelopersKey, string ApplicationName, string Username)
  464. {
  465. _dev_key = DevelopersKey;
  466. _app_name = ApplicationName;
  467. _username = Username;
  468. _yt_service = new YouTubeService(ApplicationName, string.Empty, DevelopersKey);
  469. _query_thread.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(_query_thread_RunWorkerCompleted);
  470. _query_thread.DoWork += new System.ComponentModel.DoWorkEventHandler(_query_thread_DoWork);
  471. }
  472. public clsVideoFeedReader(string DevelopersKey, string ApplicationName, string Username, string Password)
  473. : this(DevelopersKey, ApplicationName, Username)
  474. {
  475. if (_password != "-")
  476. {
  477. _password = Password;
  478. SetCredentials(Username, Password);
  479. }
  480. }
  481.  
  482. // public methods
  483. public void SetCredentials(string Username, string Password)
  484. {
  485. _username = Username;
  486. _password = Password;
  487. _yt_service.setUserCredentials(Username, Password);
  488. }
  489. public void GetVideos()
  490. {
  491. YouTubeQuery query = new YouTubeQuery(YouTubeQuery.CreateUserUri(_username));
  492.  
  493. query.NumberToRetrieve = 50;
  494. _retrieved = 0;
  495. _current_retry_query = 1;
  496. _status_changed(enumFeedReaderState.GETTING_FIRST_CHUNK);
  497. _last_updated = DateTime.Now;
  498. _do_query(query);
  499. }
  500. public void GetVideosModifiedSince(DateTime When)
  501. {
  502. YouTubeQuery query = new YouTubeQuery(YouTubeQuery.CreateUserUri(_username));
  503.  
  504. query.NumberToRetrieve = 50;
  505. query.ModifiedSince = When;
  506. _retrieved = 0;
  507. _current_retry_query = 1;
  508. _status_changed(enumFeedReaderState.GETTING_FIRST_CHUNK);
  509. _last_updated = DateTime.Now;
  510. _do_query(query);
  511. }
  512. public void Dispose()
  513. {
  514. _query_thread.Dispose();
  515. OnEntryFetched = null;
  516. OnException = null;
  517. OnProgress = null;
  518. OnQueryRetry = null;
  519. OnStatusChange = null;
  520. }
  521.  
  522. // private methods
  523. private void _do_query(YouTubeQuery q)
  524. {
  525. if (_query_thread.IsBusy)
  526. return;
  527. QueryArgs qa = new QueryArgs();
  528. qa.Query = q;
  529. qa.Service = _yt_service;
  530. _query_thread.RunWorkerAsync(qa);
  531. }
  532. private void _query_thread_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
  533. {
  534. QueryArgs qa = e.Argument as QueryArgs;
  535. QueryReturn qr = new QueryReturn();
  536.  
  537. qr.Exception = null;
  538. qr.Feed = null;
  539.  
  540. try
  541. {
  542. YouTubeFeed feed = qa.Service.Query(qa.Query);
  543. qr.Feed = feed;
  544. }
  545. catch (Exception exception)
  546. {
  547. qr.Exception = exception;
  548. qr.NextQuery = qa.Query;
  549. }
  550. e.Result = qr;
  551. }
  552. private void _query_thread_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
  553. {
  554. QueryReturn qr = e.Result as QueryReturn;
  555. YouTubeFeed feed = qr.Feed;
  556.  
  557. if (feed != null)
  558. {
  559. _retrieved += feed.Entries.Count;
  560.  
  561. foreach (YouTubeEntry entry in feed.Entries)
  562. _entry_fetched(entry);
  563.  
  564. if (_state == enumFeedReaderState.GETTING_FIRST_CHUNK || _state == enumFeedReaderState.GETTING_ANOTHER_CHUNK)
  565. _status_changed(_state + 1);
  566.  
  567. if (feed.NextChunk != null)
  568. {
  569. _progress(_retrieved, feed.TotalResults);
  570. _status_changed(enumFeedReaderState.GETTING_ANOTHER_CHUNK);
  571. _do_query(new YouTubeQuery(feed.NextChunk));
  572. }
  573. else
  574. {
  575. _progress(1, 1);
  576. _status_changed(enumFeedReaderState.IDLE);
  577. }
  578. }
  579. else
  580. {
  581. _exception(qr.Exception);
  582. if (_current_retry_query < _max_retry_query)
  583. {
  584. _current_retry_query++;
  585. if (qr.NextQuery != null)
  586. {
  587. _status_changed(_state);
  588. _do_query(new YouTubeQuery(qr.NextQuery.Uri.AbsoluteUri));
  589. }
  590. else
  591. {
  592. _progress(1, 1);
  593. _status_changed(enumFeedReaderState.ERROR);
  594. }
  595. }
  596. else
  597. {
  598. _progress(1, 1);
  599. _status_changed(enumFeedReaderState.ERROR);
  600. }
  601. }
  602.  
  603. }
  604.  
  605. // public events
  606. public delegate void StatusChangeHandler(object Sender, enumFeedReaderState NewState);
  607. public event StatusChangeHandler OnStatusChange;
  608. private void _status_changed(enumFeedReaderState state)
  609. {
  610. _state = state;
  611. if (OnStatusChange != null)
  612. OnStatusChange(this, state);
  613. }
  614.  
  615. public delegate void EntryFetchedHandler(object Sender, clsVideoEntry Entry);
  616. public event EntryFetchedHandler OnEntryFetched;
  617. private void _entry_fetched(YouTubeEntry Entry)
  618. {
  619. if (OnEntryFetched != null)
  620. {
  621. clsVideoEntry nEntry = new clsVideoEntry(Entry);
  622. nEntry.Time = DateTime.Now;
  623. nEntry.Account = new clsCredentials(_username, _password);
  624. OnEntryFetched(this, nEntry);
  625. }
  626. }
  627.  
  628. public delegate void ProgressHandler(object Sender, int Current, int Total);
  629. public event ProgressHandler OnProgress;
  630. private void _progress(int Current, int Total)
  631. {
  632. if (OnProgress != null)
  633. OnProgress(this, Current, Total);
  634. }
  635.  
  636. public delegate void QueryRetryHandler(object Sender, Exception e);
  637. public event QueryRetryHandler OnQueryRetry;
  638. private void _query_retry(Exception e)
  639. {
  640. if (OnQueryRetry != null)
  641. OnQueryRetry(this, e);
  642. }
  643.  
  644. public delegate void ExceptionHandler(object Sender, Exception e);
  645. public event ExceptionHandler OnException;
  646. private void _exception(Exception e)
  647. {
  648. if (OnException != null)
  649. OnException(this, e);
  650. }
  651.  
  652. // public properties
  653. public string StateString
  654. {
  655. get
  656. {
  657. switch (_state)
  658. {
  659. case enumFeedReaderState.ERROR:
  660. return "Error";
  661. case enumFeedReaderState.GETTING_ANOTHER_CHUNK:
  662. return "Getting another chunk";
  663. case enumFeedReaderState.GETTING_FIRST_CHUNK:
  664. return "Getting first chunk";
  665. case enumFeedReaderState.GOT_ANOTHER_CHUUNK:
  666. return "Got another chunk";
  667. case enumFeedReaderState.GOT_FIRST_CHUNK:
  668. return "Got first chunk";
  669. case enumFeedReaderState.IDLE:
  670. return "Idle";
  671. default:
  672. return "Error";
  673. }
  674. }
  675. }
  676. public enumFeedReaderState State
  677. {
  678. get { return _state; }
  679. }
  680. public int MaxRetries
  681. {
  682. get { return _max_retry_query; }
  683. set { _max_retry_query = value; }
  684. }
  685. public bool IsBusy
  686. {
  687. get { return _query_thread.IsBusy; }
  688. }
  689. public DateTime Updated
  690. {
  691. get { return _last_updated; }
  692. }
  693. public string Username
  694. {
  695. get { return _username; }
  696. }
  697. }
  698.  
  699. /* Math class for crunching all of the numbers in any given data set */
  700. public class clsStatMasher
  701. {
  702. private List<clsVideoEntry> _initial_dataset = null;
  703. private Dictionary<string, List<clsDataPoint>> _historical_data = null;
  704.  
  705. public clsStatMasher() {}
  706. public clsStatMasher(List<clsVideoEntry> InitialDataSet, Dictionary<string, List<clsDataPoint>> HistoricalData)
  707. {
  708. _initial_dataset = InitialDataSet;
  709. _historical_data = HistoricalData;
  710. }
  711.  
  712. // public methods
  713. public List<clsDataPoint> GetDataPointsByID(string VideoID)
  714. {
  715. if (_historical_data.ContainsKey(VideoID))
  716. return _historical_data[VideoID];
  717. else
  718. return null;
  719. }
  720. public clsVideoEntry GetInitialDataByID(string VideoID)
  721. {
  722. if (_initial_dataset == null)
  723. return null;
  724. int index = _initial_dataset.FindIndex(delegate(clsVideoEntry e) { return e.VideoID.Equals(VideoID); });
  725. if (index >= 0)
  726. return _initial_dataset[index];
  727. else
  728. return null;
  729. }
  730. public double AverageNewRatingByID(string VideoID)
  731. {
  732. clsVideoEntry InitialData = GetInitialDataByID(VideoID);
  733. List<clsDataPoint> HistoricalData = GetDataPointsByID(VideoID);
  734.  
  735. if (InitialData == null || HistoricalData == null || HistoricalData.Count == 0)
  736. return 0;
  737.  
  738. double[] A = { InitialData.AverageRating, -1 };
  739. double[] N = { InitialData.Raters, -1 };
  740. int counted = 0;
  741.  
  742. for (int i = HistoricalData.Count - 1; i >= 0; i--)
  743. {
  744. if (A[1] != -1 && N[1] != -1)
  745. break;
  746.  
  747. if (HistoricalData[i].Field.Field == clsDataPointField.VideoDataFields.RATERS)
  748. {
  749. if (N[1] == -1)
  750. N[1] = HistoricalData[i].New;
  751. counted += (int)HistoricalData[i].Delta;
  752. }
  753. if (HistoricalData[i].Field.Field == clsDataPointField.VideoDataFields.AVERAGE_RATING && A[1] == -1)
  754. A[1] = HistoricalData[i].New;
  755.  
  756. }
  757.  
  758. if (counted == 0)
  759. return 0;
  760.  
  761. if (A[1] == -1)
  762. return 0;
  763.  
  764. return _calc_average_new_rating(A[0], A[1], N[0], N[1]);
  765. }
  766. public KeyValuePair<int,double> AverageNewRatingByID(string VideoID, int numMostRecentVotes)
  767. {
  768. clsVideoEntry InitialData = GetInitialDataByID(VideoID);
  769. List<clsDataPoint> HistoricalData = GetDataPointsByID(VideoID);
  770.  
  771. if (InitialData == null || HistoricalData == null || HistoricalData.Count == 0)
  772. return new KeyValuePair<int,double>(0,0);
  773.  
  774. double[] A = { 0, -1 };
  775. double[] N = { 0, -1 };
  776. int counted = 0;
  777.  
  778. for (int i = HistoricalData.Count - 1; i >= 0; i--)
  779. {
  780. if ((counted >= numMostRecentVotes) && A[1] != -1 && N[1] != 1)
  781. break;
  782. if (HistoricalData[i].Field.Field == clsDataPointField.VideoDataFields.RATERS)
  783. {
  784. if( N[1] == -1)
  785. N[1] = HistoricalData[i].New;
  786. N[0] = HistoricalData[i].Old;
  787. counted += (int)HistoricalData[i].Delta;
  788. }
  789. if (HistoricalData[i].Field.Field == clsDataPointField.VideoDataFields.AVERAGE_RATING)
  790. {
  791. if (A[1] == -1)
  792. A[1] = HistoricalData[i].New;
  793. A[0] = HistoricalData[i].Old;
  794. }
  795. }
  796.  
  797. if (counted == 0)
  798. return new KeyValuePair<int, double>(0, 0);
  799.  
  800. if (A[1] == -1)
  801. return new KeyValuePair<int, double>(0, 0);
  802.  
  803. return new KeyValuePair<int,double>(counted, _calc_average_new_rating(A[0], A[1], N[0], N[1]));
  804. }
  805. public void CleanUp()
  806. {
  807. _initial_dataset = null;
  808. _historical_data = null;
  809. }
  810. public double GetMovedStatByField(string VideoID, clsDataPointField.VideoDataFields field)
  811. {
  812. clsVideoEntry InitialData = GetInitialDataByID(VideoID);
  813. List<clsDataPoint> HistoricalData = GetDataPointsByID(VideoID);
  814.  
  815. if (InitialData == null || HistoricalData == null || HistoricalData.Count == 0)
  816. return 0;
  817.  
  818. double ret = 0;
  819. int num = 0;
  820. foreach (clsDataPoint d in HistoricalData)
  821. {
  822. if (d.Field.Field == field)
  823. {
  824. if (d.Field.Field == clsDataPointField.VideoDataFields.AVERAGE_RATING)
  825. num++;
  826. else
  827. ret += d.Delta;
  828. }
  829. }
  830. if (field == clsDataPointField.VideoDataFields.AVERAGE_RATING)
  831. return num;
  832. else
  833. return ret;
  834. }
  835.  
  836. // private methods
  837. private double _calc_average_new_rating(double old_average, double new_average, double old_raters, double new_raters)
  838. {
  839. System.Diagnostics.Debug.Print((((new_average * new_raters) - (old_average * old_raters)) / (new_raters - old_raters)).ToString());
  840. return ((new_average * new_raters) - (old_average * old_raters)) / (new_raters - old_raters);
  841. }
  842.  
  843. // public properties
  844. public List<clsVideoEntry> InitialDataSet
  845. {
  846. get { return _initial_dataset; }
  847. set { _initial_dataset = value; }
  848. }
  849. public Dictionary<string, List<clsDataPoint>> HistoricalDataPoints
  850. {
  851. get { return _historical_data; }
  852. set { _historical_data = value; }
  853. }
  854. }
  855.  
  856. /* Utility class to check/edit video settings */
  857. // sorry, this it too dangerous to give out - EDITED
  858.  
  859. /* Utility class to handle multiple clsSettingsManager objects */
  860. public class clsED
  861. {
  862. private List<clsSettingsManager> _settings_managers = new List<clsSettingsManager>();
  863. private clsFileLogger _logger = null;
  864. private clsSettings _settings;
  865. private bool _in_action = false;
  866.  
  867. public clsED()
  868. {
  869. }
  870. public clsED(clsSettings Settings)
  871. {
  872. _settings = Settings;
  873. _settings.OnAccountAdded += new clsSettings.AccountAddedHandler(_settings_OnAccountAdded);
  874. _settings.OnAccountRemoved += new clsSettings.AccountRemovedHandler(_settings_OnAccountRemoved);
  875.  
  876. if (_settings.ED_Log_File != null && _settings.ED_Log_File != string.Empty)
  877. _logger = new clsFileLogger(_settings.ED_Log_File);
  878.  
  879. foreach (clsCredentials Account in _settings.Accounts)
  880. {
  881. if (Account.Password == "-")
  882. continue;
  883. clsSettingsManager sm = new clsSettingsManager(Account.Username, Account.Password);
  884. sm.OnException += new clsSettingsManager.ExceptionEventHandler(sm_OnException);
  885. sm.OnFailure += new clsSettingsManager.FailureEventHandler(sm_OnFailure);
  886. sm.OnStatusChange += new clsSettingsManager.StatusEventHandler(sm_OnStatusChange);
  887. sm.OnSuccess += new clsSettingsManager.SuccessEventHandler(sm_OnSuccess);
  888. _settings_managers.Add(sm);
  889. }
  890. }
  891.  
  892. void _settings_OnAccountRemoved(object sender, clsCredentials Account)
  893. {
  894. RemoveAccount(Account);
  895. }
  896. void _settings_OnAccountAdded(object sender, clsCredentials Account)
  897. {
  898. AddAccount(Account);
  899. }
  900.  
  901. void sm_OnSuccess(object sender, clsSettingsManager.FailureCodes f)
  902. {
  903. clsSettingsManager thisSM = (clsSettingsManager)sender;
  904. if (_logger != null)
  905. _logger.appendFile(DateTime.Now.ToString() + "-" + thisSM.Username + " Success @ " + f.ToString());
  906. }
  907. void sm_OnStatusChange(object sender, clsSettingsManager.InternalState s)
  908. {
  909. _in_action = false;
  910. if (s == clsSettingsManager.InternalState.idle)
  911. {
  912. foreach (clsSettingsManager sm in _settings_managers)
  913. {
  914. _in_action = (sm.State != clsSettingsManager.InternalState.idle);
  915. break;
  916. }
  917. }
  918. clsSettingsManager thisSM = (clsSettingsManager)sender;
  919. System.Diagnostics.Debug.WriteLine(thisSM.Username + " Status changed to " + s.ToString());
  920. }
  921. void sm_OnFailure(object sender, clsSettingsManager.FailureCodes f)
  922. {
  923. clsSettingsManager thisSM = (clsSettingsManager)sender;
  924. if (_logger != null)
  925. _logger.appendFile(DateTime.Now.ToString() + "-" + thisSM.Username + " Failed @ " + f.ToString());
  926. }
  927. void sm_OnException(object sender, Exception e)
  928. {
  929. clsSettingsManager thisSM = (clsSettingsManager)sender;
  930. if (_logger != null)
  931. _logger.appendFile(DateTime.Now.ToString() + "-" + thisSM.Username + " Exception: " + e.Message);
  932. }
  933.  
  934. private void AddAccount(clsCredentials Account)
  935. {
  936. foreach (clsSettingsManager sm in _settings_managers)
  937. if (sm.Username == Account.Username)
  938. return;
  939.  
  940. if (Account.Password == "-")
  941. return;
  942.  
  943. clsSettingsManager newSM = new clsSettingsManager(Account.Username, Account.Password);
  944. newSM.OnSuccess += new clsSettingsManager.SuccessEventHandler(sm_OnSuccess);
  945. newSM.OnFailure += new clsSettingsManager.FailureEventHandler(sm_OnFailure);
  946. newSM.OnException += new clsSettingsManager.ExceptionEventHandler(sm_OnException);
  947. newSM.OnStatusChange += new clsSettingsManager.StatusEventHandler(sm_OnStatusChange);
  948.  
  949. _settings_managers.Add(newSM);
  950. }
  951. private void RemoveAccount(clsCredentials Account)
  952. {
  953. int i = 0;
  954. while (i < _settings_managers.Count)
  955. {
  956. if (_settings_managers[i].Username == Account.Username)
  957. {
  958. _settings_managers[i].Dispose();
  959. _settings_managers.RemoveAt(i);
  960. }
  961. else
  962. i++;
  963. }
  964. }
  965. public void ChangeAccountRatings(clsCredentials Account, string VideoID, bool Enable)
  966. {
  967. foreach (clsSettingsManager sm in _settings_managers)
  968. {
  969. if (sm.Username == Account.Username)
  970. {
  971. if (Enable)
  972. sm.EnableVideo(VideoID);
  973. else
  974. sm.DisableVideo(VideoID);
  975. _raise_started_action();
  976. return;
  977. }
  978. }
  979. }
  980. public void ChangeAccountRatings(clsCredentials Account, List<clsVideoEntry> Videos, bool Enable)
  981. {
  982. foreach (clsSettingsManager sm in _settings_managers)
  983. {
  984. if (sm.Username == Account.Username)
  985. {
  986. if (Enable)
  987. {
  988. foreach (clsVideoEntry e in Videos)
  989. sm.EnableVideo(e.VideoID);
  990. }
  991. else
  992. {
  993. foreach (clsVideoEntry e in Videos)
  994. sm.DisableVideo(e.VideoID);
  995. }
  996. _raise_started_action();
  997. return;
  998. }
  999. }
  1000. }
  1001.  
  1002. public delegate void StartedActionEventHandler(object Sender);
  1003. public event StartedActionEventHandler OnStartedAction;
  1004. private void _raise_started_action()
  1005. {
  1006. if (_in_action == true)
  1007. return;
  1008. if (OnStartedAction != null)
  1009. OnStartedAction(this);
  1010. }
  1011.  
  1012. public List<clsSettingsManager> SettingsManagers { get { return _settings_managers; } }
  1013. public clsFileLogger Logger { set { _logger = value; } }
  1014. }
  1015.  
  1016. /* Utility class that write to file */
  1017. public class clsFileLogger
  1018. {
  1019. private string _file_name = string.Empty;
  1020.  
  1021. public delegate void ErrorEventHandler(object sender, IOException e);
  1022. public event ErrorEventHandler OnError;
  1023. protected virtual void Error(IOException e)
  1024. {
  1025. if (OnError != null)
  1026. OnError(this, e);
  1027. }
  1028.  
  1029. public clsFileLogger(string filename)
  1030. {
  1031. _file_name = filename;
  1032. }
  1033.  
  1034. public void Initialize()
  1035. {
  1036. try
  1037. {
  1038. FileStream file = new FileStream(_file_name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  1039. file.Close();
  1040. }
  1041. catch (IOException e)
  1042. { Error(e); }
  1043. }
  1044. public string readFile()
  1045. {
  1046. FileStream file = new FileStream(_file_name, FileMode.OpenOrCreate, FileAccess.Read);
  1047. StreamReader sr = new StreamReader(file);
  1048. string ret = sr.ReadToEnd();
  1049. sr.Close();
  1050. file.Close();
  1051. return ret;
  1052. }
  1053. public void appendFile(string data)
  1054. {
  1055. StreamWriter sw = File.AppendText(_file_name);
  1056. sw.WriteLine(data);
  1057. sw.Flush();
  1058. sw.Close();
  1059. }
  1060. }
  1061.  
  1062. /*Class to maintain settings */
  1063. public class clsSettings
  1064. {
  1065. private const string DEFAULT_FILENAME = "TubeGuardian.log";
  1066. private const string DEFAULT_SETTINGS_FILENAME = "TubeGuardian.settings";
  1067. private string _settings_file_name;
  1068.  
  1069. private int _collect_interval;
  1070. private string _collect_log_file;
  1071. private int _analyzer_relevant_entries;
  1072. private double _analyzer_vote_threshold;
  1073. private bool _analyzer_disable_affected;
  1074. private bool _analyzer_disable_all;
  1075. private int _analyzer_disable_duration;
  1076. private string _ed_log_file;
  1077.  
  1078. // Gatherer settings
  1079. public int Collect_Interval { get { return _collect_interval; } set { _collect_interval = value; _event_settings_changed(); } }
  1080. public string Collect_Log_File { get { return _collect_log_file; } set { _collect_log_file = value; _event_settings_changed(); } }
  1081.  
  1082. // Account settings
  1083. public List<clsCredentials> Accounts { get; private set; }
  1084.  
  1085. // Analyzer settings
  1086. public int Analyzer_Relevant_Entries { get { return _analyzer_relevant_entries; } set { _analyzer_relevant_entries = value; _event_settings_changed(); } }
  1087. public double Analyzer_Vote_Threshold { get { return _analyzer_vote_threshold; } set { _analyzer_vote_threshold = value; _event_settings_changed(); } }
  1088. public bool Analyzer_Disable_Affected { get { return _analyzer_disable_affected; } set { _analyzer_disable_affected = value; _event_settings_changed(); } }
  1089. public bool Analyzer_Disable_All { get { return _analyzer_disable_all; } set { _analyzer_disable_all = value; _event_settings_changed(); } }
  1090. public int Analyzer_Disable_Duration { get { return _analyzer_disable_duration; } set { _analyzer_disable_duration = value; _event_settings_changed(); } }
  1091.  
  1092. // ED settinds
  1093. public string ED_Log_File { get { return _ed_log_file; } set { _ed_log_file = value; _event_settings_changed(); } }
  1094.  
  1095. public clsSettings(string filename)
  1096. {
  1097. _settings_file_name = filename;
  1098. LoadSettings();
  1099. }
  1100. public clsSettings() : this(DEFAULT_SETTINGS_FILENAME) { }
  1101.  
  1102. public void LoadSettings()
  1103. {
  1104. this.Collect_Interval = 10;
  1105. this.Collect_Log_File = DEFAULT_FILENAME;
  1106. this.Analyzer_Vote_Threshold = 2.5;
  1107. this.Analyzer_Relevant_Entries = 10;
  1108. this.Analyzer_Disable_Duration = 300;
  1109. this.Analyzer_Disable_All = false;
  1110. this.Analyzer_Disable_Affected = true;
  1111. this.Accounts = new List<clsCredentials>();
  1112. this.ED_Log_File = DEFAULT_FILENAME;
  1113.  
  1114. FileStream file = new FileStream(_settings_file_name, FileMode.OpenOrCreate, FileAccess.Read);
  1115. StreamReader sr = new StreamReader(file);
  1116. string data = sr.ReadToEnd();
  1117. sr.Close();
  1118. file.Close();
  1119.  
  1120. if (data == string.Empty)
  1121. return;
  1122. else
  1123. {
  1124. string accounts = _get_section(data, "ACCOUNTS {", "}");
  1125. if (accounts != null)
  1126. {
  1127. string[] individuals = accounts.Split(',');
  1128. foreach (string acct in individuals)
  1129. {
  1130. string[] UP = acct.Split(':');
  1131. AddAccount(new clsCredentials(UP[0], UP[1]));
  1132. }
  1133. }
  1134. try { this.Collect_Interval = int.Parse(_get_section(data, "C_I{", "}")); }
  1135. catch { }
  1136. try { this.Collect_Log_File = _get_section(data, "C_L{", "}"); }
  1137. catch { }
  1138. try { this.Analyzer_Vote_Threshold = double.Parse(_get_section(data, "A_V{", "}")); }
  1139. catch { }
  1140. try { this.Analyzer_Relevant_Entries = int.Parse(_get_section(data, "A_R{", "}")); }
  1141. catch { }
  1142. try { this.Analyzer_Disable_All = bool.Parse(_get_section(data, "A_D1{", "}")); }
  1143. catch { }
  1144. try { this.Analyzer_Disable_Affected = bool.Parse(_get_section(data, "A_D2{", "}")); }
  1145. catch { }
  1146. try { this.ED_Log_File = _get_section(data, "ED_L{", "}"); }
  1147. catch { }
  1148. }
  1149. }
  1150. public void SaveSettings()
  1151. {
  1152. string data = null;
  1153.  
  1154. data = "ACCOUNTS {";
  1155. foreach (clsCredentials account in Accounts)
  1156. data += account.Username + ":" + account.Password + ",";
  1157. data = data.Substring(0, data.Length - 1);
  1158. data += "}";
  1159.  
  1160. data += "C_I{" + this.Collect_Interval.ToString() + "}\n";
  1161. data += "C_L{" + this.Collect_Log_File + "}\n";
  1162. data += "A_V{" + this.Analyzer_Vote_Threshold.ToString() + "}\n";
  1163. data += "A_R{" + this.Analyzer_Relevant_Entries.ToString() + "}\n";
  1164. data += "A_D1{" + this.Analyzer_Disable_All.ToString() + "}\n";
  1165. data += "A_D2{" + this.Analyzer_Disable_Affected.ToString() + "}\n";
  1166. data += "A_D3{" + this.Analyzer_Disable_Duration.ToString() + "}\n";
  1167. data += "ED_L{" + this.ED_Log_File + "}\n";
  1168.  
  1169. File.WriteAllText(_settings_file_name, data);
  1170. }
  1171. public void AddAccount(clsCredentials Account)
  1172. {
  1173. if (Account == null)
  1174. return;
  1175. RemoveAccount(Account);
  1176. this.Accounts.Add(Account);
  1177. _event_account_added(Account);
  1178. }
  1179. public void RemoveAccount(clsCredentials Account)
  1180. {
  1181. if (Account == null)
  1182. return;
  1183.  
  1184. int i = 0;
  1185. while (i < Accounts.Count)
  1186. {
  1187. if (Accounts[i].Username == Account.Username)
  1188. {
  1189. _event_account_removed(Accounts[i]);
  1190. Accounts.RemoveAt(i);
  1191. }
  1192. else i++;
  1193. }
  1194. }
  1195. private string _get_section(string data, string left, string right)
  1196. {
  1197. int i_left = -1;
  1198. int i_right = -1;
  1199.  
  1200. i_left = data.IndexOf(left);
  1201.  
  1202. if (i_left == -1)
  1203. return null;
  1204.  
  1205. i_left += left.Length;
  1206.  
  1207. i_right = data.IndexOf(right, i_left);
  1208.  
  1209. if (i_right == -1)
  1210. return null;
  1211.  
  1212. return data.Substring(i_left, i_right - i_left);
  1213. }
  1214.  
  1215. // public methods
  1216. public clsCredentials GetAccountByUsername( string username )
  1217. {
  1218. foreach (clsCredentials acct in Accounts)
  1219. if (acct.Username == username)
  1220. return acct;
  1221. return null;
  1222. }
  1223.  
  1224. // events!
  1225. public delegate void AccountAddedHandler(object sender, clsCredentials Account);
  1226. public event AccountAddedHandler OnAccountAdded;
  1227. private void _event_account_added(clsCredentials Account)
  1228. {
  1229. if (OnAccountAdded != null)
  1230. OnAccountAdded(this, Account);
  1231. }
  1232.  
  1233. public delegate void AccountRemovedHandler(object sender, clsCredentials Account);
  1234. public event AccountRemovedHandler OnAccountRemoved;
  1235. private void _event_account_removed(clsCredentials Account)
  1236. {
  1237. if (OnAccountRemoved != null)
  1238. OnAccountRemoved(this, Account);
  1239. }
  1240.  
  1241. public delegate void SettingsChangedHandler(object sender);
  1242. public event SettingsChangedHandler OnSettingsChanged;
  1243. private void _event_settings_changed()
  1244. {
  1245. if (OnSettingsChanged != null)
  1246. OnSettingsChanged(this);
  1247. }
  1248. }
  1249.  
  1250. }
Add Comment
Please, Sign In to add comment