Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Type-ish class to hold youtube usernames, and passwords */
- public class clsCredentials
- {
- public clsCredentials(string Username, string Password) { this.Username = Username; this.Password = Password; }
- public clsCredentials() { }
- public string Username
- {
- get;
- set;
- }
- public string Password
- {
- get;
- set;
- }
- }
- /* Type-ish class to hold data points (old/new value + data field) */
- public class clsDataPoint
- {
- public clsDataPoint()
- {
- this.Old = this.New = 0;
- this.Field = new clsDataPointField();
- this.Time = DateTime.Now;
- }
- public clsDataPoint(double OldValue, double NewValue, clsDataPointField DataField, string VideoID)
- {
- this.Old = OldValue;
- this.New = NewValue;
- this.Field = DataField;
- this.Time = DateTime.Now;
- this.VideoID = VideoID;
- }
- public override string ToString()
- {
- string[] values = {Time.ToShortDateString(), Time.ToShortTimeString(), VideoID, Field.Field.ToString(), Old.ToString(), New.ToString() };
- return string.Join(",", values);
- }
- public clsDataPointField Field { get; set; }
- public double Old { get; set; }
- public double New { get; set; }
- public double Delta { get { return New - Old; } }
- public DateTime Time { get; set; }
- public string VideoID { get; set; }
- }
- /* Type-ish class to hold data field values and convert to string equivalents */
- public class clsDataPointField
- {
- public enum VideoDataFields
- {
- UNKNOWN = -1,
- VIEWS,
- RATERS,
- AVERAGE_RATING,
- COMMENT_COUNT,
- FAVORITED_COUNT
- }
- public clsDataPointField() { this.Field = VideoDataFields.UNKNOWN; }
- public clsDataPointField(VideoDataFields f) { this.Field = f; }
- public VideoDataFields Field { get; set; }
- public override string ToString()
- {
- switch (this.Field)
- {
- case VideoDataFields.AVERAGE_RATING:
- return "Average Rating";
- case VideoDataFields.COMMENT_COUNT:
- return "Comment Count";
- case VideoDataFields.FAVORITED_COUNT:
- return "Favorited Count";
- case VideoDataFields.RATERS:
- return "Raters";
- case VideoDataFields.UNKNOWN:
- return "Unknown";
- case VideoDataFields.VIEWS:
- return "Views";
- default:
- return "Error";
- }
- }
- }
- /* Wrapper class to access the information needed from YouTubeEntry objects */
- public class clsVideoEntry
- {
- private YouTubeEntry _yt_entry = null;
- // constructor
- public clsVideoEntry(YouTubeEntry e)
- {
- _yt_entry = e;
- }
- public override string ToString()
- {
- string[] values = { Time.ToShortDateString(), Time.ToShortTimeString(), VideoID, Title, Raters.ToString(), AverageRating.ToString(), ViewsCount.ToString(), CommentCount.ToString(), FavoritedCount.ToString() };
- return string.Join(",", values);
- }
- // public properties
- public bool IsNull
- {
- get { return (_yt_entry == null); }
- }
- public int Raters
- {
- get { try { return _yt_entry.Rating.NumRaters; } catch { return 0; } }
- }
- public double AverageRating
- {
- get { try { return _yt_entry.Rating.Average; } catch { return 0; } }
- }
- public int ViewsCount
- {
- get { try { return int.Parse(_yt_entry.Statistics.ViewCount); } catch { return 0; } }
- }
- public int CommentCount
- {
- get { try { return _yt_entry.Comments.FeedLink.CountHint; } catch { return 0; } }
- }
- public int FavoritedCount
- {
- get { try { return int.Parse(_yt_entry.Statistics.FavoriteCount); } catch { return 0; } }
- }
- public string VideoID
- {
- get { try { return _yt_entry.VideoId; } catch { return string.Empty; } }
- }
- public string Title
- {
- get { try { return _yt_entry.Title.Text; } catch { return string.Empty; } }
- }
- public YouTubeEntry YouTubeEntry
- {
- get { return _yt_entry; }
- }
- public DateTime Time { get; set; }
- }
- /* Bot-ish class that contantly maintains an updated dictionary of clsDataPoint objects */
- public class clsStatMonger
- {
- // private data members
- private List<clsCredentials> _accounts = new List<clsCredentials>();
- private List<clsVideoFeedReader> _feed_readers = new List<clsVideoFeedReader>();
- private List<clsVideoEntry> _initial_dataset = new List<clsVideoEntry>();
- private List<clsVideoEntry> _current_dataset = new List<clsVideoEntry>();
- private Dictionary<string, List<clsDataPoint>> _historical_data = new Dictionary<string, List<clsDataPoint>>();
- private System.Timers.Timer _update_timer;
- private clsFileLogger _file_logger = null;
- // constructor
- public clsStatMonger(string DeveloperKey, string ApplicationName, List<clsCredentials> Credentials)
- {
- this.Enabled = false;
- this.Interval = 20 * 60 * 1000; // default 20 minutes
- _update_timer = new System.Timers.Timer();
- _update_timer.Enabled = false;
- _update_timer.Interval = this.Interval;
- _update_timer.Elapsed += new ElapsedEventHandler(_update_timer_Elapsed);
- _accounts = Credentials;
- foreach (clsCredentials c in _accounts)
- {
- clsVideoFeedReader new_feed = new clsVideoFeedReader(DeveloperKey, ApplicationName, c.Username);
- if (c.Password != string.Empty)
- new_feed.SetCredentials(c.Username, c.Password);
- new_feed.OnEntryFetched += new clsVideoFeedReader.EntryFetchedHandler(new_feed_OnEntryFetched);
- _feed_readers.Add(new_feed);
- }
- }
- // public methods
- public void Enable()
- {
- if (this.Enabled)
- return;
- this.Enabled = true;
- _update_videos();
- _update_timer.Interval = this.Interval;
- _update_timer.Enabled = true;
- }
- public void Disable()
- {
- this.Enabled = false;
- _update_timer.Enabled = false;
- }
- public void Update()
- {
- _update_videos();
- }
- // public events
- public delegate void EntryAddedEventHandler(object Sender, clsVideoEntry Entry);
- public event EntryAddedEventHandler OnEntryAdded;
- private void _entry_added(clsVideoEntry Entry)
- {
- if (_file_logger != null)
- _file_logger.appendFile(Entry.ToString());
- if (OnEntryAdded != null)
- OnEntryAdded(this, Entry);
- }
- public delegate void EntryUpdatedEventHandler(object Sender, clsDataPoint DataPoint, clsVideoEntry Entry);
- public event EntryUpdatedEventHandler OnEntryUpdated;
- private void _entry_updated(clsDataPoint DataPoint, clsVideoEntry Entry)
- {
- if (_file_logger != null)
- _file_logger.appendFile(DataPoint.ToString());
- if (OnEntryUpdated != null)
- OnEntryUpdated(this, DataPoint, Entry);
- }
- // private methods
- void _update_timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- if (this.Enabled == false)
- return;
- _update_timer.Interval = this.Interval;
- _update_videos();
- }
- private void new_feed_OnEntryFetched(object Sender, clsVideoEntry Entry)
- {
- clsVideoEntry initial_entry;
- if ((initial_entry = _GetEntryByIdFromList(_initial_dataset, Entry.VideoID)) == null)
- {
- _initial_dataset.Add(Entry);
- _current_dataset.Add(Entry);
- _entry_added(Entry);
- }
- else
- {
- clsVideoEntry CurrentEntry = _GetEntryByIdFromList(_current_dataset, Entry.VideoID);
- if (CurrentEntry == null)
- {
- _current_dataset.Add(Entry);
- _compare_entries(initial_entry, Entry);
- }
- else
- {
- _current_dataset.Remove(CurrentEntry);
- _current_dataset.Add(Entry);
- _compare_entries(CurrentEntry, Entry);
- }
- }
- }
- private void _compare_entries(clsVideoEntry OldEntry, clsVideoEntry NewEntry)
- {
- if (OldEntry.VideoID != NewEntry.VideoID)
- return;
- _compare_stat(OldEntry.AverageRating, NewEntry.AverageRating, clsDataPointField.VideoDataFields.AVERAGE_RATING, NewEntry);
- _compare_stat(OldEntry.CommentCount, NewEntry.CommentCount, clsDataPointField.VideoDataFields.COMMENT_COUNT, NewEntry);
- _compare_stat(OldEntry.FavoritedCount, NewEntry.FavoritedCount, clsDataPointField.VideoDataFields.FAVORITED_COUNT, NewEntry);
- _compare_stat(OldEntry.Raters, NewEntry.Raters, clsDataPointField.VideoDataFields.RATERS, NewEntry);
- _compare_stat(OldEntry.ViewsCount, NewEntry.ViewsCount, clsDataPointField.VideoDataFields.VIEWS, NewEntry);
- }
- private void _compare_stat(double Old, double New, clsDataPointField.VideoDataFields Field, clsVideoEntry Entry)
- {
- if (Old == New)
- return;
- else
- {
- clsDataPoint new_datapoint = new clsDataPoint(Old, New, new clsDataPointField(Field), Entry.VideoID);
- if (_historical_data.ContainsKey(Entry.VideoID))
- _historical_data[Entry.VideoID].Add(new_datapoint);
- else
- {
- List<clsDataPoint> new_dp_list = new List<clsDataPoint>();
- new_dp_list.Add(new_datapoint);
- _historical_data.Add(Entry.VideoID, new_dp_list);
- }
- _entry_updated(new_datapoint, Entry);
- }
- }
- private clsVideoEntry _GetEntryByIdFromList(List<clsVideoEntry> Entries, string VideoID)
- {
- int index = Entries.FindIndex(delegate(clsVideoEntry e) { return e.VideoID.Equals(VideoID); });
- return (index == -1) ? null : Entries[index];
- }
- private void _update_videos()
- {
- foreach (clsVideoFeedReader r in _feed_readers)
- if (!r.IsBusy)
- r.GetVideosModifiedSince(r.Updated);
- }
- // public properties
- public bool Enabled { get; private set; }
- public int Interval { get; set; }
- public List<clsVideoEntry> CurrentDataSet
- {
- get { return new List<clsVideoEntry>(_current_dataset); }
- }
- public List<clsVideoEntry> InitialDataSet
- {
- get { return new List<clsVideoEntry>(_initial_dataset); }
- }
- public Dictionary<string, List<clsDataPoint>> HistoricalDataPoints
- {
- get { return new Dictionary<string, List<clsDataPoint>>(_historical_data); }
- }
- public clsFileLogger FileLogger
- {
- get { return _file_logger; }
- set { _file_logger = value; }
- }
- }
Add Comment
Please, Sign In to add comment