Convert all scripts to Python 3

This commit is contained in:
Klaus-Uwe Mitterer 2015-04-13 22:58:32 +02:00
parent 284e64daee
commit af14406224
3 changed files with 13 additions and 13 deletions

View file

@ -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)

View file

@ -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))

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sqlite3, sys