Skip to content

Instantly share code, notes, and snippets.

View talfco's full-sized avatar

Felix Kuestahler talfco

View GitHub Profile
@talfco
talfco / lesson1-execise.py
Last active January 27, 2019 10:53
Python Tutorial: Checking the Party membership of a twitter account.
# Method will return the party of the list member by analyzing
# twitter account attributes. If party cannot be detected the
# account column have an entry "unknown"
def check_for_party(self, account_list):
party_column = []
party_abbreviations = ["FDP", "CVP", "SP", "SVP", "EVP", "BDP"]
for account in account_list:
party = "unknown"
for abbr in party_abbreviations:
if abbr in account.description:
def __init__(self, country_code):
self.__country_code = country_code
# Dependent on the country parameter load the relevant configurations
file_name = "config-"+country_code+".yaml"
with open("../../cfg/"+file_name, 'r') as stream:
try:
self.__cfg= yaml.load(stream)
print(self)
except yaml.YAMLError as exc:
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
class GovernmentSocialMediaAnalyzer(object):
# Class Instance Variable
__cfg = {}
__country_code = None
__api = None
def __init__(self, country_code):
self.__country_code = country_code
# Dependent on the country parameter load the relevant configurations
file_name = "config-"+country_code+".yaml"
twitterListAccount: "SoMePolis"
twitterListName: "BundesparlamentarierInnen"
def __get_government_members(self):
lists = self.__api.lists_all(self.__cfg['twitterListAccount'])
for list in lists:
if list.name == self.__cfg['twitterListName']:
result = []
for item in tweepy.Cursor(self.__api.list_members, list_id=list.id).items():
result.append(item)
return result
return None
def __extract_columns(self):
accounts = self.__get_government_members()
labels = range(0,len(accounts))
self.__col_screen_name = [account.screen_name for account in accounts]
self.__col_name = [account.name for account in accounts]
self.__col_description = [account.description for account in accounts]
self.__col_followers_count = [account.followers_count for account in accounts]
self.__col_friends_count = [account.friends_count for account in accounts]
self.__col_party = self.__check_for_party(accounts)
def check_for_party(self, account_list):
party_column = []
party_abbreviations = ["FDP", "CVP", "SP", "SVP", "EVP", "BDP"]
for account in account_list:
party = "unknown"
for abbr in party_abbreviations:
if abbr in account.description or abbr in account.screen_name:
party = abbr
party_column.append(party)
return party_column
twitterListAccount: "SoMePolis"
twitterListName: "BundesparlamentarierInnen"
# https://www.parlament.ch/de/organe/fraktionen/im-parlament-vertretene-parteien
parties:
- twitter: "spschweiz"
abbrs: ["SP","PS"]
- twitter: "FDP_Liberalen"
abbrs: ["FDP","PLR"]
- twitter: "SVPch"
abbrs: ["SVP","UDC"]
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']: