-
Notifications
You must be signed in to change notification settings - Fork 3
/
DailyPopBot.py
52 lines (44 loc) · 2.66 KB
/
DailyPopBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Daily Population Bot
# Follow on twitter @DailyPopulation
# By Logicmn
from random import randint
from time import sleep
from tweepy import OAuthHandler, API
from pprint import pprint
import requests
import re
consumer_key = '' # Input consumer key
consumer_secret = '' # Input secret consumer key
access_token = '' # Input access token
access_token_secret = '' # Input secret access token
auth = OAuthHandler(consumer_key, consumer_secret) # Input Twitter app credentials
auth.set_access_token(access_token, access_token_secret) # Initiate connection to Twitter bot
twitterBot = API(auth)
def main():
while True:
todayPop = worldPopulation() # Define the current world population
percent, popChange = populationChange(todayPop) # Find the percent change and population change from yesterday to today
appendPop(todayPop) # Add the population to a log file of all the past world populations
twitterBot.update_status('Currently there are {0} people on earth. That is a {1} increase since yesterday!'.format(todayPop, popChange))
sleep(86400) # Pause for 24 hours
def worldPopulation(): # Grab the worlds population in .json format by using a GET request
x = requests.get('http://api.population.io:80/1.0/population/World/today-and-tomorrow/')
world = x.json()
worldPop = world['total_population']
today = worldPop[0]
todayPop = int(today['population']) # Convert the population to an integer
return todayPop
def appendPop(todayPop):
with open("log.txt", "a") as logfile:
logfile.write("\n{0}".format(str(todayPop))) # Log every population
def populationChange(todayPop):
with open('log.txt', "rb") as logfile:
logfile.seek(-20, 2) # 2 means "from the end of the file"
lastLine = logfile.readlines()[-1]
lastPop = int("".join(map(chr, lastLine)))
percent = 100 * (1 - lastPop/todayPop) # Calculate the percent change
popChange = todayPop - lastPop # Calculate the population change
print(lastPop, todayPop)
print(percent, popChange)
return(percent, popChange)
main()