コード例 #1
0
ファイル: ZohoReader.cs プロジェクト: pvalenta/zohosync
        /// <summary>
        /// authenticate
        /// </summary>
        public bool Authenticate()
        {
            // write to console
            Program.OutputWrite("Zoho: authenticate - ");

            // authenticate
            var webClient = new SlowWebClient();
            webClient.Encoding = Encoding.UTF8;
            string response = webClient.DownloadString(string.Format(LOGIN_API, ConfigReader.ZohoLogin, ConfigReader.ZohoPassword));

            // split by lines
            var lines = response.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            // result
            var result = bool.Parse((from l in lines
                                     where l.StartsWith("RESULT=")
                                     select l.Split('=')[1]).FirstOrDefault());
            if (!result)
            {
                // failed
                var cause = (from l in lines
                             where l.StartsWith("CAUSE=")
                             select l.Split('=')[1]).FirstOrDefault();
                Program.OutputWriteLine(" failed. [" + cause + "]");
                return false;
            }
            else
            {

                // auth token
                this.token = (from l in lines
                              where l.StartsWith("AUTHTOKEN=")
                              select l.Split('=')[1]).FirstOrDefault();

                // done
                Program.OutputWriteLine(" done. [" + this.token + "]");
                return true;
            }
        }
コード例 #2
0
        /// <summary>
        /// send data
        /// </summary>
        /// <param name="list">smart emailing list</param>
        /// <param name="data">data</param>
        public void SendData(string list, XElement data)
        {
            Program.OutputWrite("SmartEmailing: building xml request - ");

            // build xml header
            var root = new XElement("xmlrequest",
                new XElement("username", ConfigReader.SmartEmailLogin),
                new XElement("usertoken", ConfigReader.SmartEmailToken),
                new XElement("requesttype", "sm_lists"),
                new XElement("requestmethod", "SynchronizeList"),
                new XElement("details",
                    new XElement("list_name", list),
                    new XElement("replytoemail", ConfigReader.SmartEmailReplyTo),
                    new XElement("ownername", ConfigReader.SmartEmailOwnerName),
                    new XElement("owneremail", ConfigReader.SmartEmailOwnerEmail),
                    new XElement("notMatchingContacts", "setUnsubscribed"),
                    new XElement("contacts")));
            var contacts = root.Descendants("contacts").First();

            // let's build data in
            contacts.Add(data.Elements("record")
                .Where(e => e.Element("email").Value.Length > 0)
                .Select(e => new XElement("details",
                    new XElement("emailaddress", e.Element("email").Value),
                    new XElement("format", "html"),
                    new XElement("confirmed", "yes"),
                    new XElement("status", "active"),
                    new XElement("customfields",
                        new XElement("item",
                            new XElement("fieldid", "2"),
                            new XElement("value", e.Element("firstName").Value)),
                        new XElement("item",
                            new XElement("fieldid", "3"),
                            new XElement("value", e.Element("lastName").Value))))));

            // convert done
            Program.OutputWriteLine("done [" + contacts.Elements().Count() + " records]");

            if (Program.WriteFiles)
            {
                // save it
                string file = Path.Combine(Directory.GetCurrentDirectory(), list + ".xml");
                root.Save(file);
                Program.OutputWriteLine("SmartEmailing: export saved to '" + file + "' done.");
            }

            Program.OutputWrite("SmartEmailing: submit to server - ");

            if (Program.TestOnly)
            {
                Program.OutputWriteLine("skipped (test only).");
            }
            else
            {
                // submit it
                var webClient = new SlowWebClient();
                webClient.Encoding = Encoding.UTF8;

                try
                {
                    var response = webClient.UploadString(API_URL, root.ToString());

                    // parse
                    root = XElement.Parse(response);
                    if (root.Element("status").Value.Equals("success", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Program.OutputWriteLine("done.");
                    }
                    else
                    {
                        Program.OutputWriteLine("failed.");
                    }
                }
                catch (WebException ex)
                {
                    Program.OutputWriteLine("failed (" + ex.Message + ")");
                }
                catch (Exception ex)
                {
                    Program.OutputWriteLine("failed (" + ex.Message + ")");
                }
            }
        }
コード例 #3
0
ファイル: ZohoReader.cs プロジェクト: pvalenta/zohosync
        /// <summary>
        /// get data
        /// </summary>
        /// <param name="zohoTable">zoho table</param>
        /// <returns>Xml Element with all data</returns>
        public XElement GetData(string zohoTable)
        {
            // main xml
            XElement root = new XElement("response");

            // request table
            Program.OutputWrite("Zoho: request table '" + zohoTable + "'");
            var webClient = new SlowWebClient();
            var counter = 0;
            for (int i = 0; i < 1000000; i = i + 200)
            {
                Program.OutputWrite(".");

                // request
                webClient.Encoding = Encoding.UTF8;

                string content = webClient.DownloadString(string.Format(TABLE_API, zohoTable, token, i, i + 199));

                // parse it
                XElement partRoot = XElement.Parse(content);
                if (partRoot.Elements("nodata").Any())
                {
                    Program.OutputWriteLine(" done [" + counter + " records]");
                    break;
                }
                else if (partRoot.Elements("error").Any())
                {
                    Program.OutputWriteLine(" failed.");
                    var error = partRoot.Elements("error").First();
                    error.Remove();
                    root.Add(error);
                    break;
                }
                else if (partRoot.Descendants(zohoTable).Any())
                {
                    var result = partRoot.Descendants(zohoTable).First().Elements("row").ToList();
                    foreach (var r in result)
                    {
                        // hodnoty
                        var row = new XElement("record");
                        var email = r.Elements("FL").Where(e => e.Attribute("val").Value == "Email").FirstOrDefault();
                        if (email != null) row.Add(new XElement("email", email.Value));
                        else row.Add(new XElement("email", string.Empty));
                        var fname = r.Elements("FL").Where(e => e.Attribute("val").Value == "First Name").FirstOrDefault();
                        if (fname != null) row.Add(new XElement("firstName", fname.Value));
                        else row.Add(new XElement("firstName", string.Empty));
                        var lname = r.Elements("FL").Where(e => e.Attribute("val").Value == "Last Name").FirstOrDefault();
                        if (lname != null) row.Add(new XElement("lastName", lname.Value));
                        else row.Add(new XElement("lastName", string.Empty));

                        counter++;
                        root.Add(row);
                    }
                }
            }

            if (Program.WriteFiles)
            {
                // save it
                string file = Path.Combine(Directory.GetCurrentDirectory(), zohoTable + ".xml");
                root.Save(file);
                Program.OutputWriteLine("Zoho: export saved to '" + file + "' done.");
            }

            return root;
        }