This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_party_table(self): | |
# Panda Data Frame - Table of Tweeter Users per Party | |
# https://stackoverflow.com/questions/48909110/python-pandas-mean-and-sum-groupby-on-different-columns-at-the-same-time | |
panda_data = { "Party": self.__col_party, | |
"PartyCount": self.__col_party, # Duplicate Party column for the counting | |
"FollowersCount": self.__col_followers_count, | |
"FriendsCount": self.__col_friends_count | |
} | |
df = DataFrame(panda_data , index=self.__labels) | |
# We use the as_index parameter, so that Party will also be a column of the data frame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
twitterListAccount: "SoMePolis" | |
twitterListName: "BundesparlamentarierInnen" | |
# https://www.parlament.ch/de/organe/fraktionen/im-parlament-vertretene-parteien | |
parties: | |
- twitter: "spschweiz" | |
abbrs: ["SP","PS"] | |
keywords: ["socialist", "sozialist"] | |
- twitter: "FDP_Liberalen" | |
abbrs: ["FDP","PLR"] | |
keywords: ["fdp","plr"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __check_for_party(self, user_list): | |
party_column = [] | |
# iterate through each user account | |
for user in user_list: | |
# iterate trough all parties | |
for party in self.__cfg.get('parties'): | |
res = "unknown" | |
# Check first all abbreviations | |
# We don't do lower here because that could result to false positive | |
for abbr in party['abbrs']: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_politican_table(self): | |
# Panda Data Frame - Table of all Parliament Members | |
panda_data = { 'ScreenName' : self.__col_screen_name, | |
'Name': self.__col_name, | |
'Description': self.__col_description, | |
"FollowersCount": self.__col_followers_count, | |
"FriendsCount": self.__col_friends_count, | |
"Party": self.__col_party | |
} | |
df = DataFrame(panda_data , index=self.__labels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
analyzer_ch = GovernmentSocialMediaAnalyzer("CH") | |
analyzer_ch.create_politican_table() | |
analyzer_ch.create_party_table() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __init__(self): | |
res = {} | |
# Reading the secret values from a local disk | |
with open("../../../secret/twitter.yaml", 'r') as stream: | |
try: | |
res= yaml.load(stream) | |
except yaml.YAMLError as exc: | |
print(exc) | |
exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_government_members(self): | |
lists = self.__api.lists_all('SoMePolis') | |
for list in lists: | |
if list.name == 'BundesparlamentarierInnen': | |
result = [] | |
for item in tweepy.Cursor(self.__api.list_members, list_id=list.id).items(): | |
result.append(item) | |
return result | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_plotly_table(self): | |
lists = self.get_government_members() | |
labels = range(0,len(lists)) | |
col1 = [list.screen_name for list in lists] | |
col2 = [list.name for list in lists] | |
col3 = [list.description for list in lists] | |
col4 = [list.followers_count for list in lists] | |
col5 = [list.friends_count for list in lists] | |
# Panda Data Set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# attempt authentication | |
try: | |
# create OAuthHandler object | |
auth = OAuthHandler(consumer_key, consumer_secret) | |
# set access token and secret | |
auth.set_access_token(access_token, access_token_secret) | |
# create tweepy API object to fetch tweets | |
self.__api = tweepy.API(auth) | |
except: | |
print("Error: Authentication Failed") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_party_friends_count_bar_chart(self, df): | |
data = [ | |
go.Bar( | |
x=df.Party, # assign x as the dataframe column 'x' | |
y=df.FriendsCount | |
) | |
] | |
py.plot(data, filename=self.__country_code+'-tw-party_politicans_count') | |
def create_party_politicans_count_pie_chart(self, df): |