-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter_listener.py
42 lines (33 loc) · 1.07 KB
/
twitter_listener.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
from django.core.management import setup_environ
import settings
setup_environ(settings)
import pycurl, json
import re
from actions.models import Action
hashtag = "#doadoing"
STREAM_URL = "https://stream.twitter.com/1/statuses/filter.json"
WORDS = "track=" + hashtag
USER = "_martinariel"
PASS = "martin1793"
insensitive_hippo = re.compile ( re.escape ( hashtag ), re.IGNORECASE)
def on_tweet(data):
try :
tweet = json.loads(data)
content = tweet['text']
content = insensitive_hippo.sub('', content )
action = Action()
action.content = content[:70]
action.from_ip = 'Twitter'
print (action.content)
action.save()
print ( action.id )
except :
return
conn = pycurl.Curl()
conn.setopt(pycurl.POST, 1)
conn.setopt(pycurl.POSTFIELDS, WORDS)
conn.setopt(pycurl.HTTPHEADER, ["Connection: keep-alive", "Keep-Alive: 3000"])
conn.setopt(pycurl.USERPWD, "%s:%s" % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL)
conn.setopt(pycurl.WRITEFUNCTION, on_tweet)
conn.perform()