1 from models import * |
1 from models import * |
2 import datetime |
2 import datetime |
3 import email.utils |
3 import email.utils |
4 import json |
4 import json |
5 import logging |
5 import logging |
|
6 import os.path |
6 import sys |
7 import sys |
7 import twitter |
8 import twitter |
|
9 import twitter.oauth |
|
10 import twitter.oauth_dance |
8 import twitter_text |
11 import twitter_text |
9 import os.path |
|
10 import twitter.oauth |
|
11 |
12 |
12 |
13 |
13 def get_oauth_token(token_file_path=None): |
14 def get_oauth_token(token_file_path=None): |
14 |
15 |
15 if token_file_path and os.path.file_exists(token_file_path): |
16 if token_file_path and os.path.exists(token_file_path): |
16 logging.debug("reading token from file %s" % token_file_path) |
17 logging.debug("reading token from file %s" % token_file_path) |
17 return twitter.oauth.read_token_file(token_file_path) |
18 return twitter.oauth.read_token_file(token_file_path) |
18 #read access token info from path |
19 #read access token info from path |
19 |
20 |
20 if 'ACCESS_TOKEN_KEY' in dict() and 'ACCESS_TOKEN_SECRET' in dict() and ACCESS_TOKEN_KEY and ACCESS_TOKEN_SECRET: |
21 if 'ACCESS_TOKEN_KEY' in dict() and 'ACCESS_TOKEN_SECRET' in dict() and ACCESS_TOKEN_KEY and ACCESS_TOKEN_SECRET: |
21 return ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET |
22 return ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET |
22 |
23 |
23 return twitter.oauth_dance.oauth_dance(APPLICATION_NAME, CONSUMER_KEY, CONSUMER_SECRET, token_filename) |
24 return twitter.oauth_dance.oauth_dance(APPLICATION_NAME, CONSUMER_KEY, CONSUMER_SECRET, token_file_path) |
24 |
25 |
25 def parse_date(date_str): |
26 def parse_date(date_str): |
26 ts = email.utils.parsedate_tz(date_str) |
27 ts = email.utils.parsedate_tz(date_str) |
27 return datetime.datetime(*ts[0:7]) |
28 return datetime.datetime(*ts[0:7]) |
28 |
29 |
66 class TwitterProcessorException(Exception): |
67 class TwitterProcessorException(Exception): |
67 pass |
68 pass |
68 |
69 |
69 class TwitterProcessor(object): |
70 class TwitterProcessor(object): |
70 |
71 |
71 def __init__(self, json_dict, json_txt, session): |
72 def __init__(self, json_dict, json_txt, session, token_filename=None): |
72 |
73 |
73 if json_dict is None and json_txt is None: |
74 if json_dict is None and json_txt is None: |
74 raise TwitterProcessorException("No json") |
75 raise TwitterProcessorException("No json") |
75 |
76 |
76 if json_dict is None: |
77 if json_dict is None: |
85 |
86 |
86 if "id" not in self.json_dict: |
87 if "id" not in self.json_dict: |
87 raise TwitterProcessorException("No id in json") |
88 raise TwitterProcessorException("No id in json") |
88 |
89 |
89 self.session = session |
90 self.session = session |
|
91 self.token_filename = token_filename |
90 |
92 |
91 def __get_user(self, user_dict): |
93 def __get_user(self, user_dict): |
92 logging.debug("Get user : " + repr(user_dict)) |
94 logging.debug("Get user : " + repr(user_dict)) |
93 |
95 |
94 user_id = user_dict.get("id",None) |
96 user_id = user_dict.get("id",None) |
106 return user |
108 return user |
107 |
109 |
108 user_created_at = user_dict.get("created_at", None) |
110 user_created_at = user_dict.get("created_at", None) |
109 |
111 |
110 if user_created_at is None: |
112 if user_created_at is None: |
111 acess_token_key, access_token_secret = get_oauth_token() |
113 acess_token_key, access_token_secret = get_oauth_token(self.token_filename) |
112 t = twitter.Twitter(auth=twitter.OAuth(token_key, token_secret, CONSUMER_KEY, CONSUMER_SECRET)) |
114 t = twitter.Twitter(auth=twitter.OAuth(acess_token_key, access_token_secret, CONSUMER_KEY, CONSUMER_SECRET)) |
113 try: |
115 try: |
114 if user_id: |
116 if user_id: |
115 user_dict = t.users.show(user_id=user_id) |
117 user_dict = t.users.show(user_id=user_id) |
116 else: |
118 else: |
117 user_dict = t.users.show(screen_name=user_name) |
119 user_dict = t.users.show(screen_name=user_name) |