本文整理汇总了Python中models.Tweet类的典型用法代码示例。如果您正苦于以下问题:Python Tweet类的具体用法?Python Tweet怎么用?Python Tweet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tweet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: search_tweets
def search_tweets(search_query):
result = {}
json_data = raw_to_json(Tweet.search(search_query.replace("&", " ").split())[0])
result[search_query] = json_data
json_data = raw_to_json(Tweet.search(search_query.replace("&", " ").split())[1])
result[search_query+"_or"] = json_data
return jsonify(result)
示例2: get_context_data
def get_context_data(self, *args, **kwargs):
context = super(TestTweetsView, self).get_context_data(*args, **kwargs)
# retrieve tweets from db whose actual classifications have been set (already verified from previous tests)
tweets_train_tourism = Tweet.objects.filter(classified=True, actual_classification='tourism')
tweets_train_nontourism = Tweet.objects.filter(classified=True, actual_classification='nontourism')
# train classifier using tourism and nontourism files and tweets from db
train_result = train_db(tweets_train_tourism, tweets_train_nontourism)
classifier = train_result['classifier']
# retrieve random tweets from a file
tweets = self.randomize_tweets('classifier/data/2015-03-06.happydata.txt', 10)
tweets_test = []
for tweet in tweets:
if not Tweet.objects.filter(tweet_id=tweet['id']) and tweet['coordinates']:
tweet_id = tweet['id']
user = tweet['user']['name'].encode('utf-8')
lat = tweet['coordinates']['coordinates'][1]
lng = tweet['coordinates']['coordinates'][0]
text = tweet['text'].encode('utf-8')
if tweet_id and user and lat and lng and text:
# classify tweet
classification = classifier.classify(feature_extractor_lda_tripadvisor_top_words_weights(text))
# save tweet with classification (to be verified later)
tweet_obj = Tweet(tweet_id=tweet_id, user=user,
lat=lat, lng=lng, text=text,
classification=classification)
tweet_obj.save()
tweets_test.append(tweet_obj)
context['tweets'] = tweets_test
return context
示例3: schedule
def schedule(request):
if request.POST.get('auth') != settings.TWITGHOST_AUTH_KEY:
logging.error("Unauthorized access")
return HttpResponse('forbidden', status=403)
new_tweet = Tweet(tweet=request.POST['tweet'])
new_tweet.save()
return HttpResponse('ok', mimetype="text/xml")
示例4: tweet
def tweet(request):
if request.method == 'POST':
form = TweetForm(request.POST)
if form.is_valid():
twit = Tweet()
status = twit.tweet = form.cleaned_data['tweet']
twit.name = request.user.username
twit.save()
variables = RequestContext(request,{
'status':status
})
return render_to_response('tweet.html',RequestContext(request),variables)
else:
return render_to_response('tweet.html',{'request':RequestContext(request)})
else:
tweets = []
queryset=Tweet.objects.all()
for query in queryset:
if request.user.username == query.name:
tweets.append(query)
status = ''
for tweet in tweets:
status = tweet.tweet
form = TweetForm()
form.name = request.user
variables = RequestContext(request, {
'form': form,'status': status
})
return render_to_response('tweet.html',RequestContext(request),variables)
示例5: feed
def feed(request, username):
url = "https://twitrss.me/twitter_user_to_rss/?user=" + username
dicc = feedparser.parse(url)
out = ""
for number in range(5):
out += dicc.entries[number].title + "<br>"
#Obtener las urls del tweet si hay
urls = dicc.entries[number].title.split()
for i in urls:
if i.startswith("http://") or i.startswith("https://"):
i = i.split('&')[0]
out += "<li><a href=" + i + ">" + i + "</a></li>"
#Obtener primer elemento <p>
soup = BeautifulSoup(urllib.urlopen(i).read())
out += str(soup.p).decode('utf8')
#Obtener primer elemento <img>
out += str(soup.img).decode('utf8') + "<br><br>"
#Comprobar y guardar autor del tweet
user = dicc.entries[number].title.split(':')[0]
try:
p = User.objects.get(name=user)
except ObjectDoesNotExist:
p = User(name=user)
p.save()
#Comprobar y guardar tweet
try:
t = Tweet.objects.get(content=dicc.entries[number].title)
except ObjectDoesNotExist:
t = Tweet(content=dicc.entries[number].title, url= dicc.entries[number].link, name=p)
t.save()
return HttpResponse(out)
示例6: do
def do(self):
logger.info("Initiating cronjob: {}".format(self.code))
try:
auth = tweepy.OAuthHandler(self._tak, self._tas)
auth.set_access_token(self._tat, self._tats)
api = tweepy.API(auth)
screen_name = api.me().screen_name
timeline = api.home_timeline()
except Exception:
e = traceback.format_exc()
logger.error("Could not get home timeline. Error: {}".format(self.screen_name, e))
return False
for t in timeline:
if t.user.screen_name == screen_name:
msg = t.text
if msg.startswith('RT @everycolorbot'):
chex = msg.split()[2].strip("\"")
evcinst = EveryColorBotTweet.objects.get_or_none(color__hex = chex)
if evcinst is not None:
evcinst.tweeted = True
evcinst.save()
inst = Tweet.objects.get_or_none(message = msg)
if inst is None:
logger.info('Cronjob encountered unsaved tweet: "{}" Saving it to database.'.format(msg))
inst = Tweet(color_code = chex, message = msg)
inst.save()
示例7: post
def post(self):
data = request.json
validation_error = validate_user(data)
if validation_error is not None:
return validation_error
user = get_user(data['user']['username'])
if user is None:
return jsonify(status=400, message="user does not exist")
if user.password != data['user']['password']:
return jsonify(status=403, message="incorrect password")
text = data['text']
if(len(text) > 140):
return jsonify(status=400, message="tweet too long"), 400
if(len(text) == 0):
return jsonify(status=400, message="empty tweet"), 400
for word in [u"#arsenal", u"#denfølelsen"]:
if word in text.lower():
abort(418) # I am a teapot
tweet = Tweet(user, text)
db['tweets'].append(tweet)
return jsonify(tweet.serialize()), 200
示例8: fromUser
def fromUser(self, screen_name, tweets_number=10, is_bot=False):
user = self.createUser(screen_name, is_bot)
tweets = self.twitter_client.user_timeline(screen_name=screen_name, count=tweets_number)
for i, status in enumerate(tweets):
tweet = status._json
text = tweet['text']
date = tweet['created_at']
entities = tweet['entities']
user_mentions = entities['user_mentions']
mentions_list = []
if len(user_mentions) > 0:
for mention in user_mentions:
mentions_list.append(mention['screen_name'])
text_string = unicodedata.normalize('NFKD', text).encode('ascii','ignore')
date_string = unicodedata.normalize('NFKD', date).encode('ascii','ignore')
name_mentions_string = ",".join(mentions_list)
Tweet.create(
user = user,
text = text_string,
date = date_string,
source = status.source,
mentions = name_mentions_string
)
示例9: get
def get(self):
self.response.write(self.request.get("keyword"))
# Prompt for login credentials and setup stream object
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
keyword = self.request.get("keyword")
count =0
search_results = api.search(q=keyword, result_type = "recent", count=100)
for tweet in search_results:
if tweet.place != None:
count+=1
tweet.text.replace("\n", " ");
text = tweet.text.encode('utf8')
print text
#logging.info(tweet.text)
latitude = tweet.place.bounding_box.coordinates[0][0][0]
longtitude = tweet.place.bounding_box.coordinates[0][0][1]
e = Tweet(search_key=keyword, text=tweet.text, latitude= latitude, longtitude= longtitude)
e.put()
示例10: search
def search(search_query):
search_query_strings = search_query.replace("&", " ").split()
json_data = {
"tweets" : raw_to_json(Tweet.search(search_query_strings)[0]),
"tweets_or": raw_to_json(Tweet.search(search_query_strings)[1]),
"hashtags" : raw_to_json(Hashtag.search(search_query_strings)[0]),
"hashtags_or": raw_to_json(Hashtag.search(search_query_strings)[1])
}
return jsonify(json_data)
示例11: buscar_en_todos_los_tweets
def buscar_en_todos_los_tweets(busqueda, cantidad_de_tweets=50, partir_del_tweet=None):
## App keys
APP_KEY = 'IR5sQyceHa34Cxxm2hAw'
APP_SECRET = 'y5Nti0zdCFfzY1ifokC6iHIZFZ14Z2GAjTI6VFx2mg'
## User keys
OAUTH_TOKEN = '202826188-6OCQEmzi7JCu8S6UERP5nhjUo4NHirk0Kd4VRRvl'
OAUTH_TOKEN_SECRET = '1wmkXUkqkDhBnbugzaNIzf07VKeCddjEPynVG2gayU'
# requires authentication as of Twitter API v1.1
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
try:
search_results = twitter.search(q=busqueda, count=cantidad_de_tweets)
print '===============', busqueda, '==============='
except TwythonError as e:
print e
# Initialize array of tweet objects
tweets = []
for tweet in search_results['statuses']:
# Parameters of a tweet
tweet_id = tweet['id_str']
tweet_user = tweet['user']['screen_name'].encode('utf-8')
tweet_created_at = tweet['created_at']
tweet_text = tweet['text'].encode('utf-8')
tweet_reply_to_status_id = tweet['in_reply_to_user_id_str']
tweet_retweet_count = tweet['retweet_count']
try:
Tweet.objects.get(pk=tweet_id)
print "Tweet %s already in the db." % tweet_id
except:
# Print in console
print 'Added %s.- Tweet from @%s Date: %s' % (tweet_id, tweet_user, tweet_created_at)
print tweet_text, '\n', 'reply to %s' % tweet_reply_to_status_id
## Change twitter api time to python datetime
## ej. Wed Jan 16 22:08:18 +0000 2013
tweet_created_at = datetime.strptime(tweet_created_at, "%a %b %d %H:%M:%S +0000 %Y").replace(tzinfo=utc)
tmp_tweet = Tweet(id=tweet_id, user=tweet_user, text=tweet_text, created_at=tweet_created_at, retweet_count=tweet_retweet_count)
add_tweet_search = TwitterSearch(search=busqueda, tweet=tmp_tweet)
# extract, save and link hastags with tweet
extract_hash_tags(tmp_tweet)
# save newly created objects
tmp_tweet.save()
add_tweet_search.save()
## Tests are to be performed here
# buscar_en_todos_los_tweets('#uabc')
示例12: on_success
def on_success(self, data):
if 'text' in data:
data['fetched_timestamp'] = datetime.datetime.now()
data['fresh_tweet'] = True
tweet = Tweet()
tweet.tweet_id = data['id_str']
tweet.tweets.append(data)
tweet.save()
print("saved", self.i)
self.i += 1
示例13: save_raw
def save_raw(results: Generator) -> Generator:
"""Save the results and yields the id values
"""
for result in results:
id_str = result.__dict__.get('id_str')
data = result.__dict__.get('_json')
tweet = Tweet(id_str)
tweet.save(data)
yield id_str
示例14: tweets
def tweets(handle):
statuses = api.GetUserTimeline(screen_name=handle)
tweet_list = []
for s in statuses:
tweet = Tweet(id = s.id, text=s.text, retweet_count = s.retweet_count,
created_at = convert_datetime(s.created_at), influence_score=influence_score_calc(s.text),
picture = (None if s.media==[] else s.media[0].get("media_url", None)))
tweet_list.append(tweet)
return jsonify(tweets=[tweet.serialize() for tweet in tweet_list])
示例15: tweet_add
def tweet_add():
user = current_user()
if user is None:
return redirect(url_for('login_view'))
else:
t = Tweet(request.form)
# 设置是谁发的
t.user = user
# 保存到数据库
t.save()
return redirect(url_for('timeline_view', username=user.username))