/// <summary> /// Create a new Medium object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="url">Initial value of the Url property.</param> /// <param name="sourceSiteID">Initial value of the SourceSiteID property.</param> public static Medium CreateMedium(global::System.Int32 id, global::System.String url, global::System.Int32 sourceSiteID) { Medium medium = new Medium(); medium.ID = id; medium.Url = url; medium.SourceSiteID = sourceSiteID; return medium; }
private static void addTweet(VisualizationEntities db, DateTime date, string handle, string tweet, string url) { counter++; Debug.Print("Counter: " + counter); HashSet<string> seenUrls = new HashSet<string>(); Debug.Print("Tweet: " + tweet); Regex linkParser = new Regex(@"\b(?:http://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase); string id = url.Split('/').Last(); DB2.Tweet newTweet = new DB2.Tweet() { TweetText = tweet, Date = date, Username = handle.Split(' ').First(), TweetID = id }; if (!db.Tweets.Select(i => i.TweetID).Contains(id)) { db.Tweets.AddObject(newTweet); db.SaveChanges(); } else { return; } foreach (Match m in linkParser.Matches(tweet)) { string fullUrl = ""; string uniqueUrl = ""; try { fullUrl = m.Value.ExpandUrl(); uniqueUrl = fullUrl.UnuiqeUrl(); } catch { continue; } if (fullUrl.Count() > 300 || db.Websites.Any(i => i.Url == fullUrl)) { continue; } Debug.Print("Website: " + fullUrl); string websiteUrl; var seen = db.Websites.Where(i => i.Url == uniqueUrl).SingleOrDefault(); if (seen != null) { seen.HitCount++; websiteUrl = seen.Url; db.SaveChanges(); } else { var page = new PageScraper(fullUrl); var website = new DB2.Website() { Url = page.Url, Title = page.Title() }; db.Websites.AddObject(website); db.SaveChanges(); foreach (var m2 in page.Media()) { if (m2.Link.Count() > 300 || db.Media.Any(i => i.Url == m2.Link)) { continue; } try { DB2.Medium media = new DB2.Medium() { Type = m2.Type, Url = m2.Link, SourceSiteID = website.ID }; if (m2.Type == "image") { var request = WebRequest.Create(m2.Link); using (var response = request.GetResponse()) using (var stream = response.GetResponseStream()) using (var b = System.Drawing.Bitmap.FromStream(stream)) { media.Width = b.Width; media.Height = b.Height; } } db.Media.AddObject(media); db.SaveChanges(); Debug.Print("Media element: " + m2.Link); } catch { } } websiteUrl = website.Url; } } db.SaveChanges(); }
/// <summary> /// Deprecated Method for adding a new object to the Media EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToMedia(Medium medium) { base.AddObject("Media", medium); }