Skip to content

Instantly share code, notes, and snippets.

Avatar

Felix Kuestahler talfco

View GitHub Profile
@talfco
talfco / lesson1-execise.py
Last active Jan 27, 2019
Python Tutorial: Checking the Party membership of a twitter account.
View lesson1-execise.py
# 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:
View tut01-lesson2-init.py
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:
View tut01-get_government_members-lesson1.py
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
View tut01-lesson2-init1.py
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"
View tut01-lesson2-config-CH.yaml
twitterListAccount: "SoMePolis"
twitterListName: "BundesparlamentarierInnen"
View tut01-lesson2-get_government_members.py
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
View tut01-lesson2-extract_columns.py
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)
View tut01-lesson1-ex1.py
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
View tut01-1-config-CH.yaml
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"]
View tut01-lesson2-check_for_party_1.py
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']: