(rctx *importer.RunContext)
| 299 | var _ importer.LongPoller = (*imp)(nil) |
| 300 | |
| 301 | func (im *imp) LongPoll(rctx *importer.RunContext) error { |
| 302 | clientId, secret, err := rctx.Credentials() |
| 303 | if err != nil { |
| 304 | return err |
| 305 | } |
| 306 | |
| 307 | acctNode := rctx.AccountNode() |
| 308 | accessToken := acctNode.Attr(importer.AcctAttrAccessToken) |
| 309 | accessSecret := acctNode.Attr(importer.AcctAttrAccessTokenSecret) |
| 310 | if accessToken == "" || accessSecret == "" { |
| 311 | return errors.New("access credentials not found") |
| 312 | } |
| 313 | oauthClient := &oauth.Client{ |
| 314 | TemporaryCredentialRequestURI: temporaryCredentialRequestURL, |
| 315 | ResourceOwnerAuthorizationURI: resourceOwnerAuthorizationURL, |
| 316 | TokenRequestURI: tokenRequestURL, |
| 317 | Credentials: oauth.Credentials{ |
| 318 | Token: clientId, |
| 319 | Secret: secret, |
| 320 | }, |
| 321 | } |
| 322 | accessCreds := &oauth.Credentials{ |
| 323 | Token: accessToken, |
| 324 | Secret: accessSecret, |
| 325 | } |
| 326 | |
| 327 | form := url.Values{"with": {"user"}} |
| 328 | req, _ := http.NewRequestWithContext(rctx.Context(), "GET", "https://userstream.twitter.com/1.1/user.json", nil) |
| 329 | req.Header.Set("Authorization", oauthClient.AuthorizationHeader(accessCreds, "GET", req.URL, form)) |
| 330 | req.URL.RawQuery = form.Encode() |
| 331 | |
| 332 | log.Printf("twitter: beginning long poll, awaiting new tweets...") |
| 333 | res, err := http.DefaultClient.Do(req) |
| 334 | if err != nil { |
| 335 | return err |
| 336 | } |
| 337 | defer res.Body.Close() |
| 338 | if res.StatusCode != 200 { |
| 339 | return errors.New(res.Status) |
| 340 | } |
| 341 | bs := bufio.NewScanner(res.Body) |
| 342 | for bs.Scan() { |
| 343 | line := strings.TrimSpace(bs.Text()) |
| 344 | if line == "" || strings.HasPrefix(line, `{"friends`) { |
| 345 | continue |
| 346 | } |
| 347 | log.Printf("twitter: long poll saw activity") |
| 348 | return nil |
| 349 | } |
| 350 | if err := bs.Err(); err != nil { |
| 351 | return err |
| 352 | } |
| 353 | return errors.New("twitter: got EOF without a tweet") |
| 354 | } |
| 355 | |
| 356 | func (r *run) errorf(format string, args ...interface{}) { |
| 357 | log.Printf("twitter: "+format, args...) |
nothing calls this directly
no test coverage detected