Example #1
0
        protected bool FetchUpdates()
        {
            try
            {
                string data = this.snapconnector.GetUpdates();

                if (this.dlSnaps)
                {
                    JsonClasses.Snap[] snaps = this.snapconnector.GetSnaps(data);
                    //process snaps
                    foreach (JsonClasses.Snap snap in snaps)
                    {
                        if (snap.st < 2 && !string.IsNullOrEmpty(snap.sn))
                        {
                            bool dlSnap = this.connector.AddSnap(snap);
                            if (dlSnap)
                            {
                                try
                                {
                                    byte[] image = this.snapconnector.GetMedia(snap.id);
                                    if (!SnapConnector.isMedia(image))
                                    {
                                        image = SnapConnector.decryptECB(image);
                                    }
                                    string filename = this.SaveMedia(image, snap);
                                    this.NotifyTray(snap, filename);
                                }
                                catch (WebException w)
                                {
                                    //too bad
                                    HttpWebResponse resp = w.Response as HttpWebResponse;
                                    if (resp.StatusCode != HttpStatusCode.Gone && resp.StatusCode != HttpStatusCode.InternalServerError)
                                    {
                                        throw w;
                                    }
                                }
                            }
                        }
                    }
                }
                if (this.dlStories)
                {
                    JsonClasses.Story[] stories = this.snapconnector.GetStories(data);
                    //process stories
                    foreach (JsonClasses.Story story in stories)
                    {
                        if (story.media_type < 2)
                        {
                            bool dlStory = this.connector.AddStory(story);
                            if (dlStory)
                            {
                                try
                                {
                                    byte[] image    = this.snapconnector.GetStoryMedia(story.media_id, story.media_key, story.media_iv);
                                    string filename = this.SaveMedia(image, story);
                                    if (!string.IsNullOrEmpty(filename))
                                    {
                                        this.NotifyTray(story, filename);
                                    }
                                }
                                catch (WebException w)
                                {
                                    //too bad
                                    if (w.Status != WebExceptionStatus.ProtocolError)
                                    {
                                        throw w;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    HttpWebResponse resp = wex.Response as HttpWebResponse;
                    if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        MessageBox.Show("Invalid credentials", "Auth error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (resp.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        //shit happens
                        return(true);
                    }
                    else
                    {
                        MessageBox.Show(wex.Message, "WebException in listener thread", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    this.autoStart = false;
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                //TODO: log
                MessageBox.Show(ex.Message, "General Exception in listener thread", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.autoStart = false;
                return(false);
            }
            return(true);
        }