<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Twitter data mining using Python assignment 14
Team Rython
Step1:  Twitter authentication (fill this!)
Step2:  Using Tweepy instead of Twython (because it's more readily available via apt-get or zypper).
Step3:  Database file to write to (fill this!)
The database file has to exist and already have a table defined. An emty ready database file is included in "spatial-backup.sqlite", so you can use that.
Step4:  SQLite opening
Make sure you load pysqlite2, and give a path for mod_spatialite for SpatiaLite support! This may be distribution-specific! You might need to install libspatialite!
Step5:  Coordinates to WKT
Converts Twitter coordinates (two points) into a Well Known Text.
Step6:  Bounding box to WKT
Calculates the centroid of a bounding box and returns a Well Known Text of that point. Only polygonal bounding boxes are supported (but are there any other kind?)
Step7:  Process query
Step8:  Example queries
Write queries in succession (or loops if you like). Their results (if they are geolocated) will be added into the SpatiaLite database.
Step9:  Close the database
Step10:  Visualise data
Opens QGIS with the database passed as an argument. It should show you all the points. Add a layer of OpenStreetMap or such for a nice visualisation of the points.
<ASSISTANT_TASK:>
Python Code:
from __future__ import division
import tweepy
import datetime
import json
import os
from pysqlite2 import dbapi2 as sqlite3
Explanation: Twitter data mining using Python assignment 14
Team Rython: Dainius Masiliunas and Tim Weerman
Date: 21st of January, 2016
Apache License 2.0  
Imports
Make sure you have pysqlite2, tweepy and spatialite installed!
End of explanation
APP_KEY = ""
APP_SECRET = ""
OAUTH_TOKEN = ""
OAUTH_TOKEN_SECRET = ""
Explanation: Twitter authentication (fill this!)
End of explanation
auth = tweepy.OAuthHandler(APP_KEY, APP_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
api = tweepy.API(auth)
Explanation: Using Tweepy instead of Twython (because it's more readily available via apt-get or zypper).
End of explanation
databasefile = "spatial-backup.sqlite"
Explanation: Database file to write to (fill this!)
The database file has to exist and already have a table defined. An emty ready database file is included in "spatial-backup.sqlite", so you can use that.
End of explanation
conn = sqlite3.connect(databasefile)
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("/usr/lib64/mod_spatialite.so.7")')
curs = conn.cursor()
Explanation: SQLite opening
Make sure you load pysqlite2, and give a path for mod_spatialite for SpatiaLite support! This may be distribution-specific! You might need to install libspatialite!
End of explanation
def coordinates_to_wkt(coords):
    if coords == None:
        return ""
    return "POINT("+str(coords["coordinates"][0])+" "+str(coords["coordinates"][1])+")"
Explanation: Coordinates to WKT
Converts Twitter coordinates (two points) into a Well Known Text.
End of explanation
def bbox_to_wkt(bbox):
    if bbox.coordinates == None:
        return ""
    if bbox.type == "Polygon":
        centroid = [0, 0]
        centroid[0] = (bbox.coordinates[0][2][0] + bbox.coordinates[0][0][0]) / 2
        centroid[1] = (bbox.coordinates[0][2][1] + bbox.coordinates[0][0][1]) / 2
        return "POINT("+str(centroid[0])+" "+str(centroid[1])+")"
    print "Unknown place type!"
    return ""
Explanation: Bounding box to WKT
Calculates the centroid of a bounding box and returns a Well Known Text of that point. Only polygonal bounding boxes are supported (but are there any other kind?)
End of explanation
def process_query(search_results):
    for tweet in search_results:
        full_place_name = ""
        place_type = ""
        location = ""
        username =  tweet.user.screen_name
        followers_count =  tweet.user.followers_count
        tweettext = tweet.text.encode("utf-8")
        if tweet.place != None:
            full_place_name = tweet.place.full_name
            place_type =  tweet.place.place_type
        coordinates = tweet.coordinates
        if (coordinates != None) or (tweet.place != None):
            print 'Found a geolocated tweet! By:'
            print username
            print '==========================='
            if coordinates != None:
                location = coordinates_to_wkt(coordinates)
            else:
                if tweet.place != None:
                    location = bbox_to_wkt(tweet.place.bounding_box)
            curs.execute("insert into tweets (username, followers_count, tweettext, full_place_name, place_type, coordinates, geometry) values (?, ?, ?, ?, ?, ?, ST_GeomFromText( ? , 4326));", \
                (username, followers_count, tweettext.decode('utf-8'), full_place_name, place_type, location, location))
            conn.commit()
Explanation: Process query: main function of the script
Does the query parsing and output to SpatiaLite. Pass the result of api.search() to it.
End of explanation
process_query(api.search(q="Beer", count=100))
process_query(api.search(q="Jorn", count=100))
process_query(api.search(q="cairo", count=100))
process_query(api.search(q="washington", count=100))
Explanation: Example queries
Write queries in succession (or loops if you like). Their results (if they are geolocated) will be added into the SpatiaLite database.
End of explanation
conn.close()
Explanation: Close the database
End of explanation
os.system("qgis "+databasefile)
Explanation: Visualise data
Opens QGIS with the database passed as an argument. It should show you all the points. Add a layer of OpenStreetMap or such for a nice visualisation of the points.
End of explanation
<END_TASK>