Skip to content

Commit

Permalink
Added --friends option to perform a quick analysis on friends (lang+t…
Browse files Browse the repository at this point in the history
…imezone) 😇
  • Loading branch information
x0rz committed Feb 2, 2017
1 parent 6b2f2c2 commit 15c4a0d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The goal of this simple python script is to analyze a Twitter profile through it
- Sources used (mobile application, web browser, ...)
- Geolocations
- Most used hashtags, most retweeted users and most mentioned users
- Friends analysis based on most frequent timezones/languages

There are plenty of things that could be added to the script, feel free to contribute! 👍

Expand All @@ -20,23 +21,12 @@ $ pip install tweepy ascii_graph tqdm numpy
```


#### Linux Ubuntu / Debian Flavours

You will need to do this to get pip working with python2
```
wget https://bootstrap.pypa.io/get-pip.py
sudo python2.7 get-pip.py
sudo pip2.7 install tweepy ascii_graph tqdm numpy
python2 tweets_analyzer.py -n targetname
```

### Usage

```
usage: tweets_analyzer.py [-h] [-l N] -n screen_name [-f FILTER]
[--no-timezone] [--utc-offset UTC_OFFSET]
usage: tweets_analyzer.py -n <@screen_name> [options]
Analyze a Twitter account activity
Simple Twitter Profile Analyzer
optional arguments:
-h, --help show this help message and exit
Expand All @@ -49,6 +39,8 @@ optional arguments:
--no-timezone removes the timezone auto-adjustment (default is UTC)
--utc-offset UTC_OFFSET
manually apply a timezone offset (in seconds)
--friends will perform quick friends analysis based on lang and
timezone (rate limit = 15 requests)
```

### Example output
Expand Down
39 changes: 37 additions & 2 deletions tweets_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#
# Install:
# pip install tweepy ascii_graph tqdm numpy

from __future__ import unicode_literals

from ascii_graph import Pyasciigraph
Expand All @@ -29,14 +28,18 @@
import collections
import datetime

__version__ = '0.2'

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse

from secrets import consumer_key, consumer_secret, access_token, access_token_secret

parser = argparse.ArgumentParser(description='Analyze a Twitter account activity')
parser = argparse.ArgumentParser(description=
"Simple Twitter Profile Analyzer (https://github.com/x0rz/tweets_analyzer) version %s" % __version__,
usage='%(prog)s -n <@screen_name> [options]')
parser.add_argument('-l', '--limit', metavar='N', type=int, default=1000,
help='limit the number of tweets to retreive (default=1000)')
parser.add_argument('-n', '--name', required=True, metavar="screen_name",
Expand All @@ -50,6 +53,9 @@
parser.add_argument('--utc-offset', type=int,
help='manually apply a timezone offset (in seconds)')

parser.add_argument('--friends', action='store_true',
help='will perform quick friends analysis based on lang and timezone (rate limit = 15 requests)')


args = parser.parse_args()

Expand Down Expand Up @@ -105,6 +111,8 @@
retweeted_users = collections.Counter()
mentioned_users = collections.Counter()
id_screen_names = {}
friends_timezone = collections.Counter()
friends_lang = collections.Counter()


def process_tweet(tweet):
Expand Down Expand Up @@ -182,6 +190,17 @@ def process_tweet(tweet):
id_screen_names[ht['id_str']] = "@%s" % ht['screen_name']


def process_friend(friend):
""" Process a single friend """
friends_lang[friend.lang] += 1 # Getting friend language & timezone
if friend.time_zone:
friends_timezone[friend.time_zone] += 1

def get_friends(api, username, limit):
""" Download friends and process them """
for friend in tqdm(tweepy.Cursor(api.friends, screen_name=username).items(limit), unit="friends", total=limit):
process_friend(friend)

def get_tweets(api, username, limit):
""" Download Tweets from username account """
for status in tqdm(tweepy.Cursor(api.user_timeline, screen_name=username).items(limit),
Expand Down Expand Up @@ -324,6 +343,22 @@ def main():
print("[+] Most referenced domains (from URLs)")
print_stats(detected_domains, top=6)

if args.friends:
max_friends = numpy.amin([user_info.friends_count, 300])
print("[+] Getting %d @%s's friends data..." % (max_friends, args.name))
try:
get_friends(twitter_api, args.name, limit=max_friends)
except tweepy.error.TweepError as e:
if e[0][0]['code'] == 88:
print("[\033[91m!\033[0m] Rate limit exceeded to get friends data, you should retry in 15 minutes")
raise

print("[+] Friends languages")
print_stats(friends_lang, top=6)

print("[+] Friends timezones")
print_stats(friends_timezone, top=8)


if __name__ == '__main__':
try:
Expand Down

0 comments on commit 15c4a0d

Please sign in to comment.