HELLO


In [ ]:


In [130]:
import os
import re
import tweepy
from datetime import datetime
import pandas as pd
import numpy as np
import seaborn as sb

In [131]:
class info:
    
    def __init__(self,file):
        self.__file = file
        with open(self.__file,"r") as f:
            
            vals = f.read()
            listVals = [i.split("=")  for i in vals.split("\n")]
            del listVals[-1]
            dictVals = {x[0]:x[1] for x in listVals}
            #print(dictVals)
            try:
                self.__consumer_key = dictVals["consumer_key"]
                self.__consumer_secret = dictVals["consumer_secret"]
                self.__access_token = dictVals["access_token"]
                self.__access_token_secret = dictVals["access_token_secret"]
            except:
                print("sorry....")
        
    @property
    def file(self):
        return self.__file
    
    @file.setter
    def file(self,file):
        self.__file = file
        
    @property
    def consumer_key(self):
        return self.__consumer_key
    
    @property
    def consumer_secret(self):
        return self.__consumer_secret
    
    @property
    def access_token(self):
        return self.__access_token
    
    @property
    def access_token_secret(self):
        return self.__access_token_secret

In [132]:
##############################Insert the right path!################################
p = info("/home/michael/twitterTokens")

In [133]:
ip = %system ifconfig

In [134]:
ipAdd = list(filter(lambda x: "inet addr:192" in x,ip))
extracted = re.search(r"inet addr:\d{3}\.\d{3}\.\d{1,3}\.\d{1,3}",ipAdd[0])

In [135]:
adress = re.search(r'\d+\.\d+\.\d+\.\d+', extracted.group(0))
ipAddress = [int(i) for i in adress.group(0).split(".")]
print(ipAddress)


[192, 168, 0, 13]

In [136]:
#Variables that contains the user credentials to access Twitter API 
####


auth = tweepy.OAuthHandler(p.consumer_key, p.consumer_secret)
auth.set_access_token(p.access_token, p.access_token_secret)

api = tweepy.API(auth)

#show my own tweets from my time line
public_tweets = api.home_timeline()
for tweet in public_tweets:
    print(tweet.text)


We’re testing robotics in Antarctic ice caves to better understand tools needed for visits to icy worlds like Europ… https://t.co/s93tDauxnD
RT @Space_Station: Astronauts train today for @SpaceX #Dragon capture and check out @AstroRobonaut's power supply. https://t.co/96n48q5yVt…
Marlon Brando and a cat c. 1950s https://t.co/YGuk1yYNuI
Engineer Thomas Byrdsong (r) works on the Apollo/Saturn 1B Ground-wind-loads model @NASA_Langley in 1963:… https://t.co/FkYyqL3FES
Jim Morrison, recording 1971’s L.A. Woman, by Edmund Teske https://t.co/OcC5QaILaV
Jupiter and three of its moons were seen by our @OSIRISREx spacecraft as it travels in search of an asteroid. Info:… https://t.co/eir6qV7KPP
RT @NASASunEarth: 15 years after launch, this instrument is still breaking ground in upper-atmosphere research. https://t.co/Vw8AseVukK htt…
Seen from space: Pine Island Glacier sheds another block of ice into Antarctic waters, showing evidence of fragilit… https://t.co/4JF4pyLSBN
We've investigating one of the biggest gaps in the understanding of Earth's water resources: snow. More:… https://t.co/6TRM0lZiPy
.@SpaceX launch Saturday will be 1st from Pad 39A since shuttle & will deliver 5,500 lbs of cargo to @Space_Station… https://t.co/PZuMRbpbd0
RT @Thom_astro: With Progress and HTV gone, our Soyuz is lonely. At least it gets to enjoy the wonderful view of the entire Floridian penin…
Sounding rocket set to launch to explore the aurora & its interactions with Earth’s upper atmosphere and ionosphere… https://t.co/0CvUlhvWf3
China. Guangxi province, 1980. Photo by Bruno Barbey. https://t.co/ADMDn0zu06
1960s civil rights https://t.co/FDmA2ZgQEd
RT @Thom_astro: Beautiful at night as well as during the day – you know what they say... all roads lead to #Rome https://t.co/E4Z3zKURKX ht…
Monthly @NASAGISS temp update shows January 2017 was the 3rd warmest January in 137 years of modern record-keeping:… https://t.co/gdGyqdQUIJ
Acting Admin Lightfoot presents our Exceptional Public Achievement Medal to author & director of #HiddenFigures, wh… https://t.co/1DYeGMGQUo
Summer, 1976.. London https://t.co/B9DAHL2rXp
Such beautiful imagery! #CassiniInspires https://t.co/hGYOrE39Al
Dare to dream! We're working to send humans on a #JourneyToMars. The testing is real, the journey is underway https://t.co/BNUZclDes5

