forked from geduldig/TwitterAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterOAuth.py
More file actions
59 lines (45 loc) · 1.83 KB
/
TwitterOAuth.py
File metadata and controls
59 lines (45 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
__author__ = "geduldig"
__date__ = "February 7, 2013"
__license__ = "MIT"
import os
class TwitterOAuth:
"""Optional class for retrieving Twitter credentials stored in a text file.
:param consumer_key: Twitter application consumer key
:param consumer_secret: Twitter application consumer secret
:param access_token_key: Twitter application access token key
:param access_token_secret: Twitter application access token secret
"""
def __init__(
self,
consumer_key,
consumer_secret,
access_token_key,
access_token_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.access_token_key = access_token_key
self.access_token_secret = access_token_secret
@classmethod
def read_file(cls, file_name=None):
"""Read OAuth credentials from a text file. File format:
consumer_key=YOUR_CONSUMER_KEY
consumer_secret=YOUR_CONSUMER_SECRET
access_token_key=YOUR_ACCESS_TOKEN
access_token_secret=YOUR_ACCESS_TOKEN_SECRET
:param file_name: File containing credentials or None (default) reads credentials
from TwitterAPI/credentials.txt
"""
if file_name is None:
path = os.path.dirname(__file__)
file_name = os.path.join(path, 'credentials.txt')
with open(file_name) as f:
oauth = {}
for line in f:
if '=' in line:
name, value = line.split('=', 1)
oauth[name.strip()] = value.strip()
return TwitterOAuth(
oauth['consumer_key'],
oauth['consumer_secret'],
oauth['access_token_key'],
oauth['access_token_secret'])