Plurk 服務 .net C# 開發週記(二) - Plurk開發遇到的鳥問題

  • 8372
  • 0
  • 2009-10-23

開發遇到的鳥問題
1. PM 說來抓抓 Plurk 朋友的個資, 原函式是有提供, 經N次測試後, 發現 Plurk 已關閉該功能… XD 殘念
2. 瞎~無法發[噗]… 原來是因為少傳了UID這個參數(看粗體), 造成無法新增噗文~(這還是去找PHP的API才發現的, 昏倒 XD)

開發遇到的鳥問題

1. PM 說來抓抓 Plurk 朋友的個資, 原函式是有提供, 經N次測試後, 發現 Plurk 已關閉該功能… XD 殘念

        /// <summary>
        /// Gets a friend collection
        /// </summary>
        /// <param name="uid">The user's uid that you wanna get friends</param>
        /// <returns>A friends collection</returns>
        public PlurkFriends getFriends(int uid) 
        {
            string jsonString = "";
            PlurkFriends friends = new PlurkFriends();
            string data = "";
            data = web.GetPage("http://www.plurk.com/Users/getFriends?user_id=" + uid.ToString(), null, ref cookie, true);
            if (data == "") return null;
            if (data == "[]") return friends;
            data = dejsonize(data);
 
            string[] arrayData = Regex.Split(data, "}, {");
            foreach (string item in arrayData)
            {
                jsonString = item;
                if (jsonString[0] != '{')
                {
                    jsonString = "{" + jsonString;
                }
 
                if (jsonString[jsonString.Length - 1] != '}')
                {
                    jsonString = jsonString + "}";
                }
                friends.Add(new PlurkFriend(jsonString));
            }
            return friends;
        }

2. 瞎~無法發[噗]… 原來是因為少傳了UID這個參數(看粗體), 造成無法新增噗文~(這還是去找PHP的API才發現的, 昏倒 XD)

OLD

        /// <summary>
        /// Add plurk message
        /// </summary>
        /// <param name="lang">The plurk language</param>
        /// <param name="qualifier">The plurk qualifier</param>
        /// <param name="content">The content of plurk message to be posted</param>
        /// <param name="alowComments">true if this plurk message allows comments, false otherwise</param>
        /// <param name="limited_to">Limite this plurk message to some friends. Format: [uid,uid,uid]. Otherwise set with ""</param>
        /// <returns>true if it was posted, otherwise false</returns>
        public bool addMessage(string lang, string qualifier, string content, bool alowComments, string limited_to)
        {
            string query = "";
            string data = "";
            string error_match = "";
 
            query = string.Format("posted={0}&qualifier={1}&content={2}&lang={3}&no_comments={4}",
                                    HttpUtility.UrlEncode(DateTime.Now.ToUniversalTime().ToString("s")), qualifier, HttpUtility.UrlEncode(content), lang,
                                    !alowComments ? "1" : "0");
            if (limited_to != "")
            {
                query += "&limited_to=" + HttpUtility.UrlEncode(limited_to);
            }
            data = web.GetPage("http://www.plurk.com/TimeLine/addPlurk?" + query, null, ref cookie, true);
            if (data.IndexOf("/anti-flood/") != -1 || cookie == null)
                return false;
            try
            {
                error_match = new Regex("\"error\":\\s(\\S+)}").Matches(data)[0].Groups[1].Value;
            }
            catch 
            {
                return false;
            }
            if (error_match != "null")
            {
                return false;
            }
            return true;
        }

NEW

        /// <summary>
        /// Add plurk message
        /// </summary>
        /// <param name="lang">The plurk language</param>
        /// <param name="qualifier">The plurk qualifier</param>
        /// <param name="content">The content of plurk message to be posted</param>
        /// <param name="alowComments">true if this plurk message allows comments, false otherwise</param>
        /// <param name="limited_to">Limite this plurk message to some friends. Format: [uid,uid,uid]. Otherwise set with ""</param>
        /// <param name="uid">The user's uid that you wanna add plurk message</param>
        /// <returns>true if it was posted, otherwise false</returns>
        public bool addMessage(string lang, string qualifier, string content, bool alowComments, string limited_to, int uid)
        {
            string query = "";
            string data = "";
            string error_match = "";
 
            query = string.Format("posted={0}&qualifier={1}&content={2}&lang={3}&no_comments={4}&uid={5}",
                                    HttpUtility.UrlEncode(DateTime.Now.ToUniversalTime().ToString("s")), qualifier, HttpUtility.UrlEncode(content), lang,
                                    !alowComments ? "1" : "0", uid);
            if (limited_to != "")
            {
                query += "&limited_to=" + HttpUtility.UrlEncode(limited_to);
            }
            data = web.GetPage("http://www.plurk.com/TimeLine/addPlurk?" + query, null, ref cookie, true);
            if (data.IndexOf("/anti-flood/") != -1 || cookie == null)
                return false;
            try
            {
                error_match = new Regex("\"error\":\\s(\\S+)}").Matches(data)[0].Groups[1].Value;
            }
            catch
            {
                return false;
            }
            if (error_match != "null")
            {
                return false;
            }
            return true;
        }

 

上述是目前遇到~比較鳥的問題…