In [137]:
user = api.get_user('BaumRn')
followers = user.followers()

In [16]:
def getAllTweets(user):
    '''
        This method returns a list with all tweets that a person has made or brought
    '''
    tweets = user.timeline(count=200)
    allTweets = tweets
    while 1:
        try:
            maxId = [v.id for v in tweets][-1]
        except IndexError:
            break
        tweets = user.timeline(count=200,max_id=maxId-1)
        allTweets = allTweets+tweets    
    return allTweets

In [17]:
allTweets = getAllTweets(user)
print(len(allTweets))
firstTweet = allTweets[-1]
#for v in allTweets:
#    #print(v)
#    print(v.retweet_count)
#    print(str(v.id)+"\t"+str(re.findall(r'\#[a-zA-Z0-9]+',v.text))+"\t"+str(v.created_at)+"\t")
#   print("\n")


430

In [18]:
#the first tweet TB did
print(str(firstTweet.id)+"\t"+firstTweet.text+"\t"+str(firstTweet.created_at))


657108374887251968	#dkpol #dkbiz Fortsat bedring i finansieringsklimaet i Danmark https://t.co/hinDzMU9vl	2015-10-22 08:16:59

In [19]:
dates = list(map(lambda x: (x.year,x.month,x.day), [v.created_at for v in allTweets]))
dateDf = pd.DataFrame(dates,columns=["year","month","day"])

In [20]:
dateDf.groupby(by=["year","month"],as_index=False).count()


Out[20]:
year month day
0 2015 10 5
1 2015 12 11
2 2016 1 48
3 2016 2 75
4 2016 3 62
5 2016 4 49
6 2016 5 18
7 2016 6 45
8 2016 7 14
9 2016 8 6
10 2016 9 59
11 2016 10 11
12 2016 11 24
13 2016 12 2
14 2017 1 1

In [14]:
#people following Thorbjørn

for i in user.followers():
    print(i.name)
    
tbTimeLine = user.timeline()


Peter Sergio Larsen
Mick Holm Kristensen
Michael Svanholm
Dennis Jensen
Frederik Funder
Anders Goul Møller
Anders F. Kjærgaard
Ema Radmilovic
Sigrid Wilbeck
Ania
Stirling Global.
Niels BrandtPetersen
Christian Hannibal
Adam Lebech
Maiken Stensgaard
Genevieve Alister
Hanne Schou
Lise Walbom
Kenneth Schultz
Anders Brandtoft

In [21]:
api.friends_ids()


Out[21]:
[3613880475,
 2807378283,
 2339814349,
 40304113,
 228107253,
 11348282,
 1086485478,
 1287555762,
 15165493]

In [23]:
#number of times Thorbjørn has been retweetedt
tbTimeLine


