OSDN Git Service

SearchApi通信関連のみ実装(UI,データ保持は未実装)
authorkiri_feather <kiri_feather@users.sourceforge.jp>
Sat, 26 Dec 2009 15:32:35 +0000 (15:32 +0000)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 18 Feb 2012 14:10:34 +0000 (23:10 +0900)
git-svn-id: http://svn.sourceforge.jp/svnroot/tween/trunk@41 e39ad16e-3079-482e-bb30-4b4d378143b6

Tween/MySocket.vb
Tween/StatusDictionary.vb
Tween/Twitter.vb

index c3bd713..e6f392b 100644 (file)
@@ -41,17 +41,14 @@ Public NotInheritable Class MySocket
         ReqGETBinary
         ReqPOST
         ReqPOSTEncode
-        'ReqPOSTEncodeProtoVer1
-        'ReqPOSTEncodeProtoVer2
-        'ReqPOSTEncodeProtoVer3
         ReqGETForwardTo
         ReqGETFile
         ReqGETFileUp
         ReqGETFileRes
         ReqGETFileDll
-        'ReqGetNoCache
         ReqPOSTAPI
         ReqGetAPI
+        ReqGetAPINoAuth
         ReqGetApp
     End Enum
 
@@ -104,7 +101,7 @@ Public NotInheritable Class MySocket
                 webReq.Timeout = timeOut
             End If
 
-            If reqType <> REQ_TYPE.ReqPOSTAPI AndAlso reqType <> REQ_TYPE.ReqGetAPI Then
+            If reqType <> REQ_TYPE.ReqPOSTAPI AndAlso reqType <> REQ_TYPE.ReqGetAPI AndAlso reqType <> REQ_TYPE.ReqGetAPINoAuth Then
                 webReq.CookieContainer = cCon
                 webReq.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
             End If
@@ -123,9 +120,6 @@ Public NotInheritable Class MySocket
             If reqType = REQ_TYPE.ReqPOST OrElse _
                reqType = REQ_TYPE.ReqPOSTEncode OrElse _
                reqType = REQ_TYPE.ReqPOSTAPI Then
-                'reqType = REQ_TYPE.ReqPOSTEncodeProtoVer1 OrElse _
-                'reqType = REQ_TYPE.ReqPOSTEncodeProtoVer2 OrElse _
-                'reqType = REQ_TYPE.ReqPOSTEncodeProtoVer3 OrElse _
                 webReq.Method = "POST"
 
                 If DefaultTimeOut = timeOut Then
@@ -140,42 +134,16 @@ Public NotInheritable Class MySocket
                     Case REQ_TYPE.ReqPOST
                         webReq.ContentType = "application/x-www-form-urlencoded"
                     Case REQ_TYPE.ReqPOSTEncode
-                        '                        webReq.ContentType = "application/x-www-form-urlencoded; charset=" + _enc.WebName
                         webReq.ContentType = "application/x-www-form-urlencoded"
                         webReq.Accept = "text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*"
-                        'Case REQ_TYPE.ReqPOSTEncodeProtoVer1
-                        '    webReq.ContentType = "application/x-www-form-urlencoded; charset=" + _enc.WebName
-                        '    webReq.Accept = "text/javascript, text/html, application/xml, text/xml, */*"
-                        '    webReq.Headers.Add("x-prototype-version", "1.6.0.1")
-                        '    webReq.Headers.Add("x-requested-with", "XMLHttpRequest")
-                        '    webReq.Headers.Add("Accept-Language", "ja,en-us;q=0.7,en;q=0.3")
-                        '    webReq.Headers.Add("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.7")
-                        'Case REQ_TYPE.ReqPOSTEncodeProtoVer2
-                        '    webReq.ContentType = "application/x-www-form-urlencoded; charset=" + _enc.WebName
-                        '    webReq.Accept = "text/javascript, text/html, application/xml, text/xml, */*"
-                        '    webReq.Headers.Add("x-prototype-version", "1.6.0.1")
-                        '    webReq.Headers.Add("x-requested-with", "XMLHttpRequest")
-                        '    webReq.Headers.Add("Accept-Language", "ja,en-us;q=0.7,en;q=0.3")
-                        '    webReq.Headers.Add("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.7")
-                        'Case REQ_TYPE.ReqPOSTEncodeProtoVer3
-                        '    webReq.ContentType = "application/x-www-form-urlencoded; charset=" + _enc.WebName
-                        '    webReq.Accept = "application/json, text/javascript, */*"
-                        '    webReq.Headers.Add("x-prototype-version", "1.6.0.1")
-                        '    webReq.Headers.Add("x-requested-with", "XMLHttpRequest")
-                        '    webReq.Headers.Add("Accept-Language", "ja,en-us;q=0.7,en;q=0.3")
-                        '    webReq.Headers.Add("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.7")
                     Case REQ_TYPE.ReqPOSTAPI
                         webReq.ContentType = "application/x-www-form-urlencoded"
                         webReq.Accept = "text/html, */*"
