-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroboDBWriter.py
More file actions
188 lines (155 loc) · 4.53 KB
/
roboDBWriter.py
File metadata and controls
188 lines (155 loc) · 4.53 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import tweepy
import time
from googlelangdetect import detect_language_v2
# exc_info is used for getting exceptions info
from sys import exc_info
from nltk import pos_tag, word_tokenize
import bpgsql
import re
def sanitize(w):
r = w;
r = r.replace("!"," ");
r = r.replace("'"," ");
r = r.replace('"',' ');
# r = r.replace("#","");
# r = r.replace("@","");
# r = r.replace("$","");
# r = r.replace("%","");
# r = r.replace("^","");
# r = r.replace("&","");
# r = r.replace("*","");
r = r.replace("("," ");
r = r.replace(")"," ");
r = r.replace("{"," ");
r = r.replace("}"," ");
r = r.replace("["," ");
r = r.replace("]"," ");
r = r.replace("<"," ");
r = r.replace(">"," ");
# r = r.replace("/"," ");
r = r.replace("\\"," ");
r = r.replace("|"," ");
r = r.replace("+"," ");
r = r.replace(";"," ");
r = r.replace("<"," ");
r = r.replace(">"," ");
# r = r.replace(":"," ");
r = r.replace("\t"," ");
r = r.replace("\r"," ");
r = r.replace("\n"," ");
return r
def isNotToBeIgnored(w):
return True
def CheckinWord(curs, w):
#print "word", w
if ((w<>"") & (w<>" ")):
try:
curs.execute("SELECT checkinword(E'%s')" % w.lower());
except:
print "PGsql checkinword failed!", exc_info()[0]
def CheckinLink(curs, lk):
print "LINK:", lk
if ((lk<>"") & (lk<>" ")):
try:
curs.execute("SELECT checkinlink(E'%s')" % lk);
except:
print "PGsql checkinlink failed!", exc_info()[0]
def SetinStatusCountry(curs, w, tid, country):
#print w, tid, country
if ((w<>"") & (w<>" ")):
try:
curs.execute("SELECT setinstatecountry(E'%(w)s', %(ts)d, %(tid)s, '%(country)s' )" % {"w":w.lower(), "ts":0, "tid":tid, "country":country});
except:
print "PGsql setinstate failed", exc_info()[0]
def SetinStatusCountryPlace(curs, w, tid, country, place):
#print w, tid, country, place
if ((w<>"") & (w<>" ")):
try:
#print type(place)
#print "SELECT setinstatecountryplace(E'%(w)s', %(ts)d, %(tid)s, '%(country)s', '%(place)s' )" % {"w":w.lower(), "ts":0, "tid":tid, "country":country, "place": re.escape(unicode(place))};
curs.execute("SELECT setinstatecountryplace(E'%(w)s', %(ts)d, %(tid)s, E'%(country)s', E'%(place)s' )" % {"w":w.lower(), "ts":0, "tid":tid, "country":country, "place": re.escape(unicode(place))});
except:
print "PGsql setinstate failed", exc_info()[0]
def SetLinkinStatusCountryPlace(curs, lk, tid, country, place):
if ((lk<>"") & (lk<>" ")):
try:
curs.execute("SELECT setlinkinstatecountryplace(E'%(lk)s', %(ts)d, %(tid)s, E'%(country)s', E'%(place)s' )" % {"lk":lk, "ts":0, "tid":tid, "country":country, "place": re.escape(unicode(place))});
except:
print "PGsql setlinkinstate failed", exc_info()[0]
def extractEmails(s):
r = []
s_rem = s
m = re.findall('[\w.]+@[\w]+\.[\w.]+', s)
if m:
for lk in m:
r.append(lk)
s_rem = s_rem.replace(lk,'')
r.append(s_rem)
return r
def extractLinks(s):
r = []
s_rem = s
m = re.findall('http[s]?://\w+[\w./!#]*', s)
if m:
for lk in m:
r.append(lk)
s_rem = s_rem.replace(lk,'')
r.append(s_rem)
return r
def extractLinksorEmails(s):
r = []
s_rem = s
m = re.findall('http[s]?://\w+[\w./!#]*|[\w.]+@[\w]+\.[\w.]+', s)
if m:
for lk in m:
r.append(lk)
s_rem = s_rem.replace(lk,'')
r.append(s_rem)
return r
def loadConfig():
config = {}
f = open("server.txt")
s = f.readlines()
f.close()
for c in s:
try:
config[c.split("=")[0]] = c.split("=")[1].replace("\r","").replace("\n","")
except:
print "."
return config
if __name__ == "__main__":
config = loadConfig()
while (True):
try:
pub_tw = tweepy.api.public_timeline();
print "Got %d tweets" % pub_tw.__len__()
for t in pub_tw:
time.sleep(0.3);
dbg = bpgsql.Connection(host=config["servername"], username= config["pguser"], password=config["pgpass"], dbname=config["databasename"])
curg = dbg.cursor()
if t.place:
place = t.place;
p = t.place['country'];
print p
else:
place = ""
p = ""
linkified = extractLinksorEmails(t.text)
#print "LINKIFIED:", linkified
for lk in linkified[:-1]:
CheckinLink(curg, lk)
SetLinkinStatusCountryPlace(curg, lk, t.id.__str__(), p, place)
for w in word_tokenize(sanitize(linkified[-1])):
try:
if isNotToBeIgnored(w):
sw = w
CheckinWord(curg, sw)
SetinStatusCountryPlace(curg, sw, t.id.__str__(), p, place)
except:
print "Failed", exc_info()[0], sw
dbg.close()
except:
print "Somthing went wrong here", exc_info()[0]
finally:
print time.time()
time.sleep(30);