Plurk 服務 .net C# 開發週記(一) - Plurk基本功能

  • 8950
  • 0
  • 2009-10-23

因為遇到個需求需要將一個線上服務與 Plurk 介接
花了些時間Google了一下…只找到了 plurkapi ...

=========================================

沒用過 Plurk 的同學們 (用過,請跳過)

首先~請先看下文   

[教學] 什麼是Plurk「噗浪」? 如何使用撲浪?

=========================================

因為遇到個需求需要將一個線上服務與 Plurk 介接

花了些時間Google了一下…只找到了 plurkapi 

這個我會的 API(C#) 來用, 非官方~而且 2008/June

Final Release, 沒新的了?!

反倒是 PHP 的寫一堆…找的超心酸的~ 言歸正傳…

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Plurk 常做的事其實很簡單

就三件事

1. 發噗 (addMessage)

 

        /// <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;
        }

2. 讀噗 (getMessages)

 

        /// <summary>
        /// Get plurk messages of a specific user
        /// </summary>
        /// <param name="uid">The uid of user that you get the plurk messages</param>
        /// <param name="date_from">Start datetime in UTC format. DateTime.ToUniversalTime().ToString("s")</param>
        /// <param name="data_offset">End datetime in UTC format. DateTime.ToUniversalTime().ToString("s")</param>
        /// <param name="fetch_responses">True if you wanna get all responses for all plurk messages</param>
        /// <returns>A collection with plurk messages</returns>
        public PlurkMessages getMessages(int uid, string date_from, string data_offset, bool fetch_responses)
        {
            string jsonString = "";
            string url = "http://www.plurk.com/TimeLine/getPlurks";
            string data = "";
            PlurkMessages messages = new PlurkMessages();
 
            if (date_from == "")
            {
                data = web.GetPage(url + "?user_id=" + uid.ToString(), null, ref cookie, true);
            }
            else
            {
                data = web.GetPage(url + "?" + string.Format("user_id={0}&from_date={1}&offset={2}", uid, HttpUtility.UrlEncode(date_from), HttpUtility.UrlEncode(data_offset)), null, ref cookie, true);
            }
 
            if (data == "") return null;
            if (data == "[]") return messages;
            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 + "}";
                }
                jsonString = new Regex("new Date\\((.*)\\)").Replace(jsonString, "$1");
                PlurkMessage msg = new PlurkMessage(jsonString);
                if (fetch_responses) msg.responses = this.getMessagesResponses(msg.plurk_id);
                messages.Add(msg);
            }
            return messages;
        }

3. 回噗 (respondMessage)

 

        /// <summary>
        /// Respond a 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</param>
        /// <param name="plurk_id">The id of plurk message that you wanna respond</param>
        /// <param name="uid">The user's uid that you wanna add plurk message</param>
        /// <returns>True if it was successful, otherwise false</returns>
        public bool respondMessage(string lang, string qualifier, string content, int plurk_id, int uid)
        {
            string query = "";
            string data = "";
 
            query = string.Format("posted={0}&qualifier={1}&content={2}&lang={3}&p_uid={4}&plurk_id={5}&uid={6}",
                                    HttpUtility.UrlEncode(DateTime.Now.ToUniversalTime().ToString("s")),
                                    qualifier, HttpUtility.UrlEncode(content), lang, uid, plurk_id, uid);
            data = web.GetPage("http://www.plurk.com/Responses/add?" + query, null, ref cookie, true);
            return true;
        }