-                        'webReq.Headers.Add("X-Twitter-Client", "Tween")
-                        'webReq.Headers.Add("X-Twitter-Client-Version", _version)
-                        'webReq.Headers.Add("X-Twitter-Client-URL", "http://www.asahi-net.or.jp/~ne5h-ykmz/tween.xml")
                         webReq.Headers.Add(HttpRequestHeader.Authorization, _cre)
                 End Select
                 Dim st As Stream = webReq.GetRequestStream()
                 st.Write(dataB, 0, dataB.Length)
                 st.Close()
-                'ElseIf reqType = REQ_TYPE.ReqGET Or reqType = REQ_TYPE.ReqGetNoCache Then
             ElseIf reqType = REQ_TYPE.ReqGET Then
                 webReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
                 webReq.Headers.Add("Accept-Language", "ja,en-us;q=0.7,en;q=0.3")
@@ -187,14 +155,14 @@ Public NotInheritable Class MySocket
             ElseIf reqType = REQ_TYPE.ReqGetAPI Then
                 webReq.ContentType = "application/x-www-form-urlencoded"
                 webReq.Accept = "text/html, */*"
-                'webReq.Headers.Add("X-Twitter-Client", "Tween")
-                'webReq.Headers.Add("X-Twitter-Client-Version", _version)
-                'webReq.Headers.Add("X-Twitter-Client-URL", "http://www.asahi-net.or.jp/~ne5h-ykmz/tween.xml")
                 webReq.Headers.Add(HttpRequestHeader.Authorization, _cre)
+            ElseIf reqType = REQ_TYPE.ReqGetAPINoAuth Then
+                webReq.ContentType = "application/x-www-form-urlencoded"
+                webReq.Accept = "text/html, */*"
             End If
 
             Using webRes As HttpWebResponse = CType(webReq.GetResponse(), HttpWebResponse)
-                If reqType <> REQ_TYPE.ReqPOSTAPI AndAlso reqType <> REQ_TYPE.ReqGetAPI Then
+                If reqType <> REQ_TYPE.ReqPOSTAPI AndAlso reqType <> REQ_TYPE.ReqGetAPI AndAlso reqType <> REQ_TYPE.ReqGetAPINoAuth Then
                     SyncLock cConLock
                         For Each ck As Cookie In webRes.Cookies
                             If ck.Domain.StartsWith(".") Then
@@ -214,8 +182,7 @@ Public NotInheritable Class MySocket
 
                 Using strm As Stream = webRes.GetResponseStream()
                     Select Case reqType
-                        'Case REQ_TYPE.ReqGET, REQ_TYPE.ReqPOST, REQ_TYPE.ReqPOSTEncode, REQ_TYPE.ReqPOSTEncodeProtoVer1, REQ_TYPE.ReqPOSTEncodeProtoVer2, REQ_TYPE.ReqPOSTEncodeProtoVer3, REQ_TYPE.ReqGetNoCache, REQ_TYPE.ReqPOSTAPI, REQ_TYPE.ReqGetAPI, REQ_TYPE.ReqGetApp
-                        Case REQ_TYPE.ReqGET, REQ_TYPE.ReqPOST, REQ_TYPE.ReqPOSTEncode, REQ_TYPE.ReqPOSTAPI, REQ_TYPE.ReqGetAPI, REQ_TYPE.ReqGetApp
+                        Case REQ_TYPE.ReqGET, REQ_TYPE.ReqPOST, REQ_TYPE.ReqPOSTEncode, REQ_TYPE.ReqPOSTAPI, REQ_TYPE.ReqGetAPI, REQ_TYPE.ReqGetApp, REQ_TYPE.ReqGetAPINoAuth
                             Dim rtStr As String
                             Using sr As New StreamReader(strm, _enc)
                                 rtStr = sr.ReadToEnd()
