Repository navigation
Expand file tree
/
Copy pathtwitter_script.py
More file actions
97 lines (73 loc) · 3.26 KB
/
Copy pathtwitter_script.py
File metadata and controls
97 lines (73 loc) · 3.26 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
# !/usr/bin/env python
from utilities import *
from credentials import *
from constants import *
import tweepy
class TwitterScript:
def __init__(self):
self.application_name = "twitter"
self.twitter_api_key = TWITTER_API_KEY
self.twitter_api_secret_key = TWITTER_API_SECRET_KEY
self.twitter_access_token = TWITTER_ACCESS_TOKEN
self.twitter_access_token_secret = TWITTER_ACCESS_TOKEN_SECRET
self.twitter_user_id = TWITTER_USER_ID
self.auth = tweepy.OAuthHandler(self.twitter_api_key, self.twitter_api_secret_key)
self.auth.set_access_token(self.twitter_access_token, self.twitter_access_token_secret)
self.api = tweepy.API(self.auth)
def get_user_by_id(self, user_id, api):
try:
user_information = api.get_user(user_id)
except tweepy.TweepError as e:
logging.warning("Error: " + str(e))
user_information = ""
return user_information
def get_followers_id(self, user_id, api):
try:
user_followers = api.followers_ids(user_id)
except tweepy.TweepError as e:
logging.warning("Error: " + str(e))
user_followers = ""
return user_followers
def get_friends_id(self, user_id, api):
try:
user_friends = api.friends_ids(user_id)
except tweepy.TweepError as e:
logging.warning("Error: " + str(e))
user_friends = ""
return user_friends
def run_script(self):
logging.info('twitter script is running...')
create_directory(PD_SCRIPT_ROOT_LOGS_PATH + "/" + self.application_name)
api = self.api
user = self.get_user_by_id(self.twitter_user_id, api)
followers = self.get_followers_id(self.twitter_user_id, api)
friends = self.get_friends_id(self.twitter_user_id, api)
logging.info('creating twitter log file')
file_name = create_timestamped_and_named_file_name(self.application_name)
file = open(file_name, "w", encoding="utf-8")
logging.info('writing in twitter log file...')
# processing of followers
if len(str(user)) > 0 and len(str(followers)) > 0:
file.write(user.screen_name + " twitter user have " + str(len(followers)) + " followers")
file.write("\n\n")
file.write("List of followers' ids of " + user.screen_name + " : \n")
for follower in followers:
file.write(str(follower))
file.write("\n")
file.write("\n\n\n\n")
# processing of friends
if len(str(user)) > 0 and len(str(friends)) > 0:
file.write(user.screen_name + " twitter user have " + str(len(friends)) + " friends")
file.write("\n\n")
file.write("List of friends' ids of " + user.screen_name + " : \n")
for friend in friends:
file.write(str(friend))
file.write("\n")
logging.info('writing in twitter log file done')
file.close()
# opens the file for reading only in binary format in order to upload
file = open(file_name, "rb")
upload_file_to_server_ftp(file, file_name, self.application_name)
file.close()
logging.info('twitter script is terminated')