Out[23]:
Status(is_quote_status=False, place=None, in_reply_to_user_id=None, favorite_count=0, source='Twitter for iPhone', in_reply_to_screen_name=None, coordinates=None, in_reply_to_user_id_str=None, in_reply_to_status_id=None, entities={'symbols': [], 'user_mentions': [{'id': 85666267, 'indices': [3, 12], 'screen_name': 'bjhjuler', 'name': 'Bo Hjuler', 'id_str': '85666267'}, {'id': 1088897515, 'indices': [24, 30], 'screen_name': 'Wuxus', 'name': 'Wuxus A/S', 'id_str': '1088897515'}, {'id': 773271914, 'indices': [34, 50], 'screen_name': 'BrianMikkelsenC', 'name': 'Brian Mikkelsen', 'id_str': '773271914'}, {'id': 875236008, 'indices': [62, 74], 'screen_name': 'di_digital_', 'name': 'DI Digital', 'id_str': '875236008'}, {'id': 711855443778347008, 'indices': [86, 100], 'screen_name': 'VindFremtiden', 'name': 'DI - Vind Fremtiden', 'id_str': '711855443778347008'}], 'hashtags': [{'text': 'digidk', 'indices': [116, 123]}, {'text': 'dkbiz', 'indices': [124, 130]}], 'urls': []}, id=816344704283451392, contributors=None, favorited=False, truncated=False, id_str='816344704283451392', retweeted=False, retweeted_status=Status(is_quote_status=False, place=None, in_reply_to_user_id=None, favorite_count=8, source='Twitter Web Client', in_reply_to_screen_name=None, coordinates=None, in_reply_to_user_id_str=None, in_reply_to_status_id=None, entities={'symbols': [], 'user_mentions': [{'id': 1088897515, 'indices': [10, 16], 'screen_name': 'Wuxus', 'name': 'Wuxus A/S', 'id_str': '1088897515'}, {'id': 773271914, 'indices': [20, 36], 'screen_name': 'BrianMikkelsenC', 'name': 'Brian Mikkelsen', 'id_str': '773271914'}, {'id': 875236008, 'indices': [48, 60], 'screen_name': 'di_digital_', 'name': 'DI Digital', 'id_str': '875236008'}, {'id': 711855443778347008, 'indices': [72, 86], 'screen_name': 'VindFremtiden', 'name': 'DI - Vind Fremtiden', 'id_str': '711855443778347008'}], 'hashtags': [{'text': 'digidk', 'indices': [102, 109]}], 'urls': [{'display_url': 'twitter.com/i/web/status/8…', 'indices': [111, 134], 'url': 'https://t.co/DFkW1Gi3PA', 'expanded_url': 'https://twitter.com/i/web/status/816226586571788288'}]}, id=816226586571788288, contributors=None, possibly_sensitive=False, favorited=False, truncated=True, id_str='816226586571788288', retweeted=False, retweet_count=7, lang='da', _json={'is_quote_status': False, 'place': None, 'in_reply_to_user_id': None, 'favorite_count': 8, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'geo': None, 'in_reply_to_user_id_str': None, 'in_reply_to_status_id': None, 'entities': {'symbols': [], 'user_mentions': [{'id': 1088897515, 'indices': [10, 16], 'screen_name': 'Wuxus', 'name': 'Wuxus A/S', 'id_str': '1088897515'}, {'id': 773271914, 'indices': [20, 36], 'screen_name': 'BrianMikkelsenC', 'name': 'Brian Mikkelsen', 'id_str': '773271914'}, {'id': 875236008, 'indices': [48, 60], 'screen_name': 'di_digital_', 'name': 'DI Digital', 'id_str': '875236008'}, {'id': 711855443778347008, 'indices': [72, 86], 'screen_name': 'VindFremtiden', 'name': 'DI - Vind Fremtiden', 'id_str': '711855443778347008'}], 'hashtags': [{'text': 'digidk', 'indices': [102, 109]}], 'urls': [{'display_url': 'twitter.com/i/web/status/8…', 'indices': [111, 134], 'url': 'https://t.co/DFkW1Gi3PA', 'expanded_url': 'https://twitter.com/i/web/status/816226586571788288'}]}, 'id': 816226586571788288, 'contributors': None, 'in_reply_to_status_id_str': None, 'possibly_sensitive': False, 'truncated': True, 'favorited': False, 'id_str': '816226586571788288', 'retweeted': False, 'retweet_count': 7, 'lang': 'da', 'coordinates': None, 'created_at': 'Tue Jan 03 10:15:57 +0000 2017', 'in_reply_to_screen_name': None, 'text': 'Hør bl.a. @Wuxus og @BrianMikkelsenC. Nytårskur @di_digital_ 5. januar. @VindFremtiden ikke tabe den. #digidk… https://t.co/DFkW1Gi3PA'}, created_at=datetime.datetime(2017, 1, 3, 10, 15, 57), geo=None, _api=<tweepy.api.API object at 0x7fcfbabb0fd0>, in_reply_to_status_id_str=None, source_url='http://twitter.com', text='Hør bl.a. @Wuxus og @BrianMikkelsenC. Nytårskur @di_digital_ 5. januar. @VindFremtiden ikke tabe den. #digidk… https://t.co/DFkW1Gi3PA'), retweet_count=7, lang='da', _json={'is_quote_status': False, 'place': None, 'in_reply_to_user_id': None, 'favorite_count': 0, 'source': '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>', 'geo': None, 'in_reply_to_user_id_str': None, 'in_reply_to_status_id': None, 'entities': {'symbols': [], 'user_mentions': [{'id': 85666267, 'indices': [3, 12], 'screen_name': 'bjhjuler', 'name': 'Bo Hjuler', 'id_str': '85666267'}, {'id': 1088897515, 'indices': [24, 30], 'screen_name': 'Wuxus', 'name': 'Wuxus A/S', 'id_str': '1088897515'}, {'id': 773271914, 'indices': [34, 50], 'screen_name': 'BrianMikkelsenC', 'name': 'Brian Mikkelsen', 'id_str': '773271914'}, {'id': 875236008, 'indices': [62, 74], 'screen_name': 'di_digital_', 'name': 'DI Digital', 'id_str': '875236008'}, {'id': 711855443778347008, 'indices': [86, 100], 'screen_name': 'VindFremtiden', 'name': 'DI - Vind Fremtiden', 'id_str': '711855443778347008'}], 'hashtags': [{'text': 'digidk', 'indices': [116, 123]}, {'text': 'dkbiz', 'indices': [124, 130]}], 'urls': []}, 'id': 816344704283451392, 'contributors': None, 'in_reply_to_status_id_str': None, 'truncated': False, 'favorited': False, 'id_str': '816344704283451392', 'retweeted': False, 'retweeted_status': {'is_quote_status': False, 'place': None, 'in_reply_to_user_id': None, 'favorite_count': 8, 'source': '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>', 'geo': None, 'in_reply_to_user_id_str': None, 'in_reply_to_status_id': None, 'entities': {'symbols': [], 'user_mentions': [{'id': 1088897515, 'indices': [10, 16], 'screen_name': 'Wuxus', 'name': 'Wuxus A/S', 'id_str': '1088897515'}, {'id': 773271914, 'indices': [20, 36], 'screen_name': 'BrianMikkelsenC', 'name': 'Brian Mikkelsen', 'id_str': '773271914'}, {'id': 875236008, 'indices': [48, 60], 'screen_name': 'di_digital_', 'name': 'DI Digital', 'id_str': '875236008'}, {'id': 711855443778347008, 'indices': [72, 86], 'screen_name': 'VindFremtiden', 'name': 'DI - Vind Fremtiden', 'id_str': '711855443778347008'}], 'hashtags': [{'text': 'digidk', 'indices': [102, 109]}], 'urls': [{'display_url': 'twitter.com/i/web/status/8…', 'indices': [111, 134], 'url': 'https://t.co/DFkW1Gi3PA', 'expanded_url': 'https://twitter.com/i/web/status/816226586571788288'}]}, 'id': 816226586571788288, 'contributors': None, 'in_reply_to_status_id_str': None, 'possibly_sensitive': False, 'truncated': True, 'favorited': False, 'id_str': '816226586571788288', 'retweeted': False, 'retweet_count': 7, 'lang': 'da', 'coordinates': None, 'created_at': 'Tue Jan 03 10:15:57 +0000 2017', 'in_reply_to_screen_name': None, 'text': 'Hør bl.a. @Wuxus og @BrianMikkelsenC. Nytårskur @di_digital_ 5. januar. @VindFremtiden ikke tabe den. #digidk… https://t.co/DFkW1Gi3PA'}, 'retweet_count': 7, 'lang': 'da', 'coordinates': None, 'created_at': 'Tue Jan 03 18:05:19 +0000 2017', 'in_reply_to_screen_name': None, 'text': 'RT @bjhjuler: Hør bl.a. @Wuxus og @BrianMikkelsenC. Nytårskur @di_digital_ 5. januar. @VindFremtiden ikke tabe den. #digidk #dkbiz https://…'}, created_at=datetime.datetime(2017, 1, 3, 18, 5, 19), geo=None, _api=<tweepy.api.API object at 0x7fcfbabb0fd0>, in_reply_to_status_id_str=None, source_url='http://twitter.com/download/iphone', text='RT @bjhjuler: Hør bl.a. @Wuxus og @BrianMikkelsenC. Nytårskur @di_digital_ 5. januar. @VindFremtiden ikke tabe den. #digidk #dkbiz https://…')

In [ ]: