In [5]:
api_key = "my key"
api_secret = "my secret"

In [2]:
!pip3 install twython


Requirement already satisfied (use --upgrade to upgrade): twython in c:\users\radhika\appdata\local\programs\python\python35-32\lib\site-packages
Requirement already satisfied (use --upgrade to upgrade): requests>=2.1.0 in c:\users\radhika\appdata\local\programs\python\python35-32\lib\site-packages (from twython)
Requirement already satisfied (use --upgrade to upgrade): requests_oauthlib>=0.4.0 in c:\users\radhika\appdata\local\programs\python\python35-32\lib\site-packages (from twython)
Requirement already satisfied (use --upgrade to upgrade): oauthlib>=0.6.2 in c:\users\radhika\appdata\local\programs\python\python35-32\lib\site-packages (from requests_oauthlib>=0.4.0->twython)

In [7]:
twitter = twython.Twython(api_key, api_secret)
auth = twitter.get_authentication_tokens()
print("Log into Twitter as the user you want to authorize and visit this URL:")
print("\t" + auth['auth_url'])


Log into Twitter as the user you want to authorize and visit this URL:
	https://api.twitter.com/oauth/authenticate?oauth_token=EcOZWwAAAAAAwA4cAAABVdBc8k8

In [8]:
pin = "0494636"

twitter = twython.Twython(api_key, api_secret, auth['oauth_token'], auth['oauth_token_secret'])
tokens = twitter.get_authorized_tokens(pin)

new_access_token = tokens['oauth_token']
new_token_secret = tokens['oauth_token_secret']
print("your access token:", new_access_token)
print("your token secret:", new_token_secret)


your access token: 733731514526142466-06kcSBZvovHcpOvNqQfb9SrebkRiRFm
your token secret: sAuJ5XFiiIQiq4ofCSgtOOZ4PS38J0vIA2A22AH1yOy5k

In [9]:
twitter = twython.Twython(api_key, api_secret, new_access_token, new_token_secret)

In [10]:
twitter.update_status(status="hi there, I am testing my cute little Twitter Bot, how's it going :-)")


Out[10]:
{'contributors': None,
 'coordinates': None,
 'created_at': 'Sat Jul 09 15:55:35 +0000 2016',
 'entities': {'hashtags': [], 'symbols': [], 'urls': [], 'user_mentions': []},
 'favorite_count': 0,
 'favorited': False,
 'geo': None,
 'id': 751807018944561152,
 'id_str': '751807018944561152',
 'in_reply_to_screen_name': None,
 'in_reply_to_status_id': None,
 'in_reply_to_status_id_str': None,
 'in_reply_to_user_id': None,
 'in_reply_to_user_id_str': None,
 'is_quote_status': False,
 'lang': 'en',
 'place': None,
 'retweet_count': 0,
 'retweeted': False,
 'source': '<a href="https://twitter.com/radhika_lede" rel="nofollow">andromedabot</a>',
 'text': "hi there, I am testing my cute little Twitter Bot, how's it going :-)",
 'truncated': False,
 'user': {'contributors_enabled': False,
  'created_at': 'Fri May 20 18:49:59 +0000 2016',
  'default_profile': True,
  'default_profile_image': False,
  'description': 'My_Bot@ColumbiaLedeProgram',
  'entities': {'description': {'urls': []}},
  'favourites_count': 0,
  'follow_request_sent': False,
  'followers_count': 1,
  'following': False,
  'friends_count': 7,
  'geo_enabled': False,
  'has_extended_profile': False,
  'id': 733731514526142466,
  'id_str': '733731514526142466',
  'is_translation_enabled': False,
  'is_translator': False,
  'lang': 'en',
  'listed_count': 0,
  'location': '',
  'name': 'My_Lede_Bot',
  'notifications': False,
  'profile_background_color': 'F5F8FA',
  'profile_background_image_url': None,
  'profile_background_image_url_https': None,
  'profile_background_tile': False,
  'profile_image_url': 'http://pbs.twimg.com/profile_images/750333936543395840/rt8ruH8U_normal.jpg',
  'profile_image_url_https': 'https://pbs.twimg.com/profile_images/750333936543395840/rt8ruH8U_normal.jpg',
  'profile_link_color': '2B7BB9',
  'profile_sidebar_border_color': 'C0DEED',
  'profile_sidebar_fill_color': 'DDEEF6',
  'profile_text_color': '333333',
  'profile_use_background_image': True,
  'protected': False,
  'screen_name': 'radhika_lede',
  'statuses_count': 6,
  'time_zone': None,
  'url': None,
  'utc_offset': None,
  'verified': False}}

In [48]:
# setting up the Planet Database on Postgres
import pg8000

lakes = []

conn = pg8000.connect(user='postgres', password='password', database='andromedabot')
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE solarsystem (
   name varchar(80),
   radius varchar(80),
   art_distance varchar(80),
   distance varchar(80),
   flattening varchar(80),
   tilt varchar(80),
   tilt_x varchar(80),
   tilt_y varchar(80),
   tilt_z varchar(80),
   rotperiod varchar(80),
   eccentricity varchar(80),
   orbitperiod varchar(80),
   texture varchar(80),
   color varchar(80)

)
""")
conn.commit()

In [51]:
conn.rollback()

In [49]:
cursor.execute("COPY solarsystem(name,radius,art_distance,distance,flattening,tilt,tilt_x,tilt_y,tilt_z,rotperiod,eccentricity,orbitperiod,texture,color) FROM '/Users/Radhika/Downloads/planets.csv' DELIMITER ',' CSV")
conn.commit()

In [52]:
import pg8000

solar_sys = []

conn = pg8000.connect(user='postgres', password='password', database='andromedabot')
cursor = conn.cursor()
cursor.execute("SELECT name, radius, distance, tilt, rotperiod, orbitperiod FROM solarsystem")
for row in cursor.fetchall():
    solar = {'name': row[0], 'radius':row[1], 'distance': row[2], 'tilt': row[3], 'rotperiod': row[4], 'orbitperiod': row[5]}
    solar_sys.append(solar)
len(solar)


Out[52]:
6

In [53]:
sentences = {
    'radius': 'The radius of Planet {} is {} kilometers.',
    'distance': 'The distance of Planet {} from Sun is {} kilometers.',
    'tilt': 'The tilt angle to orbit plane of Planet {} is {} degrees.',
    'rotperiod': 'The rotation period of Planet {} is {} days.',
    'orbitperiod': 'The orbital period of Planet {} is {} days.'
    
}

In [70]:
import random
def random_planet_sentence(solar_sys, sentences):
    planet = random.choice(solar_sys)
    possible_keys = [k for k, v in planet.items() if k != 'name']
    col = random.choice(possible_keys)
    sentence_template = sentences[col]
    output = sentence_template.format(planet['name'], planet[col])
    return output
    
for i in range(10):
    print(random_planet_sentence(solar_sys, sentences))


The radius of Planet Earth is 6378 kilometers.
The rotation period of Planet Earth is 0.99726968 days.
The orbital period of Planet Jupiter is 4332.59 days.
The distance of Planet Mercury from Sun is 57909050 kilometers.
The rotation period of Planet Uranus is 0.71833 days.
The distance of Planet Venus from Sun is 108208000 kilometers.
The distance of Planet Jupiter from Sun is 778547200 kilometers.
The radius of Planet Mars is 3396 kilometers.
The orbital period of Planet Earth is 365.256363004 days.
The orbital period of Planet Mercury is 87.9691 days.

In [85]:
flare = ["Wow!", "Cool, huh?", "Now you know.", "See", "Neat-o!!"]
output = random_planet_sentence(solar_sys, sentences) + " " + random.choice(flare)
twitter.update_status(status=output)


Out[85]:
{'contributors': None,
 'coordinates': None,
 'created_at': 'Sat Jul 09 21:19:57 +0000 2016',
 'entities': {'hashtags': [], 'symbols': [], 'urls': [], 'user_mentions': []},
 'favorite_count': 0,
 'favorited': False,
 'geo': None,
 'id': 751888647977312256,
 'id_str': '751888647977312256',
 'in_reply_to_screen_name': None,
 'in_reply_to_status_id': None,
 'in_reply_to_status_id_str': None,
 'in_reply_to_user_id': None,
 'in_reply_to_user_id_str': None,
 'is_quote_status': False,
 'lang': 'en',
 'place': None,
 'retweet_count': 0,
 'retweeted': False,
 'source': '<a href="https://twitter.com/radhika_lede" rel="nofollow">andromedabot</a>',
 'text': 'The orbital period of Planet Jupiter is 4332.59 days. Wow!',
 'truncated': False,
 'user': {'contributors_enabled': False,
  'created_at': 'Fri May 20 18:49:59 +0000 2016',
  'default_profile': True,
  'default_profile_image': False,
  'description': 'My_Bot@ColumbiaLedeProgram',
  'entities': {'description': {'urls': []}},
  'favourites_count': 0,
  'follow_request_sent': False,
  'followers_count': 1,
  'following': False,
  'friends_count': 6,
  'geo_enabled': False,
  'has_extended_profile': False,
  'id': 733731514526142466,
  'id_str': '733731514526142466',
  'is_translation_enabled': False,
  'is_translator': False,
  'lang': 'en',
  'listed_count': 0,
  'location': '',
  'name': 'My_Lede_Bot',
  'notifications': False,
  'profile_background_color': 'F5F8FA',
  'profile_background_image_url': None,
  'profile_background_image_url_https': None,
  'profile_background_tile': False,
  'profile_image_url': 'http://pbs.twimg.com/profile_images/750333936543395840/rt8ruH8U_normal.jpg',
  'profile_image_url_https': 'https://pbs.twimg.com/profile_images/750333936543395840/rt8ruH8U_normal.jpg',
  'profile_link_color': '2B7BB9',
  'profile_sidebar_border_color': 'C0DEED',
  'profile_sidebar_fill_color': 'DDEEF6',
  'profile_text_color': '333333',
  'profile_use_background_image': True,
  'protected': False,
  'screen_name': 'LedeGalaxy',
  'statuses_count': 20,
  'time_zone': 'Pacific Time (US & Canada)',
  'url': None,
  'utc_offset': -25200,
  'verified': False}}

In [ ]:


In [ ]: