-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetSearch.py
67 lines (50 loc) · 1.67 KB
/
tweetSearch.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
#!/usr/bin/python
import urllib
import simplejson
import sys
import cgitb
import cgi
import keyword_extract
import simplejson, json
from xml.dom.minidom import parseString
from string import rfind
#from twsearch import searchTweets
def getTweets(query):
#print "<p>Search Value is:", query
#print "<p>"
search = urllib.urlopen("http://search.twitter.com/search.json?q="+query)
# dict is a dictionary created by parsing the search string
#all tweets are in dict["result"], which is a list of dictionaries
resultDict = simplejson.loads(search.read())
results = []
if resultDict and "results" in resultDict.keys():
for result in resultDict["results"]:
results.append(result)
#print "<p>"
#print "*",result["text"].encode('utf-8'),"\n"
return results
#print searchTweets(query)
def getWebPgTweets(url, no_keywords):
webpage = keyword_extract.getWebPg(url)
keywords = keyword_extract.freqWords(webpage, no_keywords)
keywordTweets = dict()
for keyword in keywords:
tweets = getTweets(keyword)
extractList = []
for t in tweets:
tempdic = dict()
tempdic["text"] = t["text"]
tempdic["from_user"] = t["from_user"]
tempdic["from_user_name"] = t["from_user_name"]
tempdic["profile_image_url"] = t["profile_image_url"]
extractList.append(tempdic)
keywordTweets[keyword] = extractList
return json.dumps(keywordTweets)
# For webpage DEMO
cgitb.enable()
form = cgi.FieldStorage()
query = form.getvalue("urllink")
keywords = form.getvalue("keywords")
no_of_kw = int(keywords)
print "Content-type: text/html\n\n"
print getWebPgTweets(query, no_of_kw)