Exemplo n.º 1
0
    def updatePool(self, tableId, data):
        """
        Update the data for a pool which is already in the table.
        """

        row_id = self.ft_client.query(SQL().select(
            tableId, ["ROWID"], "pool_id = {}".format(data["pool_id"])))
        row_id = int(row_id.split('\n')[1])

        sql = SQL().update(tableId, data, row_id=row_id)
        self.ft_client.query(sql)
    def _importRows(self, filehandle, table_id, cols):
        """ Helper function to upload rows of data in a CSV file to a table """
        max_per_batch = 100
        current_row = 0
        queries = []
        rows = []
        for line in filehandle:
            values = dict(zip(cols, line))
            query = SQL().insert(table_id, values)
            queries.append(query)
            current_row += 1
            if current_row == max_per_batch:
                full_query = ';'.join(queries)
                try:
                    rows += self.ftclient.query(full_query).split("\n")[1:-1]
                except:
                    print str(sys.exc_info()[1])
                    print full_query + "\n"
                time.sleep(1)

                current_row = 0
                queries = []

        if len(queries) > 0:
            full_query = ';'.join(queries)
            try:
                rows += self.ftclient.query(full_query).split("\n")[1:-1]
            except:
                print str(sys.exc_info()[1])
                print full_query

        return rows
Exemplo n.º 3
0
 def create_table(self, table):
     """ create a table given spec in this way:
         table = {'test_poli':{'name':'STRING', 'locations':'LOCATION'}}
         return FT table id if success, None otherwise
     """
     try:
         sql = SQL().createTable(table)
         return int(self.sql(sql).split("\n")[1])
     except ValueError:
         return None
    def importFile(self, filename, table_name=None, data_types=None):
        """ Creates new table and imports data from CSV file """
        filehandle = csv.reader(open(filename, "rb"))
        cols = filehandle.next()
        if data_types: columns_and_types = dict(zip(cols, data_types))
        else: columns_and_types = dict([(c, "STRING") for c in cols])

        table = {}
        table[table_name or filename] = columns_and_types
        results = self.ftclient.query(SQL().createTable(table))
        table_id = int(results.split()[1])

        self._importRows(filehandle, table_id, cols)

        return table_id
Exemplo n.º 5
0
    def insertRow(self, tableId, data):
        """
        Insert a pool in the table.
        Data is a dict with this form : `{'columnName': dataToInsert}`

        Example :
            {
                'pool_id': 12,
                'pool_name': 'Piscine Quintal',
                'schedule_text': {
                    "Lundi": "9h00 à 18h00,
                    "Mardi": "10h00 à 15h00"
                }
            }
        """

        sql = SQL().insert(tableId, data)
        print sql
        result = self.ft_client.query(sql)
        rowId = int(result.split("\n")[1])
        return rowId
Exemplo n.º 6
0
    def getPoolId(self, tableId, name):
        """
        Get an id from the pool name.

        It will check if there's already the pool address
        in the table, if so, it will return the same id. If not, it will call
        generateIdGen(tableId).

        the function will return `id, True` if the pool already exists
        and `id, False` if it's a new pool
        """

        # Try to get the id of the pool from the given name
        sql = SQL().select(tableId, [u"pool_id"],
                           u"pool_name = '{}'".format(name))
        print sql
        result = self.ft_client.query(sql).split('\n')[1]

        # Check if the query returned an id
        if result:
            return int(result), True
        else:
            return self.generatePoolId(tableId), False
Exemplo n.º 7
0
token = ClientLogin().authorize(auth.login, auth.password)
ft_client = fusiontables.ftclient.ClientLoginFTClient(token)

# Erase all datas in the google table
#ft_client.query(SQL().deleteAllRows(tableid))

for url in URL_SOURCE:
    flux_rss = RssParser(url)
    print url
    feeds = flux_rss.process()
    for feed in feeds:
        print "\n"
        try:
            if ft_client.query(SQL().select(
                    tableid, None, "Title='" + feed.title.replace("'", "\\'") +
                    "'")).count('\n') == 1 and (feed.place.latitude != 0
                                                or feed.place.longitude != 0):
                rowid = int(
                    ft_client.query(SQL().insert(
                        tableid, {
                            'Title': feed.title.replace("'", "\\'"),
                            'Location': str(feed.place.place).replace(
                                "'", "\\'"),
                            'Date': str(feed.date),
                            'Number': str(feed.number),
                            'Latitude': str(feed.place.latitude),
                            'Longitude': str(feed.place.longitude),
                            'url': str(feed.link),
                            'Picture': str(feed.picture),
                            'Language': str(feed.lang),
Exemplo n.º 8
0
    import sys, getpass
    consumer_key = sys.argv[1]
    consumer_secret = getpass.getpass("Enter your secret: ")

    url, token, secret = OAuth().generateAuthorizationURL(
        consumer_key, consumer_secret, consumer_key)
    print "Visit this URL in a browser: ", url
    raw_input("Hit enter after authorization")

    token, secret = OAuth().authorize(consumer_key, consumer_secret, token,
                                      secret)
    oauth_client = ftclient.OAuthFTClient(consumer_key, consumer_secret, token,
                                          secret)

    #show tables
    results = oauth_client.query(SQL().showTables())
    print results

    #create a table
    table = {
        'tablename': {
            'strings': 'STRING',
            'numbers': 'NUMBER',
            'locations': 'LOCATION'
        }
    }
    tableid = int(oauth_client.query(SQL().createTable(table)).split("\n")[1])
    print tableid

    #insert row into table
    rowid = int(
Exemplo n.º 9
0
from fusiontables.fileimport.fileimporter import CSVImporter



if __name__ == "__main__":

  import sys, getpass
  username = sys.argv[1]
  password = getpass.getpass("Enter your password: "******"\n")[1])
  print tableid
  
  #insert row into table
  rowid = int(ft_client.query(SQL().insert(tableid, {'strings':'mystring', 'numbers': 12, 'locations':'Palo Alto, CA'})).split("\n")[1])
  print rowid
  
  #show rows
  print ft_client.query(SQL().select(tableid, None, "numbers=12"))
  print ft_client.query(SQL().select(tableid, ['rowid','numbers'], "numbers=12"))