· 7 years ago · Feb 22, 2018, 06:08 AM
1# -*- coding: utf-8 -*-
2import scrapy
3from scrapy import Request
4from scrapy.exceptions import CloseSpider
5import oauth2 as oauth
6
7
8CONSUMER_KEY = "xxxx"
9CONSUMER_SECRET = "xxxx"
10ACCESS_KEY = "xxxx"
11ACCESS_SECRET = "xxxx"
12
13
14class TwitterSpider(scrapy.Spider):
15 name = 'twitter'
16 timeline_endpoint = "https://api.twitter.com/1.1/statuses/user_timeline.json"
17
18 def create_authorizatioin_header(self, endpoint):
19 consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
20 token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
21 params = {
22 'oauth_consumer_key': CONSUMER_KEY,
23 'oauth_nonce':oauth.generate_nonce(),
24 'oauth_timestamp':str(int(time.time())),
25 'oauth_token': ACCESS_KEY,
26 'oauth_version':"1.0",
27 }
28
29 req = oauth.Request(method="POST", url=endpoint, parameters=params)
30 req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
31 signature = req['oauth_signature'].decode("utf-8")
32 print(signature)
33 header = 'OAuth oauth_consumer_key="'+ CONSUMER_KEY +'",oauth_nonce="'+ params['oauth_nonce']+'",oauth_signature="'+ signature +'",oauth_signature_method="HMAC-SHA1",oauth_timestamp="'+ params['oauth_timestamp'] +'",oauth_token="'+ ACCESS_KEY +'",oauth_version="1.0"'
34 print(header)
35 return header
36
37 def start_requests(self):
38 yield Request(self.timeline_endpoint, method="POST", headers={
39 'Content-Type': 'application/x-www-form-urlencoded',
40 'Authorization': self.create_authorizatioin_header(self.timeline_endpoint)
41 })
42
43 def parse(self, response):
44 print(response.text)