index addec55..328f0cc 100644 (file)
@@ -25,7 +25,7 @@ Imports System.Collections.Generic
 Imports System.Collections.ObjectModel
 Imports Tween.TweenCustomControl
 Imports System.Text.RegularExpressions
-
+Imports System.Web.HttpUtility
 
 Public NotInheritable Class PostClass
     Private _Nick As String
@@ -1108,8 +1108,56 @@ Public NotInheritable Class TabClass
     Private _tmpIds As List(Of TempolaryId)
     Private _tabName As String = ""
     Private _tabType As TabUsageType = TabUsageType.Undefined
-    Private _tabFilename As String = ""
-    'Private rwLock As New System.Threading.ReaderWriterLock()   'フィルタ用
+
+    'Search query
+    Private _searchLang As String = ""
+    Private _searchSource As String = ""
+    Private _searchLinks As Boolean = False
+    Private _searchWords As String = ""
+
+    Public Property SearchLang() As String
+        Get
+            Return _searchLang
+        End Get
+        Set(ByVal value As String)
+            _searchLang = value
+        End Set
+    End Property
+    Public Property SearchSource() As String
+        Get
+            Return _searchSource
+        End Get
+        Set(ByVal value As String)
+            _searchSource = value
+        End Set
+    End Property
+    Public Property SearchLinks() As Boolean
+        Get
+            Return _searchLinks
+        End Get
+        Set(ByVal value As Boolean)
+            _searchLinks = value
+        End Set
+    End Property
+    Public Property SearchWords() As String
+        Get
+            Return _searchWords
+        End Get
+        Set(ByVal value As String)
+            _searchWords = value.Trim
+        End Set
+    End Property
+    Public ReadOnly Property SearchQuery() As String
+        Get
+            Dim qry As String = ""
+            If String.IsNullOrEmpty(_searchWords) Then Return ""
+            qry = "q=" + _searchWords
+            If Not String.IsNullOrEmpty(_searchSource) Then qry += " source:" + _searchSource
+            If _searchLinks Then qry += " filter:links"
+            If Not String.IsNullOrEmpty(_searchLang) Then qry += "&lang=" + _searchLang
+            Return UrlEncode(qry)
+        End Get
+    End Property
 
     Private Structure TempolaryId
         Public Id As Long
@@ -1389,16 +1437,6 @@ Public NotInheritable Class TabClass
         End Set
     End Property
 
-    <Xml.Serialization.XmlIgnore()> _
-    Public Property TabFilename() As String
-        Get
-            Return _tabFilename
-        End Get
-        Set(ByVal value As String)
-            _tabFilename = value
-        End Set
-    End Property
-
     Public Property TabType() As TabUsageType
         Get
             Return _tabType
index 7421cb7..8ce1b68 100644 (file)
@@ -3176,6 +3176,118 @@ Public Module Twitter
         Return ""
     End Function
 
