Esempio n. 1
0
        void ParseAtom(XElement atom)
        {
            var n = atom.Name.Namespace;

            foreach (var entry in atom.Descendants(n + "entry"))
            {
                Func <XName, string> ev = nn => {
                    var e = entry.Element(nn);
                    if (e != null)
                    {
                        return(e.Value.Trim());
                    }
                    else
                    {
                        return("");
                    }
                };

                var rawId = ev(n + "id");

                var msg = GetMessageByRawId(rawId);

                //Console.WriteLine (entry);

                if (msg.BodyHtml.Length == 0)
                {
                    msg.Subject     = ParseSubject(ev(n + "title"));
                    msg.Url         = entry.Element(n + "link").Attribute("href").Value;
                    msg.PublishTime = InternetTime.Parse(ev(n + "published"));

                    var contentE = entry.Element(n + "content");

                    var body = "";
                    if (contentE != null)
                    {
                        body = contentE.Value.Trim();
                    }
                    if (string.IsNullOrEmpty(body) && contentE != null && contentE.Attribute("src") != null)
                    {
                        body = contentE.Attribute("src").Value;
                    }
                    msg.BodyHtml = FilterBodyHtml(body);

                    var a = entry.Element(n + "author");
                    if (a != null)
                    {
                        var nameE = a.Element(n + "name");
                        if (nameE != null)
                        {
                            var name = ParseName(nameE.Value.Trim());
                            msg.From = FilterFrom(name);
                        }
                    }

                    Save(msg);
                }
                //Console.WriteLine ("AM: " + msg.Subject);
            }
            Console.WriteLine("U: Parsed ATOM");
        }
Esempio n. 2
0
        protected override void DoUpdate()
        {
            var url = "http://api.twitter.com/1/statuses/home_timeline.xml";

            var doc = Api("GET", new Uri(url));

            foreach (var status in doc.Descendants("status"))
            {
                Func <XName, string> ev = n =>
                {
                    var e = status.Element(n);
                    if (e != null)
                    {
                        return(e.Value.Trim());
                    }
                    else
                    {
                        return("");
                    }
                };
                var rawId = ev("id");
                var msg   = GetMessageByRawId(rawId);

                if (msg.BodyHtml.Length == 0)
                {
                    msg.Subject     = status.Element("text").Value;
                    msg.From        = status.Element("user").Element("screen_name").Value;
                    msg.Url         = "http://twitter.com/" + msg.From + "/status/" + msg.RawId;
                    msg.PublishTime = InternetTime.Parse(ev("created_at"));
                    msg.BodyHtml    = FilterBody(status.Element("text").Value);

                    Save(msg);
                }
            }
            Console.WriteLine("U: Parsed Twitter Home");
        }
Esempio n. 3
0
        void ParseRss(XElement rss)
        {
            var ns = rss.Elements().First().Name.Namespace;

            foreach (var item in rss.Descendants(ns + "item"))
            {
                Func <string, string> ev = n => {
                    foreach (var ch in item.Elements())
                    {
                        if (ch.Name.LocalName.ToLowerInvariant() == n)
                        {
                            return(ch.Value.Trim());
                        }
                    }
                    return("");
                };
                Func <XElement, string, XElement> c = (p, n) => {
                    foreach (var ch in p.Elements())
                    {
                        if (ch.Name.LocalName == n)
                        {
                            return(ch);
                        }
                    }
                    return(null);
                };

                var rawId = ev("guid");
                if (rawId.Length == 0)
                {
                    rawId = ev("link");
                    if (rawId.Length == 0)
                    {
                        rawId = ev("pubDate");
                    }
                }
                var msg = GetMessageByRawId(rawId);

                //Console.WriteLine (item);

                if (msg.BodyHtml.Length == 0)
                {
                    msg.Subject = ParseSubject(ev("title"));
                    msg.Url     = ev("link");

                    msg.BodyHtml = FilterBodyHtml(ev("description"));

                    var content = ev("encoded");

                    if (content != "")
                    {
                        msg.BodyHtml += "<div>" + content + "</div>";
                    }

                    var d = ev("pubdate");
                    if (d.Length == 0)
                    {
                        d = ev("date");
                    }
                    msg.PublishTime = InternetTime.Parse(d);

                    var a = c(item, "author");
                    if (a != null)
                    {
                        msg.From = FilterFrom(ParseName(a.Value));
                    }

                    var enc = item.Element("enclosure");
                    if (enc != null)
                    {
                        var url = enc.Attribute("url");
                        if (url != null)
                        {
                            msg.MediaUrl = url.Value;
                        }
                    }

                    Save(msg);
                }
                //Console.WriteLine ("RM: " + msg.Subject);
            }
            Console.WriteLine("U: Parsed RSS");
        }
Esempio n. 4
0
        string ProcessFeed(JsonObject feed)
        {
            foreach (JsonObject datum in (JsonArray)feed["data"])
            {
                var id          = datum.GetString("id");
                var messageText = datum.GetString("message");
                var description = datum.GetString("description");
                var name        = datum.GetString("name");
                var picture     = datum.GetString("picture");
                var link        = datum.GetString("link");
                if (link == "http://www.facebook.com")
                {
                    link = "";
                }
                var upTime = InternetTime.Parse(datum.GetString("created_time"));
                var fr     = (string)datum["from"]["name"];

                if (link.IndexOf("apps.facebook.com") >= 0)
                {
                    continue;
                }

                var m = GetMessageByRawId(id);

                m.From        = fr;
                m.PublishTime = upTime;

                var subj = name;

                if (subj.Length == 0)
                {
                    subj = Html.ReplaceHtmlEntities(messageText.TruncateWords(100));
                }
                if (subj.Length == 0)
                {
                    subj = Html.ReplaceHtmlEntities(description.TruncateWords(100));
                }
                if (subj.Length == 0 && picture.Length != 0)
                {
                    subj = "Picture";
                }
                if (subj.Length == 0)
                {
                    Console.WriteLine(datum.ToString());
                }
                m.Subject = subj;

                var html = "<div>";

                if (name.Length > 0)
                {
                    html += "<h2>" + Html.Encode(name) + "</h2>";
                }
                if (picture.Length > 0 && link.Length > 0)
                {
                    html += "<img src=\"" + Html.Encode(picture) + "\" />";
                    m.Url = link;
                }
                if (description.Length > 0)
                {
                    html += "<blockquote>" + Html.Encode(description) + "</blockquote>";
                }

                html += Html.MakeLinks(messageText);

                html += "</div><div>";


                if (datum.ContainsKey("comments"))
                {
                    var comments = "";
                    var cs       = (JsonArray)(datum["comments"]["data"]);

                    foreach (var c in cs)
                    {
                        var cfr = c["from"].GetString("name");
                        var cm  = c.GetString("message");

                        comments += "<h4>" + Html.Encode(cfr) + "</h4><blockquote>" + Html.Encode(cm) + "</blockquote>";
                    }
                    html += comments;
                }

                html += "</div>";

                m.BodyHtml = html;

                Save(m);
            }

            var nextUrl = (string)((JsonPrimitive)feed["paging"]["next"]);

            return(nextUrl);
        }