An election twitter bot from BBC News

Wesley Stephenson @Wesstephenson

Alison Benjamin @Hey_benjamin

Link to this dropbox paper: https://tinyurl.com/s3ayd34



Agenda
  • Introduction: today’s workshop
  • Building graphics with R
  • Tweeting graphics with Python
  • Considerations 


Introduction: today’s workshop

What did our twitter bot do? 
We took the results for each of the 650 seats in the UK and we created 690 of these Twitter results cards:

One for each seat in the UK House of Commons and 40 in Welsh for the Welsh constituencies. 

As the night went on we also created 29 of these UK scorecards:
And scorecards for Scotland, England, Northern Ireland and Wales. 

What we’ll cover in today’s workshop:







Part 1 - Building graphics with R


Line 3 - delete it.

Let’s go

Lets deconstruct this card. How do we get this:

From this result csv:
gssId
constituency_shortName
newPartyCode
partyCodeLast
turnout_percentage
majorityNow
resultBanner
fullName
party_shortName
votesNow
candidate_party_code


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Lauren Davies
Modern Alliance Coalition
21098
MAC


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Justin Henderson
Social Advancement
14789
SA


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Habiba Khan
Moderate Resistance Party
2466
MRP


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Jean Cleveland
Environmental Climate Alliance
1017
ECA


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Elise Taylor
Traditional Preservation Movement
543
TPM


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Crazy Chuck McGraw III
Nutty Noodle Alliance Party
211
NNAP


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Janae Sheridan
Independent
72
IND


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Hector Whelan
Independent
33
IND


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Mylo Mcgee
Independent
31
IND


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Adrienne Curtis
Independent
19
IND


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Jan Stuart
Independent
7
IND


UC0001
Nicarshire
MAC
MAC
87.1
6309
MAC HOLD
Wiktoria Rivas
Independent
5
IND



It consists of two images. First a background which is 1920x1080 pixels. This has everything on it that will stay the same:

And the live data for each seat which is coming in from your data source. 

This is all drawn using ggplot and the secret to this is remembering that all ggplot is doing is placing things on a grid. If we put the grid back in you can see what I mean.

Follow the process

Open this file ~desktop/hands_on_classes/an_automated_twitterbot_using_r_and_python_2176/src/R/BUILD_GRAPHIC_DEMO.R

Formatting your data

Arranging and filtering your results, adding the colours for each of the parties and changing the results text so it is easier to read

Creating coordinates data frame for the twitter card:

Each of the graphics on the sheet is drawn using a set of x, y co-ordinates. You need to create a data frame with these coordinates in it.

We have set a maximum of six candidates for each card. Each candidate has their own set of graphics - a small solid box and a large opaque box and the co-ordinates need to be set out in a data frame like this.
order
group
x_small
x_large
y
grow
6
cand6
3
6
20
0
6
cand6
6
57
20
0
6
cand6
6
57
0
0
6
cand6
3
6
0
0
5
cand5
3
6
49
29
5
cand5
6
57
49
29
5
cand5
6
57
29
29
5
cand5
3
6
29
29
4
cand4
3
6
78
58
4
cand4
6
57
78
58
4
cand4
6
57
58
58
4
cand4
3
6
58
58
3
cand3
3
6
107
87
3
cand3
6
57
107
87
3
cand3
6
57
87
87
3
cand3
3
6
87
87
2
cand2
3
6
136
116
2
cand2
6
57
136
116
2
cand2
6
57
116
116
2
cand2
3
6
116
116
1
cand1
3
6
165
145
1
cand1
6
57
165
145
1
cand1
6
57
145
145
1
cand1
3
6
145
145
The code below creates the data frame above.




Building the plot


Fixed text:

This is the text that always stays the same regardless of the result

Winning party polygons:

The polygon on the left on the left denotes the winning party in 2020. The smaller one on the right is the party that won the seat in 2016.


Candidate picture or party logo


Candidate party colours

This uses the data frame of coordinates we made for the large and small boxes.  


Dynamic text:

The text that will change depending on the result

The calculation below is used to determine the y coordinate used for placing the text for each candidate - the party code, the name of the candidate and the umber of votes they received.


Finalise styling:

This removes the background grey colour the grid lines and axis ticks. 

Adding the background:

Uses the ‘magick’ package to add the background


Part 2 - Tweeting graphics with Python


pass: b33pb33p
verification phone number: 447413932663



  • How it works
  • Code walkthrough
  • Try it!


make run_twitterbot

Part 3 - Considerations for your newsroom 


  • Getting test and live developer Twitter accounts
  • What is your data source?
  • Where is your data coming from and in what format?
  • How do you want your card to look?
  • What party colours are you going to use?
  • Do you need photos - where are you going to get them from - what happens if you haven’t got a photo?
  • Trailing and promotion
  • Run locally? Run in the cloud?