From af14406224ed24a7830778974efc4777694dd04e Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 13 Apr 2015 22:58:32 +0200 Subject: [PATCH] Convert all scripts to Python 3 --- csvdb.py | 6 +++--- filler.py | 18 +++++++++--------- makedb.py | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/csvdb.py b/csvdb.py index 5385acd..a7bbc56 100755 --- a/csvdb.py +++ b/csvdb.py @@ -1,11 +1,11 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sqlite3, csv try: infile = open('tweets.csv') except IOError: - print "Please make sure that the tweets.csv from the Twitter download is located in this directory." + print("Please make sure that the tweets.csv from the Twitter download is located in this directory.") input = csv.reader(infile) @@ -15,7 +15,7 @@ cur = conn.cursor() try: cur.execute("CREATE TABLE tweets(`tweet_id` INTEGER NOT NULL, `in_reply_to_status_id` TEXT, `in_reply_to_user_id` TEXT, `timestamp` TEXT, `source` TEXT, `text` TEXT, `retweeted_status_id` TEXT, `retweeted_status_user_id` TEXT, `retweeted_status_timestamp` TEXT, `expanded_urls` TEXT, PRIMARY KEY(tweet_id));") except sqlite3.OperationalError: - print "Database.db already exists. Please delete it before trying to create a new one." + print("Database.db already exists. Please delete it before trying to create a new one.") for row in input[1:]: cur.execute("INSERT INTO tweets VALUES(?,?,?,?,?,?,?,?,?,?);",row) diff --git a/filler.py b/filler.py index 3f8e9a2..1842ee3 100755 --- a/filler.py +++ b/filler.py @@ -1,7 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- -import HTMLParser, sqlite3, time, tweepy, os +import html.parser, sqlite3, time, tweepy, os user = "Username" cke = "Consumer Key" @@ -18,8 +18,8 @@ cur = sql_conn.cursor() try: cur.execute("SELECT max(tweet_id) FROM tweets") -except sqlite3.OperationalError: - print "Please run ./makedb.py or ./csvdb.py before trying to populate the database." +except: + print("Please create a database using ./makedb.py or ./csvdb.py before trying to populate it.") try: savepoint = int(cur.fetchone()[0]) @@ -30,7 +30,7 @@ auth = tweepy.OAuthHandler(cke, cse) auth.set_access_token(ato, ase) api = tweepy.API(auth) -timelineIterator = tweepy.Cursor(api.search, q=search, since_id=savepoint).items() +timelineIterator = list(tweepy.Cursor(api.search, q=search, since_id=savepoint).items()) timeline = [] @@ -42,13 +42,13 @@ timeline.reverse() tw_counter = 0 for status in timeline: - print "(%(date)s) %(name)s: %(message)s\n" % \ + print("(%(date)s) %(name)s: %(message)s\n" % \ { "date" : status.created_at, "name" : status.author.screen_name.encode('utf-8'), - "message" : status.text.encode('utf-8') } + "message" : status.text.encode('utf-8') }) timestamp = status.created_at.strftime('%Y-%m-%d %H:%M:%S') + " +0000" - text = HTMLParser.HTMLParser().unescape(status.text).replace("'", "''") + text = html.parser.HTMLParser().unescape(status.text).replace("'", "''") cur.execute("INSERT INTO tweets('tweet_id','timestamp','text') VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')") tw_counter = tw_counter + 1 @@ -56,4 +56,4 @@ for status in timeline: sql_conn.commit() sql_conn.close() -print "Finished. %d Tweets stored" % (tw_counter) +print("Finished. %d Tweets stored" % (tw_counter)) diff --git a/makedb.py b/makedb.py index ccfd3e8..66fb3ce 100755 --- a/makedb.py +++ b/makedb.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os, sqlite3, sys