def save(request): data = {} data['success'] = False data['error'] = None data['uri'] = '/accounts/login/' try: if request.POST: id = request.POST.get('id') text = request.POST.get('tweet') user = request.user if len(id) > 0: tweet = Tweet.objects.get(id=id) tweet.editor = user else: tweet = Tweet(tweet=text, author=user) tweet.save() data['success'] = True if user.has_perm('tweets.is_author'): data['uri'] = '/tweet_app/author/' elif user.has_perm('tweets.is_editor'): data['uri'] = '/tweet_app/editor/' else: data['error'] = "How'd you get here? This is an ajax call" except: data['error'] = 'There was an error' return HttpResponse(json.dumps(data), content_type='application/json')
def scrapper(request): consumer_key="36UBmR5MKNhfdOYlYHYdA" consumer_secret="bzvGib4mEpemMLHjgNUNwxFYikK6IECr7oufScNZ7g" access_token="127869815-d6EWusWl2rTwZtb78CEEk8xvOiyn2sLhRh0p8YfS" access_token_secret="ebJaRWFWILwTtSfB34BH9P2dNXAAIUkXKngvnwPRxE" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) try: api = tweepy.API(auth) except api.DoesNotExist: raise Http404 name=api.me().name tweets = api.search(q="#nrf14", count="2500") for tweet in tweets: if tweet.in_reply_to_status_id_str: reply_status=True else: reply_status=False if tweet.retweet_count==0: retweet_status=False else: retweet_status=True t=Tweet(text=tweet.text,time=tweet.created_at,retweet=retweet_status,reply=reply_status) t.save() return render(request, 'tweets/scrapper.html', {'name': name})
def moderate(request): data = {} data['error'] = None data['moderate'] = False data['reasons'] = [] try: if request.POST: id = request.POST.get('id') text = request.POST.get('tweet') user = request.user if len(id) > 0: tweet = Tweet.objects.get(id=id) tweet.tweet = text tweet.editor = user else: tweet = Tweet(tweet=text, author=user) moderate, reasons = tweet.should_we_moderate() if not moderate or len(id) > 0: tweet.save() data['id'] = tweet.id data['moderate'] = moderate data['reasons'] = reasons else: data['error'] = "How'd you get here? This is an ajax call" except: data['error'] = 'There was an error' return HttpResponse(json.dumps(data), content_type='application/json')
def create(self, validated_data): """Create and return a new user.""" username = validated_data['usuario'] contenido = validated_data['contenido'] print(username) print(contenido) print(validated_data) try: usuario = Usuario.objects.get(username=username) except Usuario.DoesNotExist: usuario = Usuario(username=username, nombre='Jonh Doe', correo='*****@*****.**') usuario.save() tweet = Tweet( usuario=usuario, contenido=contenido, ) tweet.fecha = datetime.now() tweet.save() #numero_cuenta = datetime.now().strftime('%Y%m%d%H%M%S') #cuenta.numero = numero_cuenta + str(cuenta.id) #cuenta.save() return tweet
def post(self, request): data = json.loads(request.body.decode('utf-8')) new_tweet = Tweet(content=data.get('content')) new_tweet.save() response = serializers.serialize("json", [new_tweet]) return HttpResponse(response, content_type="application/json")
def post(self, request, username): form = TweetForm(self.request.POST, request.FILES) if form.is_valid(): user = User.objects.get(username=username) text = form.cleaned_data['text'] image = form.cleaned_data['image'] tweet_file = form.cleaned_data['tweet_file'] tweet = Tweet(text=text, user=user, tweet_file=tweet_file, image=image) tweet.save() words = text.split() for word in words: if word[0] == "#": hashtag, created = Hashtag.objects.get_or_create( name=word[1:], hashtag_count=+1) hashtag.tweet.add(tweet) return HttpResponseRedirect('/home/' + username) else: form = TweetForm() user = User.objects.get(username=username) tweets = Tweet.objects.filter(user=user) tweet_ers = 'invalid tweet' context = { 'user': user, 'form': form, 'tweets': tweets, 'tweet_ers': tweet_ers } return render(request, 'base/base.html', context)
def index(request): user_tweets = Tweet.objects.filter(author=request.user, for_editors=False, twitter_id=None) message = '' text = '' id = None if request.POST: tweet = None text = request.POST.get('tweet') action = request.POST.get('action') if action == 'publish': try: id = request.POST.get('id') tweet = Tweet.objects.get(id=id) tweet.tweet = text tweet.save() success, error, text = tweet.publish() tweet.save() if not success: message = 'Your message was not properly authored. Sending to editors.' except: message = 'There was an error publishing' elif action == 'save': tweet = Tweet(tweet=text, author=request.user) tweet.save() data = {'user_tweets': user_tweets, 'message': message, 'text': text, 'id': id } return render(request, 'tweets/author/index.html', data)
def get_historic_tweets(club_nm): print club_nm #build api_params build_params() tot_time_start = time.time() query = query_bulider(club_nm) top_id = str(get_top_id(query)) last_id = update_since_id_file(club_nm, top_id) prev_id = top_id i = 0 advance = True collected_tweets = set() index = 0 while(advance): # if program has been running for > 45 minutes, get new tokens if time.time() - tot_time_start > REFRESH_TIME: build_params() start = time.time() data = query_twitter_api(query, count=100, max_id=prev_id, since_id=last_id, index=index, result_type="popular") if data["statuses"] == []: print "end of data" advance = False else: prev_id = int(data["statuses"][-1]["id"])-1 i += 1 for status in data["statuses"]: txt = status["text"].encode('utf-8') if txt in collected_tweets: continue else: collected_tweets.add(txt) c = convert_datetime(status["created_at"]) f_ct = status["favorite_count"] id_str = status["id_str"] rt_ct = status["retweet_count"] h_tags = extract_hashtags(status["entities"]["hashtags"]) is_rt = True if "retweeted_status" in status.keys() else False url = build_tweet_url(status["user"]["screen_name"], id_str) # hit Aylien API to analyze text sentiment = analyze_text(txt) # if sentiment["polarity_confidence"] < .8: # continue sent = sentiment['polarity'] t = Tweet(created=c, url=url, team=club_nm, favorites=f_ct, tweet_id=id_str, retweets=rt_ct, text=txt,hashtags=h_tags, is_retweet=is_rt, sentiment=sent) print t t.save() end = time.time() print i, end-start index += 1 index = index % 10
def handle(self, *app_labels, **options): from tweets.models import Tweet import httplib2 import feedparser import pprint import pickle import datetime search = options.get('search',None) if not search: print "need a search term" exit() show_traceback = options.get('traceback', False) search_url = "http://search.twitter.com/search.atom?q=" h = httplib2.Http() resp, content = h.request(search_url + search) d = feedparser.parse(content) for entry in d.entries: raw_entry = pickle.dumps(entry) raw_entry = raw_entry.encode('base64') raw_entry = unicode(raw_entry) raw_content = entry.content[0]["value"] created = datetime.datetime( entry.published_parsed[0], entry.published_parsed[1], entry.published_parsed[2], entry.published_parsed[3], entry.published_parsed[4], entry.published_parsed[5], ) tweet_link = entry.link author = entry.author try: tweet = Tweet.objects.get(tweet_link=tweet_link) except Tweet.DoesNotExist: tweet = Tweet( raw_atom_entry=raw_entry, raw=raw_content, created=created, tweet_link=tweet_link, author=author, ) music_url = tweet.music_link_finder() if music_url: tweet.music_link = music_url tweet.save()
def post(self, request, username): # form = TweetForm(self.request.POST) user = User.objects.get(username=username) tweet = Tweet(text=request.POST.get('text'), user=user, country='UA') tweet.save() words = request.POST.get('text').split(" ") for word in words: if word[0] == '#': hasht, created = HashTag.objects.get_or_create(name=word[1:]) hasht.tweets.add(tweet) return HttpResponseRedirect('/user/' + username)
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username=username) ## tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet = Tweet(text=form.cleaned_data['text'], user=user) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/'+username)
def handle(self, *args, **options): Tweet.objects.all().delete() for x in range(100): t = Tweet( uid=str(randint(10000, 20000)), handle=choice(self.HANDLES), country=choice(self.REGIONS), image_url=choice(self.IMAGES), content=choice(self.TWEETS), ) t.save() self.stdout.write("Created %d" % t.pk)
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data["text"], user=user, country=form.cleaned_data["country"] ) tweet.save() words = form.cleaned_data["text"].split(" ") for word in words: if word[0] == "#": hashTag, created = HashTag.objects.get_or_create(name=word[1:]) hashTag.tweet.add(tweet) return HttpResponseRedirect("/user/" + username + "/?page=1")
def restore_object(self, attrs, instance=None): if instance: instance.lat = attrs.get('lat', instance.lat) instance.lng = attrs.get('lng', instance.lng) instance.text = attrs.get('text', instance.text) return instance return Tweet(attrs.get('lat'), attrs.get('lng'), attrs.get('text'))
def post(request, username): form = TweetForm(request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet( text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country'], ) tweet.save() words = form.cleaned_data['text'].split(' ') for word in words: if word[0] == '#': tag, created = HashTag.objects.get_or_create(name=word[1:]) tag.tweet.add(tweet) return HttpResponseRedirect(reverse('profile', args=[user])) return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def post(self, request, username): form = TweetForm(self.request.POST) if form.is_valid(): user = User.objects.get(username=username) tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") for word in words: if word[0] == "#": hashtag, created = HashTag.objects.get_or_create(name=word[1:]) hashtag.tweet.add(tweet) else: print form return HttpResponseRedirect('/user/' + username)
def handle(self, *args, **options): consumer_key = os.environ.get('CONSUMER_KEY') consumer_secret = os.environ.get('CONSUMER_SECRET') access_token_key = os.environ.get('ACCESS_TOKEN_KEY') access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret) tashkent = ['69.140877', '41.209011', '69.397034', '41.401421'] r = self.get_stream_with_track(api, tashkent) for item in r: # print(json.dumps(item)) user = User() # user.id = item['user']['id_str'] user.name = item['user']['name'] user.screen_name = item['user']['screen_name'] user.location = item['user']['location'] user.description = item['user']['description'] user.profile_image = item['user']['profile_image_url'] user.save() tweet = Tweet() tweet.text = item['text'] tweet.timestamp = item['timestamp_ms'] tweet.user = user tweet.save() print(item['user']['name'], '-->', item['text'])
def post(self, request, username): form = TweetForm(self.request.POST) form.full_clean() if form.is_valid: user = User.objects.get(username = username) if user is not None: tweet = Tweet( text = form.cleaned_data['text'], country = 'China', user = user, ) tweet.save() texts = form.cleaned_data['text'].split(' ') for text in texts: if text.startswith('#'): hashtag, created = HashTag.objects.get_or_create(name = text[1:]) hashtag.tweet.add(tweet) return HttpResponseRedirect('/user/' + username)
def save_tweet_orm(self, tweet, request): """ Ajoute un tweet dans l'ORM de Django : tweet : tuple (TweetObject, category, tweet a ajouter au fichier : requete : requete ayant permis la recuperation du tweet """ tweet, category = tweet self.clean_tweet(tweet) date_today = datetime.date(datetime.now()) returned_tweet = Tweet(id=tweet.id, user=tweet.user.name, text=tweet.text, date=date_today, request=request, category=category) returned_tweet.save() self.logger.info("Tweet (%d) has been added to the orm" % tweet.id) return returned_tweet
def handle(self, *args, **options): consumer_key = os.environ.get('CONSUMER_KEY') consumer_secret = os.environ.get('CONSUMER_SECRET') access_token_key = os.environ.get('ACCESS_TOKEN_KEY') access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') api = TwitterAPI( consumer_key, consumer_secret, access_token_key, access_token_secret ) tashkent = [ '69.140877', '41.209011', '69.397034', '41.401421' ] r = self.get_stream_with_track(api, tashkent) for item in r: # print(json.dumps(item)) user = User() # user.id = item['user']['id_str'] user.name = item['user']['name'] user.screen_name = item['user']['screen_name'] user.location = item['user']['location'] user.description = item['user']['description'] user.profile_image = item['user']['profile_image_url'] user.save() tweet = Tweet() tweet.text = item['text'] tweet.timestamp = item['timestamp_ms'] tweet.user = user tweet.save() print(item['user']['name'], '-->', item['text'])
def add_tweets(new_tweets): # New tweets are saved to the database tweets = new_tweets for i in tweets['statuses']: if not Tweet.objects.filter(tweet_id=i['id']).exists(): tweet = Tweet() tweet.tweet_id = str(i['id']) tweet.username = i['user']['screen_name'] tweet.text = i['text'] tweet.created_at = datetime.strptime(i['created_at'], '%a %b %d %H:%M:%S %z %Y') tweet.save() analyze_tweets() return
def handle(self, *args, **options): user = User(username="******", first_name="Sreevenkat", last_name="Ganapathi", email="*****@*****.**", is_superuser=True) user.set_password("hello") user.save() user1 = User(username="******", first_name="SivaShankar", last_name="Ramesh", email="*****@*****.**") user1.set_password("hello") user1.save() hello_tweet = Tweet(text="Hello twitter. Hit me up.", user=user) hello_tweet = hello_tweet.save() reply_tweet = Tweet(text="Here take it.", user=user1) reply_tweet = reply_tweet.save() reply_object = TweetReplies(tweet=reply_tweet, reply_to=hello_tweet) reply_object.save()
def handle(self, *args, **options): username = "******" if not User.objects.filter(username=username).exists(): password = getenv("ADMIN_PASSWORD") admin = User.objects.create_user(username=username, password=password) tweet_type = ContentType.objects.get(app_label='tweets', model='tweet') admin.user_permissions.add(Permission.objects.get(codename='view_tweet', content_type=tweet_type)) admin.save() tweet = Tweet() tweet.link = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" tweet.link_text = "Congratulations" tweet.text = getenv("FLAG") tweet.user = admin tweet.save()
def post(self, request, username): form = TweetForm(request.POST) if form.is_valid(): user = User.objects.get(username=username) #Создаем объект и сохраняем алгоритм понятен из ссылки вытянули имя пользователя, по нему объект с которым свяжем Твит остальное из формы tweet = Tweet(text=form.cleaned_data['text'], user=user, country=form.cleaned_data['country']) tweet.save() words = form.cleaned_data['text'].split(" ") #Ага после того как создали твит нам нужно найти в тексте хэштег мы его разбили по словам и если нашли то создаем #хєштег метод get_or_create вернет сам объект и булево значение если создан объект то ТРУ или ФАЛСЕ если найден for word in words: if word[0] == '#': hashtag, created = HashTag.objects.get_or_create( name=word[1:]) #Затем т.к у нас пока в объекте хэш тега только имя, нам нужно еще добавить в немуу ссылку на объект твита что делается методом add это для полей ManyToMany hashtag.tweet.add(tweet) #return HttpResponse(" ".join(words)) return HttpResponseRedirect("/user/" + username)
def create_charge(cls, userobj, addonobj, mixtapeobj): cu = userobj.customer ao = addonobj mt = mixtapeobj char = cu.charge(amount=Decimal(ao.price), description=addonobj.name) print '%s charge created for %s' % (ao.name, userobj.username) if ao.duration: if not mt.releaseDatetime > datetime.now(pytz.utc): edt = datetime.now(pytz.utc) + relativedelta(days=ao.duration) else: if ao.name in [ 'Basic Mixtape Page Countdown', 'Enhanced Homepage Mixtape Countdown' ]: edt = mt.releaseDatetime else: edt = mt.releaseDatetime + relativedelta(days=ao.duration) else: edt = None ac = AddonCharge(end_datetime=edt, charge=char, addon=ao, content_type_id=mt.get_content_type_id(), object_id=mt.id, updated_by=userobj, created_by=userobj) ac.save() # mt.addons= ac # mt.save() tweet = AddonType.objects.get(name='Tweet Your Mixtape') #Create tweet if this is the tweet addon if ac.addon == tweet and ac.id: Tweet.maketweet(mixtapeobj) return ac
def get_twitter_tweets(search_text, page_size=1000): url = settings.TWITTER_API + '?q=%23' + search_text + '&count=' + str( page_size) + '&result_type=mixed&lang=en' tweets = twitter_oauth_req(url, settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_TOKEN_SECRET) tweets = json.loads(tweets.decode("utf-8")) statuses = tweets.get('statuses', []) twitter_text_list = [] for stat in statuses: created_time = datetime.strptime(stat['created_at'], '%a %b %d %H:%M:%S %z %Y') twitter_text_list.append( Tweet(text=stat['text'], created_by_image=stat['user']['profile_image_url'], created_by_name=stat['user']['name'], created_at=created_time, url='https://twitter.com/' + stat['user']['screen_name'] + '/status/' + stat['id_str'])) Tweet.objects.bulk_create(twitter_text_list)
def construct_tweet(self, data, hashtag): """ Use the information of data we actually are gonna use in the analysis and create a Tweet object from this :param data: The raw tweet as received from the stream :param hashtag: A hashtag object relevant to the search query :return: """ date = timezone.now() # Try & catch to prevent crashing when a tweet is not sent correctly try: tweet = Tweet(hashtag=hashtag, tweet=data['text'], date_time=date) processed_tweet = self.tweet_processor.process_tweet(tweet.tweet) tweet.sentiment_score = self.analyser.analyse(processed_tweet) tweet.sentiment_name = Sentiment.sentiments.filter( minimum_sentiment__lte=tweet.sentiment_score).order_by( 'minimum_sentiment').last() tweet.save() # Broad exception here because we dont really care what kind of error it is except: print("error")
def add_user_to_everyone_group(sender, instance, created, raw, **kwargs): if raw is False: if created is True: everyone.user_set.add(instance) me = Tweet(message=instance.username, type="person") me.save()
def handle(self, *args, **options): if options['from_csv']: # load from filename with open(options['from_csv'],'r') as csv_file: csv_reader = csv.reader(csv_file) header = csv_reader.next() names = [] for row in csv_reader: names.append(row[1]) print names encounters = FatalEncounter.objects.filter(name__in=names) else: # load all FatalEncounters from db encounters = FatalEncounter.objects.filter(proofed=True) if options['only_black'] == True: print "only black FatalEncounters" encounters = encounters.filter(race='BLACK') print 'got',len(encounters),'encounters' last_order = Tweet.objects.filter(order__isnull=False).aggregate(Max('order'))['order__max'] if last_order: i = last_order else: i = 0 for fe in encounters: if DoNotSend.objects.filter(fatal_encounter=fe).count(): print "do not send tweet for", fe continue if options['overwrite_existing']: try: tweet = fe.tweet #print "overwriting existing tweet" if tweet.tweet_sent: print "tweet sent, skipping" continue except Tweet.DoesNotExist: tweet = Tweet(fatal_encounter=fe) else: tweet = Tweet(fatal_encounter=fe) i += 1 tweet.order = i d = model_to_dict(fe) #shorten police department names for replace,short in AGENCY_ACRONYMS.items(): d['agency_responsible'] = d['agency_responsible'].replace(replace,short) tweet.text = render_to_string('tweet.txt', d) if options['attach_image']: try: image_id = IMAGE_ID_LOOKUP[fe.name] tweet.share_image_url = "https://killedbycops.s3.amazonaws.com/%s/%s.jpg" % (options['image_prefix'], image_id) MAX_TWEET_LENGTH = 120 except KeyError: print "unable to lookup", fe.name, "from IMAGE_ID_LOOKUP" MAX_TWEET_LENGTH = 140 if len(tweet.text) > MAX_TWEET_LENGTH: print "ERROR: tweet text > MAX_TWEET_LENGTH" print "OVER MAX @", len(tweet.text) print tweet.text #if len(tweet.text)+19 <= MAX_TWEET_LENGTH: # tweet.text = tweet.text + ' via @ColorOfChange' # use shorter template? tweet.save() fe.tweet = tweet fe.save() print "saved", tweet
def post_tweet(self, content): tweet = Tweet(content=content, tweeter=self) tweet.save() self.tweets.add(tweet)
def create_tweet(user_id, text, tag_id): Tweet.get_or_create(user__id=user_id, text=text, tag__id=tag_id)
def handle(self, *args, **options): if options['from_csv']: # load from filename with open(options['from_csv'], 'r') as csv_file: csv_reader = csv.reader(csv_file) header = csv_reader.next() names = [] for row in csv_reader: names.append(row[1]) print names encounters = FatalEncounter.objects.filter(name__in=names) else: # load all FatalEncounters from db encounters = FatalEncounter.objects.filter(proofed=True) if options['only_black'] == True: print "only black FatalEncounters" encounters = encounters.filter(race='BLACK') print 'got', len(encounters), 'encounters' last_order = Tweet.objects.filter(order__isnull=False).aggregate( Max('order'))['order__max'] if last_order: i = last_order else: i = 0 for fe in encounters: if DoNotSend.objects.filter(fatal_encounter=fe).count(): print "do not send tweet for", fe continue if options['overwrite_existing']: try: tweet = fe.tweet #print "overwriting existing tweet" if tweet.tweet_sent: print "tweet sent, skipping" continue except Tweet.DoesNotExist: tweet = Tweet(fatal_encounter=fe) else: tweet = Tweet(fatal_encounter=fe) i += 1 tweet.order = i d = model_to_dict(fe) #shorten police department names for replace, short in AGENCY_ACRONYMS.items(): d['agency_responsible'] = d['agency_responsible'].replace( replace, short) tweet.text = render_to_string('tweet.txt', d) if options['attach_image']: try: image_id = IMAGE_ID_LOOKUP[fe.name] tweet.share_image_url = "https://killedbycops.s3.amazonaws.com/%s/%s.jpg" % ( options['image_prefix'], image_id) MAX_TWEET_LENGTH = 120 except KeyError: print "unable to lookup", fe.name, "from IMAGE_ID_LOOKUP" MAX_TWEET_LENGTH = 140 if len(tweet.text) > MAX_TWEET_LENGTH: print "ERROR: tweet text > MAX_TWEET_LENGTH" print "OVER MAX @", len(tweet.text) print tweet.text #if len(tweet.text)+19 <= MAX_TWEET_LENGTH: # tweet.text = tweet.text + ' via @ColorOfChange' # use shorter template? tweet.save() fe.tweet = tweet fe.save() print "saved", tweet