-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.py
129 lines (100 loc) · 4.34 KB
/
add.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# add.py
# description: This is the script for adding scheduled tweets to the database
# copyrigtht: 2015 William Patton - PattonWebz
# licence: GPLv3
# @package: PWTwitterBot
import time, sys, datetime, argparse, re
# the below imports are imporing other files from this package
import dbconnect
from configuration import dbconfig
# start a parser
parser = argparse.ArgumentParser()
# these are the default running arguments
parser.add_argument("--tweetcontent", help="the tweet content", default="")
parser.add_argument("--tweettime", help="time to send the tweet at", default="")
parser.add_argument("--tweettype", help="either a tweet or a retweet", default="tweet")
args = parser.parse_args()
TWEETCONTENT = args.tweetcontent
TWEETTIME = args.tweettime
TWEETTYPE = args.tweettype
def checkInput(tweetContent, tweetTime, tweetType):
if ( tweetType == "tweet" or tweetType == "retweet" ) :
# we have the correct tweetType, continue
if tweetContent == "" :
print('tweetContent is empty, enter the tweet:')
tweetContent = raw_input('tweet=')
if tweetTime == "" :
print('tweetTime is empty, enter the date and time to send in yyyy-mm-dd hh:mm format')
tweetTime = raw_input('time=')
tweetValid = tweetValidate(tweetContent, tweetType)
timeValid = timeValidate(tweetTime)
if (tweetValid == False or timeValid == False) :
return (tweetContent, tweetTime, tweetType)
else :
print("something was invalid")
else :
# we need to break here - wrong tweetType is a problem we can't
# automatically overcome or bypass in a sane, user-friendly, way
print('Error in checkInput')
def tweetValidate(tweet_text, tweet_type):
# should get the max t.co length from a GET request sent to help/configuration
# hardcoded at 20 for now.
# perhaps not a good idea to get the max length here, a seporate function???
MAXTCOLENGTH = 20
charCount = len(tweet_text)
if ( tweet_type == 'retweet' and charCount > 18 ) :
print ("error with retweet length")
if charCount > 140 :
# longer than 140 characters, do some additional checks
print('over 140 chars')
## extract urls from the text using regex
# bug: this fails to capture the full url when a '#' appears
# bug: needs the 'http'/'https' - we should check for just 'www.'' too
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tweet_text)
for (url) in urls:
urlLength = len(url)
if urlLength < MAXTCOLENGTH :
charCount = charCount - urlLength
else :
charCount = (charCount - urlLength) + MAXTCOLENGTH
if charCount < 141 :
# character count now fits, break.
print("Tweet length now fits")
else :
print( "Tweet too long. Length: %d" % (charCount) )
return True
#end for loop
else :
# less than 140 characters, should be good to continue
print('less than 140 chars')
try:
charCount < 140;
return False
except:
raise ValueError("Bad Tweet lengh, should be under 140 chars.")
def timeValidate(date_text):
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d %H:%M')
return False
except ValueError:
raise ValueError("Incorrect date format, should be YYYY-MM-DD HH:MM")
def main():
# this is the main function for the program.
global TWEETCONTENT, TWEETTIME, TWEETTYPE
#print('%s' % (TWEETCONTENT))
#print('%s' % (TWEETTIME))
#print('%s' % (TWEETTYPE))
tweetContent, tweetTime, tweetType = checkInput(TWEETCONTENT, TWEETTIME, TWEETTYPE)
## connect to the database and set a cursor point
cnx = dbconnect.dbconnect(dbconfig)
cursor = dbconnect.dbcursor(cnx)
insertQuery = ('INSERT INTO ScheduledTweets '
'(tweetcontent, timetosend, sent, done, tweettype) '
'VALUES '
'("%s", "%s", "0", "0", "%s")' % (tweetContent, tweetTime, tweetType))
## run the query and commit to the databse if no error is reported
cursor.execute(insertQuery)
cnx.commit() #commit the changes to the database
if __name__ =='__main__':
main()