+    Public Function GetSearch(ByVal read As Boolean, _
+                            ByVal tabName As String) As String
+
+        If _endingFlag Then Return ""
+
+        Dim retMsg As String = ""
+        Dim resStatus As String = ""
+        Dim sck As MySocket = CreateSocket()
+        Const SEARCH_HOST As String = "search."
+        Const SEARCH_PATH As String = "/search.atom"
+
+        Dim tb As TabClass = TabInformations.GetInstance.Tabs(tabName)
+        If tb Is Nothing Then Return ""
+        Dim query As String = tb.SearchQuery
+        If query = "" Then Return ""
+
+        retMsg = DirectCast(sck.GetWebResponse(_protocol + SEARCH_HOST + _hubServer + SEARCH_PATH + "?" + query, resStatus, MySocket.REQ_TYPE.ReqGetAPINoAuth, userAgent:="Tween"), String)
+
+        If retMsg = "" Then
+            If resStatus.StartsWith("Err: BadRequest") Then
+                Return "Invalid search query."
+            ElseIf resStatus.StartsWith("Err: 420") Then    '暫定:2010/1/18よりAPI制限で420返るらしい
+                Return "Maybe, the requests reached API limit."
+            Else
+                Return resStatus
+            End If
+        End If
+
+        Dim arIdx As Integer = -1
+        Dim dlgt(_countApi) As GetIconImageDelegate    'countQueryに合わせる
+        Dim ar(_countApi) As IAsyncResult              'countQueryに合わせる
+        Dim xdoc As New XmlDocument
+        Try
+            xdoc.LoadXml(retMsg)
+        Catch ex As Exception
+            TraceOut(retMsg)
+            Return "Invalid ATOM!"
+        End Try
+        Dim nsmgr As New XmlNamespaceManager(xdoc.NameTable)
+        nsmgr.AddNamespace("search", "http://www.w3.org/2005/Atom")
+        For Each xentryNode As XmlNode In xdoc.DocumentElement.SelectNodes("/search:feed/search:entry", nsmgr)
+            Dim xentry As XmlElement = CType(xentryNode, XmlElement)
+            Dim post As New PostClass
+            Try
+                post.Id = Long.Parse(xentry.Item("id").InnerText.Split(":"c)(2))
+                post.PDate = DateTime.Parse(xentry.Item("published").InnerText)
+                '本文
+                post.Data = xentry.Item("title").InnerText
+                'Source取得(htmlの場合は、中身を取り出し)
+                post.Source = xentry.Item("twitter:source").InnerText
+                post.InReplyToId = 0
+                post.InReplyToUser = ""
+                post.IsFav = False
+
+                '以下、ユーザー情報
+                Dim xUentry As XmlElement = CType(xentry.SelectSingleNode("./search:author", nsmgr), XmlElement)
+                post.Uid = 0
+                post.Name = xUentry.Item("name").InnerText.Split(" "c)(0).Trim
+                post.Nickname = xUentry.Item("name").InnerText.Substring(post.Name.Length).Trim
+                If post.Nickname.Length > 2 Then
+                    post.Nickname = post.Nickname.Substring(1, post.Nickname.Length - 2)
+                Else
+                    post.Nickname = post.Name
+                End If
+                post.ImageUrl = CType(xentry.SelectSingleNode("./search:link[@type='image/png']", nsmgr), XmlElement).GetAttribute("href")
+                post.IsProtect = False
+                post.IsMe = post.Name.ToLower.Equals(_uid)
+
+                'HTMLに整形
+                post.OriginalData = CreateHtmlAnchor(post.Data, post.ReplyToList)
+                post.Data = HttpUtility.HtmlDecode(post.Data)
+                'Source整形
+                If post.Source.StartsWith("<") Then
+                    Dim rgS As New Regex(">(?<source>.+)<")
+                    Dim mS As Match = rgS.Match(post.Source)
+                    If mS.Success Then
+                        post.Source = mS.Result("${source}")
+                    End If
+                End If
+
+                post.IsRead = read
+                post.IsReply = post.ReplyToList.Contains(_uid)
+
+                post.IsOwl = False
+                If post.IsMe AndAlso Not read AndAlso _readOwnPost Then post.IsRead = True
+
+                post.IsDm = False
+            Catch ex As Exception
+                TraceOut(retMsg)
+                Continue For
+            End Try
+
+            '非同期アイコン取得&StatusDictionaryに追加
+            arIdx += 1
+            dlgt(arIdx) = New GetIconImageDelegate(AddressOf GetIconImage)
+            ar(arIdx) = dlgt(arIdx).BeginInvoke(post, Nothing, Nothing)
+        Next
+
+        'アイコン取得完了待ち
+        For i As Integer = 0 To arIdx
+            Try
+                dlgt(i).EndInvoke(ar(i))
+            Catch ex As Exception
+                '最後までendinvoke回す(ゾンビ化回避)
+                ex.Data("IsTerminatePermission") = False
+                Throw
+            End Try
+        Next
+
+        Return ""
+    End Function
+
     Public Function GetDirectMessageApi(ByVal read As Boolean, _
                             ByVal gType As WORKERTYPE) As String
         If _endingFlag Then Return ""