@@ -84,89 +84,104 @@ class tyobot_common extends tyobot_base {
84
84
}
85
85
86
86
function get_url_string ($ url ) {
87
- if ( preg_match ( URL_WIKIPEDIA_TOPPAGE_LINK_REGEX , $ url ) ){
88
- //日本語URLが取得できていない場合。
89
- return false ;
90
- }
91
- $ str = "" ;
92
- $ heads = my_get_headers ($ url , 1 );
93
- if ( $ heads === FALSE ){ return '■get respons error. ' ; }
94
- if ( $ heads ['count ' ] > 0 ){
95
- if ( $ heads ['last-status ' ] == 301 || $ heads ['last-status ' ] == 302 ){
96
- if ( $ heads ['loop ' ]){
97
- return '■Redirecting has looped. ' ;
87
+ try {
88
+ if ( preg_match ( URL_WIKIPEDIA_TOPPAGE_LINK_REGEX , $ url ) ){
89
+ //日本語URLが取得できていない場合。
90
+ return false ;
91
+ }
92
+ $ str = "" ;
93
+ $ heads = my_get_headers ($ url , 1 );
94
+ if ( $ heads === FALSE ){ return '■get respons error. ' ; }
95
+ if ( $ heads ['count ' ] > 0 ){
96
+ if ( $ heads ['last-status ' ] == 301 || $ heads ['last-status ' ] == 302 ){
97
+ if ( $ heads ['loop ' ]){
98
+ return '■Redirecting has looped. ' ;
99
+ }
100
+ return '■It redirects it three times or more. ' ;
98
101
}
99
- return '■It redirects it three times or more. ' ;
102
+ $ url = $ heads [ 'last-location ' ];
103
+ $ str .= '▼Redirect to ' .$ url ."\n" ;
100
104
}
101
- $ url = $ heads [ 'last-location ' ];
102
- $ str .= '▼Redirect to ' .$ url ."\n" ;
103
- }
104
- if ( $ heads ['last-status ' ] != 200 ){
105
- return '■ ' .$ heads [ $ heads ['count ' ] ]['status-line ' ];
106
- }
107
- if (preg_match_all (URL_AMAZON_ITEM_LINK_REGEX , $ url , $ matches )) {
108
- $ asin = $ matches [2 ][0 ];
109
- $ item = $ this ->amazon ->ItemLookup ($ asin , Array ());
110
- if (is_array ($ item )) {
111
- $ str .= "【A】( " .$ item ['Item ' ][0 ]['ItemAttributes ' ]['Title ' ].") " .URL_AMAZON_TINY_URL .$ asin ."\n" ;
105
+ if ( $ heads ['last-status ' ] != 200 ){
106
+ return '■ ' .$ heads [ $ heads ['count ' ] ]['status-line ' ];
112
107
}
113
- } else if (preg_match_all (URL_NICONICO_WATCH_LINK_REGEX , $ url , $ matches )) {
114
- $ api_url = 'http://ext.nicovideo.jp/api/getthumbinfo/ ' .$ matches [1 ][0 ];
115
- $ xml = file_get_contents ($ api_url );
116
- $ Unserializer =& new \XML_Unserializer (Array ('parseAttributes ' => TRUE ));
117
- $ xml_status = $ Unserializer ->unserialize ($ xml );
118
- if ($ xml_status ) {
119
- $ api_array = $ Unserializer ->getUnserializedData ();
120
- if ($ api_array ['status ' ] == ok) {
121
- $ str .= '【ニコ】 ' .$ api_array ['thumb ' ]['title ' ].'[ ' .$ api_array ['thumb ' ]['length ' ].'](再生: ' .$ api_array ['thumb ' ]['view_counter ' ].") \n" ;
108
+ if (preg_match_all (URL_AMAZON_ITEM_LINK_REGEX , $ url , $ matches )) {
109
+ $ asin = $ matches [2 ][0 ];
110
+ $ item = $ this ->amazon ->ItemLookup ($ asin , Array ());
111
+ if (is_array ($ item )) {
112
+ $ str .= "【A】( " .$ item ['Item ' ][0 ]['ItemAttributes ' ]['Title ' ].") " .URL_AMAZON_TINY_URL .$ asin ."\n" ;
122
113
}
123
- }
124
- } else if (preg_match_all (URL_YOUTUBE_WATCH_LINK_REGEX , $ url , $ matches )) {
125
- $ entry = $ this ->google_youtube ->getVideoEntry ( $ matches [1 ][0 ] );
126
- $ str .= '【Y】 ' .$ entry ->getVideoTitle ().'[ ' .$ entry ->getVideoCategory ().'] ' ;
127
- }else if (preg_match_all (URL_GOOGLE_SPREADSHEETS_LINK_REGEX , $ url , $ matches )) {
128
- $ query = new \Zend_Gdata_Spreadsheets_DocumentQuery ();
129
- $ query ->setSpreadsheetKey ($ matches [1 ][0 ] );
130
- $ worksheet_feed = $ this ->google_spreadseets ->getWorksheetFeed ( $ query );
131
- $ str = '【G-s】 ' .$ worksheet_feed ->title ->text ;
132
- }else if (preg_match_all (URL_TWITTER_STATUS_LINK_REGEX , $ url , $ matches )) {
133
- $ twitter =& new \Services_Twitter (TWITTER_ID , TWITTER_PASS );
134
- $ status = $ twitter ->statuses ->show ($ matches [3 ][0 ]);
135
- $ str = '【Tw】 ' .$ status ->user ->name .'「 ' .$ status ->text .'」 ' ;
136
- }else {
137
- $ head = $ heads [$ heads ['count ' ]];
138
- $ ct = explode (' ' , $ head ['content-type ' ]);
139
- $ types = explode ('/ ' , str_replace ('; ' , '/ ' , $ ct [0 ]));
140
- switch ($ types [0 ]) {
141
- case 'text ' :
142
- if ($ types [1 ] == 'html ' ) {
143
- $ html = file_get_html ($ url );
144
- d (memory_get_usage ());
145
- $ titles = $ html ->find ('title ' , 0 );
146
- if (strlen ($ titles ->innertext )) {
147
- $ str .= to_short ('■ ' .preg_replace ('/\s\s+/ ' , ' ' , str_replace (array ("\r\n" , "\n" , "\r" ), '' , trim ($ this ->_2utf8 ($ titles ->innertext )))), URL_TITLE_MAX_LENGTH );
148
- $ str .= '[ ' .$ ct [0 ].'] ' ;
114
+ } else if (preg_match_all (URL_NICONICO_WATCH_LINK_REGEX , $ url , $ matches )) {
115
+ $ api_url = 'http://ext.nicovideo.jp/api/getthumbinfo/ ' .$ matches [1 ][0 ];
116
+ $ xml = file_get_contents ($ api_url );
117
+ $ Unserializer =& new \XML_Unserializer (Array ('parseAttributes ' => TRUE ));
118
+ $ xml_status = $ Unserializer ->unserialize ($ xml );
119
+ if ($ xml_status ) {
120
+ $ api_array = $ Unserializer ->getUnserializedData ();
121
+ if ($ api_array ['status ' ] == ok) {
122
+ $ str .= '【ニコ】 ' .$ api_array ['thumb ' ]['title ' ].'[ ' .$ api_array ['thumb ' ]['length ' ].'](再生: ' .$ api_array ['thumb ' ]['view_counter ' ].") \n" ;
123
+ }
124
+ }
125
+ } else if (preg_match_all (URL_YOUTUBE_WATCH_LINK_REGEX , $ url , $ matches )) {
126
+ $ entry = $ this ->google_youtube ->getVideoEntry ( $ matches [1 ][0 ] );
127
+ $ str .= '【Y】 ' .$ entry ->getVideoTitle ().'[ ' .$ entry ->getVideoCategory ().'] ' ;
128
+ }else if (preg_match_all (URL_GOOGLE_SPREADSHEETS_LINK_REGEX , $ url , $ matches )) {
129
+ $ query = new \Zend_Gdata_Spreadsheets_DocumentQuery ();
130
+ $ query ->setSpreadsheetKey ($ matches [1 ][0 ] );
131
+ $ worksheet_feed = $ this ->google_spreadseets ->getWorksheetFeed ( $ query );
132
+ $ str = '【G-s】 ' .$ worksheet_feed ->title ->text ;
133
+ }else if (preg_match_all (URL_TWITTER_STATUS_LINK_REGEX , $ url , $ matches )) {
134
+ $ twitter =& new \Services_Twitter (TWITTER_ID , TWITTER_PASS );
135
+ $ status = $ twitter ->statuses ->show ($ matches [3 ][0 ]);
136
+ $ str = '【Tw】 ' .$ status ->user ->name .'「 ' .$ status ->text .'」 ' ;
137
+ }else if (preg_match_all (URL_TWITTER_USER_LINK_REGEX , $ url , $ matches )) {
138
+ $ twitter =& new \Services_Twitter (TWITTER_ID , TWITTER_PASS );
139
+ $ user = $ twitter ->users ->show ($ matches [1 ][0 ]);
140
+ $ str = $ user ->name .' ( ' .$ user ->screen_name .') ' .$ user ->url .' http://twitter.com/ ' .$ user ->screen_name .' bio: ' .to_short ($ user ->description , 40 )."\n" ."followers: " .$ user ->followers_count .' following: ' .$ user ->friends_count .' status: ' .$ user ->statuses_count .' fav: ' .$ user ->favourites_count ;
141
+ }else {
142
+ $ head = $ heads [$ heads ['count ' ]];
143
+ $ ct = explode (' ' , $ head ['content-type ' ]);
144
+ $ types = explode ('/ ' , str_replace ('; ' , '/ ' , $ ct [0 ]));
145
+ switch ($ types [0 ]) {
146
+ case 'text ' :
147
+ if ($ types [1 ] == 'html ' ) {
148
+ $ html = file_get_html ($ url );
149
+ d (memory_get_usage ());
150
+ $ titles = $ html ->find ('title ' , 0 );
151
+ if (strlen ($ titles ->innertext )) {
152
+ $ str .= to_short ('■ ' .preg_replace ('/\s\s+/ ' , ' ' , str_replace (array ("\r\n" , "\n" , "\r" ), '' , trim ($ this ->_2utf8 ($ titles ->innertext )))), URL_TITLE_MAX_LENGTH );
153
+ $ str .= '[ ' .$ ct [0 ].'] ' ;
154
+ $ html ->clear ();
155
+ break ;
156
+ }
149
157
$ html ->clear ();
150
- break ;
151
158
}
152
- $ html ->clear ();
153
- }
154
- default :
155
- $ str .= "■ " .$ ct [0 ];
156
- if (!empty ($ head ['content-length ' ])) {
157
- $ str .= '( ' .ConvertUnit ($ head ['content-length ' ]).'B) ' ;
158
- }
159
- break ;
160
- case 'image ' :
161
- $ size = getimagesize ($ url );
162
- $ str .= "■ " .$ ct [0 ];
163
- if (!empty ($ head ['content-length ' ])) {
164
- $ str .= '( ' .ConvertUnit ($ head ['content-length ' ]).'B) ' ;
165
- }
166
- if ($ size [0 ] && $ size [1 ]) {
167
- $ str .= '( ' .$ size [0 ].'* ' .$ size [1 ].') ' ;
168
- }
159
+ default :
160
+ $ str .= "■ " .$ ct [0 ];
161
+ if (!empty ($ head ['content-length ' ])) {
162
+ $ str .= '( ' .ConvertUnit ($ head ['content-length ' ]).'B) ' ;
163
+ }
164
+ break ;
165
+ case 'image ' :
166
+ $ size = getimagesize ($ url );
167
+ $ str .= "■ " .$ ct [0 ];
168
+ if (!empty ($ head ['content-length ' ])) {
169
+ $ str .= '( ' .ConvertUnit ($ head ['content-length ' ]).'B) ' ;
170
+ }
171
+ if ($ size [0 ] && $ size [1 ]) {
172
+ $ str .= '( ' .$ size [0 ].'* ' .$ size [1 ].') ' ;
173
+ }
174
+ break ;
175
+ }
176
+ }
177
+ } catch (\Services_Twitter_Exception $ e ) {
178
+ switch ($ e ->getCode ()) {
179
+ case 404 :
180
+ case 403 :
181
+ $ this ->_notice ($ irc , 'this twitter request is ' .$ e ->getMessage (), $ data ->channel );
169
182
break ;
183
+ default :
184
+ $ this ->_notice ($ irc , 'this twitter request to code: ' .$ e ->getCode (), $ data ->channel );
170
185
}
171
186
}
172
187
return $ str ;
0 commit comments