Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 102 lines (81 sloc) 3.262 kb
8acd9c9 Stephan Hügel Now using tweepy and Python 2.x syntax
authored
1 #!/usr/bin/python
2 # coding=utf-8
6272d51 Stephan Hügel Added some more error handling (table creation, setting line position to...
authored
3 # This is useful: http://openbookproject.net//thinkCSpy/
4 # This, too: http://www.devshed.com/c/a/Python/Using-SQLite-in-Python/
5
4c189e4 Stephan Hügel Tidying up
authored
6 # twitter: robo_dante/beatrice
7 # gmail: alighieribot2010/beatrice1265
8
829aa8e Stephan Hügel renamed
authored
9 import sys
10 import os
11 import sqlite3
a56e304 Stephan Hügel Roughed in some error logging
authored
12 import logging
13 LOG_FILENAME = '/Users/sth/library/logs/python.log'
14 logging.basicConfig(filename=LOG_FILENAME,level=logging.ERROR)
8acd9c9 Stephan Hügel Now using tweepy and Python 2.x syntax
authored
15 import tweepy
8a28d9a Stephan Hügel A trivial bit of error handling
authored
16
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
17 # tweepy stuff
18 auth=tweepy.BasicAuthHandler('robo_dante', 'beatrice')
19 api=tweepy.API(auth)
2f23e06 Stephan Hügel Yet more tidying
authored
20
21
22
8a28d9a Stephan Hügel A trivial bit of error handling
authored
23 # create a SQLite connection, or create a new db and table
074f8b0 Stephan Hügel Some tidying up
authored
24 connection=sqlite3.connect('dc.db')
25 cursor=connection.cursor()
829aa8e Stephan Hügel renamed
authored
26 try:
79981b4 Stephan Hügel lastline and off_set incrementing in case of a header isn't working
authored
27 cursor.execute('SELECT * FROM position ORDER BY POSITION DESC LIMIT 1')
829aa8e Stephan Hügel renamed
authored
28 except sqlite3.OperationalError:
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
29 print "Couldn't find the specified table. Creating…"
30 # set up a new blank table, and insert a row which starts off at line 0
074f8b0 Stephan Hügel Some tidying up
authored
31 cursor.execute('CREATE TABLE position (id INTEGER PRIMARY KEY, position INTEGER, tweeted INTEGER, off_set INTEGER)')
6272d51 Stephan Hügel Added some more error handling (table creation, setting line position to...
authored
32 lastline=0
074f8b0 Stephan Hügel Some tidying up
authored
33 cursor.execute('INSERT INTO position VALUES (null, ?, ?, ?)',(lastline, "1", 0))
8a28d9a Stephan Hügel A trivial bit of error handling
authored
34 try:
35 cursor.execute('SELECT * FROM position ORDER BY POSITION DESC LIMIT 1')
36 except sqlite3.OperationalError:
37 print "Still couldn't execute the SQL query, even though I created a new table. Giving up."
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
38 # close the SQLite connection, and quit
39 connection.commit()
8a28d9a Stephan Hügel A trivial bit of error handling
authored
40 sys.exit()
6272d51 Stephan Hügel Added some more error handling (table creation, setting line position to...
authored
41
2f23e06 Stephan Hügel Yet more tidying
authored
42
43
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
44 # get the highest page number, and the line display offset
074f8b0 Stephan Hügel Some tidying up
authored
45 row=cursor.fetchone()
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
46 lastline=row[1]
074f8b0 Stephan Hügel Some tidying up
authored
47 off_set=row[3]
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
48 # the poem starts on line 1, not line 0, so increase by 1, then subtract offset from 'real' line display
49 displayline=(lastline + 1) - off_set
074f8b0 Stephan Hügel Some tidying up
authored
50
79981b4 Stephan Hügel lastline and off_set incrementing in case of a header isn't working
authored
51
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
52
d9d65b8 Stephan Hügel Incrementer now stuck, because I apparently can't do basic arithmetic
authored
53 def format_tweet(input_string,next_string):
79981b4 Stephan Hügel lastline and off_set incrementing in case of a header isn't working
authored
54 """ Properly format an input string based on whether it's a header line, or a poetry line
8a28d9a Stephan Hügel A trivial bit of error handling
authored
55 accepts 2 inputs: the current line from a book, and the following line. If the current line
56 begins with "CANTO", it's a header, so instead of displaying a line number, we join the next line
57 and increment both the line number and the line offset by 1. This means the line numbers
58 don't jump, when a header is encountered, as the offset is subtracted from the display line.
59 Returns a ready-to-tweet string, either a canto, or a poetry line. """
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
60 #pattern='^CANTO'
61 #if re.search(pattern, input_string):
62 if input_string.startswith("CANTO"):
d9d65b8 Stephan Hügel Incrementer now stuck, because I apparently can't do basic arithmetic
authored
63 global lastline
64 global off_set
2f23e06 Stephan Hügel Yet more tidying
authored
65 lastline=lastline + 1
66 off_set=off_set + 1
79981b4 Stephan Hügel lastline and off_set incrementing in case of a header isn't working
authored
67 return input_string + next_string
68 else:
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
69 return 'l. ' + str(displayline) + ': ' + input_string
a56e304 Stephan Hügel Roughed in some error logging
authored
70 # what if it's neither a header nor a poetry line?
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
71
4c189e4 Stephan Hügel Tidying up
authored
72
074f8b0 Stephan Hügel Some tidying up
authored
73
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
74 get_lines = list()
829aa8e Stephan Hügel renamed
authored
75 try:
8acd9c9 Stephan Hügel Now using tweepy and Python 2.x syntax
authored
76 with open('dc.txt', "r") as t_file:
829aa8e Stephan Hügel renamed
authored
77 for a_line in t_file:
78 if not a_line.strip():
79 continue
80 # if we encounter a blank line, do nothing and carry on
81 else:
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
82 get_lines.append(a_line)
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
83
829aa8e Stephan Hügel renamed
authored
84 except IOError:
8acd9c9 Stephan Hügel Now using tweepy and Python 2.x syntax
authored
85 print "Couldn't open the text file for reading. Exiting."
829aa8e Stephan Hügel renamed
authored
86 sys.exit()
7b72af8 Stephan Hügel Integrated yatcip stuff, which doesn't work yet
authored
87
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
88 # take the raw text lines, and correctly format them for display
8ada174 Stephan Hügel Was grabbing the ID from the table (row[0]) as opposed to the line posit...
authored
89 tweet=format_tweet(get_lines[lastline],get_lines[lastline + 1])
7b72af8 Stephan Hügel Integrated yatcip stuff, which doesn't work yet
authored
90
91 # check for exceptions:
ed7213b Stephan Hügel Correctly handling new Canto lines
authored
92 #try:
eb35055 Stephan Hügel Tweet formatting now in its own function
authored
93 # api.update_status(tweet)
ed7213b Stephan Hügel Correctly handling new Canto lines
authored
94 #except:
a56e304 Stephan Hügel Roughed in some error logging
authored
95 # logging.error("Something went wrong, and the tweet couldn't be sent")
ed7213b Stephan Hügel Correctly handling new Canto lines
authored
96 # sys.exit()
1b867db Stephan Hügel Tidying, faffery. I have no self-discipline when it comes to formatting.
authored
97
eb35055 Stephan Hügel Tweet formatting now in its own function
authored
98 print tweet
074f8b0 Stephan Hügel Some tidying up
authored
99 cursor.execute('INSERT INTO position VALUES (null, ?, ?, ?)',(lastline + 1, "1", off_set))
4c189e4 Stephan Hügel Tidying up
authored
100 connection.commit()
eb35055 Stephan Hügel Tweet formatting now in its own function
authored
101
Something went wrong with that request. Please try again.