def fetch_related_tweets(cls, medias): import constants import tweepy from model import Tweet auth_handler = tweepy.OAuthHandler(consumer_key=constants.TWITTER_CONSUMER_KEY, consumer_secret=constants.TWITTER_CONSUMER_SECRET) auth_handler.username = '******' auth_handler.set_access_token(constants.TWITTER_ACCESS_TOKEN, constants.TWITTER_TOKEN_SECRET) api = tweepy.API(auth_handler) total = 0 for media in medias: if media.last_twitter_fetch and \ (datetime.datetime.now() - media.last_twitter_fetch).seconds < 3600: continue try: tweets = tweepy.Cursor(api.search, q=media.host_id, rpp=10, result_type="recent", include_entities=True, lang="en").items() for tweet in tweets: total += 1 if not tweet.user.screen_name.lower() in constants.TWITTER_USER_BLACKLIST and not \ any(phrase in tweet.text.lower() for phrase in constants.TWITTER_PHRASE_BLACKLIST): Tweet.add_from_result(tweet, media) media.last_twitter_fetch = datetime.datetime.now() media.put() logging.info(str(total) + ' TWEETS FETCHED') except tweepy.TweepError, e: logging.error('query: ' + media.host_id + '\nreason: ' + str(e.reason) + '\nresponse: ' + str(e.response.read()))
def on_data(self, data): """ Called by StreamListener whenever a new tweet arrives """ try: tweet_json = json.loads(data) coordinates = tweet_json.get('coordinates', {}).get( 'coordinates', None) if tweet_json.get('coordinates') else None place = tweet_json.get('place').get('full_name') if tweet_json.get( 'place') else '' if coordinates is None: # If we don't have coordinates we dont care about this tweet return True tweet = Tweet(id=tweet_json.get('id'), tweet_text=tweet_json.get('text'), user_id=tweet_json.get('user').get('id'), coordinates=coordinates, created_at=tweet_json.get('created_at'), json=tweet_json, last_update=datetime.now(), place=place) tweet.save() except: print_exc() db_session.rollback() return True
def create(): data = request.form with db: tweet = Tweet(text=data["text"]) try: tweet.save() except Exception, e: print e
def destroy(id): with db: try: Tweet.objects(id=id).first().delete() datas = json.dumps({'id': id}) except: # raise raise return Response(datas, mimetype="application/json", status=200)
def create(): data = request.form tweet = Tweet(text=data["text"]) try: tweet.save() except Exception: print e serialized = modelSerializer(tweet) resData = json.dumps(serialized) return Response(resData, content_type="application/json", status=200)
def refresh_tweet_replies(): """ Go through all Tweets in the database, and if one is a reply to existing tweet in the DB, increment its reply count """ for tweet_id in Tweet.all_with_json(): tweet = Tweet.query.get(tweet_id) reply_to = tweet.json.get('in_reply_to_status_id') if reply_to is not None: Tweet.increment_replies(reply_to)
def show(id, *args, **kwargs): if request.headers['Content-Type'] == 'application/json' or request.filetype == 'json': with db: tweet = Tweet.objects(id=id).first() data = json.dumps(model_serializer(tweet)) return Response(data, mimetype="application/json", status=200) return render_template('index.jade')
def load_tweets(data='troll-tweets/tweets.csv'): """Load tweets from tweets.csv into database.""" df = pd.read_csv(data) df['user_id'].replace(np.nan, 0, regex=True, inplace=True) df['tweet_id'].replace(np.nan, 0, regex=True, inplace=True) df.replace(np.nan, '', regex=True, inplace=True) #getting rid of columns not in Tweet model. df.drop('created_at', axis=1, inplace=True) df.drop('expanded_urls', axis=1, inplace=True) df.drop('posted', axis=1, inplace=True) df.drop('retweeted_status_id', axis=1, inplace=True) df.drop('in_reply_to_status_id', axis=1, inplace=True) for index, row in df.iterrows(): user_id, user_key, created_str, retweet_count,\ retweeted, favorite_count, text, tweet_id, source,\ hashtags, mentions = row.values.tolist() tweet = Tweet(user_id=user_id, user_key=user_key, created_str=created_str, retweet_count=retweet_count, retweeted=retweeted, favorite_count=favorite_count, text=text, tweet_id=tweet_id, source=source, hashtags=hashtags, mentions=mentions) db.session.add(tweet) db.session.commit()
def show(id): if request.headers['Content-Type'] == 'application/json': with db: tweet = Tweet.objects(sid=id).first() data = json.dumps(modelSerializer(tweet)) return Response(data, content_type="application/json", status=200) return render_template('index.slim')
def show(id, *args, **kwargs): if request.headers[ 'Content-Type'] == 'application/json' or request.filetype == 'json': with db: tweet = Tweet.objects(id=id).first() data = json.dumps(model_serializer(tweet)) return Response(data, mimetype="application/json", status=200) return render_template('index.jade')
def on_post(self, req, resp): session = req.env['beaker.session'] tx = Tweet(**{ 'author_id': session['logged_in'], 'text': req.params['text'] }) db.add(tx) db.commit() raise falcon.HTTPFound('/')
def store_tweets(): for tweet in tweets: tweet_id = tweet.id tweet_text = clean_tweet(tweet.text) tweet_text = unicodedata.normalize('NFKD', tweet_text).encode('ascii','ignore') sentiment = tweet_sentiment(tweet_text) if(session.query(Tweet).filter_by(tweet_id = tweet_id).scalar() is None): newTweet = Tweet(tweet_id=tweet_id, tweet_text= tweet_text, sentiment= sentiment) session.add(newTweet) session.commit()
def update(id): data = request.form with db: tweet = Tweet.objects(id=id).first() tweet.text = data['text'] tweet.updated_at = datetime.now() try: tweet.save() except Exception, e: print e
def refresh_tweet_data(): """ Fetch a list of tweet ids that are due to be updated, and request them from the Twitter API. Update the DB with the new results """ tweets_to_update = batch_ids() api = tweepy.API(auth_handler, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) total_updates = 0 total_failed = 0 start_time = time.time() print('Updating tweets...', end="") while tweets_to_update: print('.', end='', flush=True) try: # statuses_lookup will return the current state of the requested tweets response = api.statuses_lookup(tweets_to_update, False, False) updated_tweets = set() for tweet in response: Tweet.update_data(tweet.id, tweet) updated_tweets.add(tweet.id) total_updates += 1 failed_update = set(tweets_to_update) - updated_tweets if failed_update: # Mark tweets that did not come back as updated, so we dont keep quering them [ Tweet.bump_last_update(tweet_id) for tweet_id in failed_update ] total_failed += len(failed_update) # Fetch the next batch tweets_to_update = batch_ids() except tweepy.error.TweepError as e: # API might return errors occasionally print_exc() time.sleep(30) end_time = time.time() print('') print('[{}] updated={} failed_update={} time_elapsed={:.2f}'.format( time.strftime('%c'), total_updates, total_failed, (end_time - start_time)))
def get_tweets(tids, fields=None): """Return tweets with specified tids.""" pipe = conn.pipeline(False) for tid in tids: if fields is None: pipe.hgetall(utils.get_tweet_key(tid)) else: pipe.hmget(utils.get_tweet_key(tid), *fields) tweets = [Tweet.load_dict(d) for d in pipe.execute()] return tweets
def create_new_users(existing_ids): """ Find all unique users that have posted a tweet, and create an entry for them """ print('[{}] Creating new users..'.format(time.strftime('%c'))) user_ids = Tweet.fetch_distinct_user_ids() # Only create entires for users that dont already exist new_users = set(user_ids) - set(existing_ids) User.batch_create(new_users) print('[{}] Created {} users'.format(time.strftime('%c'), len(new_users)))
def on_status(self, status): # aca levantar el status y encolarlo para el worker. print(status.id, ' ', status.text) print("-") print(status.user.id, status.user.description) new_user, _ = User.get_or_create(user_id=status.user.id) new_twit, _ = Tweet.get_or_create(user=new_user, status_id=status.id, message=status.text) return True
def post(self): # Get selected texts tweet_ids = self.request.get_all("tweet") # Delete text of twitter-bot from DataBase delete_tweets = [] for id in tweet_ids: tweet = Tweet.get_by_id(int(id)) delete_tweets.append(tweet) db.delete(delete_tweets) self.redirect("/")
def profile_post(username): """ Update user's profile or password :param username: username of the user :type username: string :rtype: profile page of the user Note: Use 'action' param to judge whether to update profile or password """ user = User.get(username) ownername = request.get_cookie('username', secret=COOKIES_SECRET) owner = User.get(ownername) action = request.GET.get('action', '') if ownername == username: if action == 'profile': avatar = request.files.avatar username_new = request.forms.username or username avatar_new = user.avatar if avatar and avatar.file: avatar_new = 'images/avatar_%s%s' % ( username, os.path.splitext(avatar.filename)[-1], ) avatar_file = file(avatar_new, 'w') print >> avatar_file, avatar.file.read() avatar_file.close() res, msg = user.update(username_new, avatar_new) return template('profile_update', user=user, msg=msg) elif action == 'password': old_pw = request.forms.get('old_pw') new_pw1 = request.forms.get('new_pw1') new_pw2 = request.forms.get('new_pw2') res, msg = user.update_password(old_pw, new_pw1, new_pw2) return template('password_update', user=user, msg=msg) elif action == 'tweet': param = { 'username': username, 'text': request.forms.text, 'created_at': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), } res, msg = Tweet.add(**param) return template('tweet_update', user=user, tweet_msg=msg) elif action == 'follow': owner.follow(username) elif action == 'unfollow': owner.unfollow(username) redirect('/%s/' % username)
def profile_post(username): """ Update user's profile or password :param username: username of the user :type username: string :rtype: profile page of the user Note: Use 'action' param to judge whether to update profile or password """ user = User.get(username) ownername = request.get_cookie('username', secret=COOKIES_SECRET) owner = User.get(ownername) action = request.GET.get('action', '') if ownername == username: if action == 'profile': avatar = request.files.avatar if avatar and avatar.file: avatar_new = 'images/%s' % username avatar_file = file(avatar_new, 'w') print >> avatar_file, avatar.file.read() avatar_file.close() res, msg = user.update( name=request.forms.name, gender=request.forms.gender, hometown=request.forms.hometown, ) elif action == 'password': old_pw = request.forms.get('old_pw') new_pw1 = request.forms.get('new_pw1') new_pw2 = request.forms.get('new_pw2') res, msg = user.update_password(old_pw, new_pw1, new_pw2) elif action == 'tweet': res, msg = Tweet.add( username=username, text=request.forms.text, created_at=datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S'), ) if res: redirect('/%s/' % username) else: return template('%s_update' % action, user=user, msg=msg, ownername=ownername) elif action == 'follow': owner.follow(username) elif action == 'unfollow': owner.unfollow(username) redirect('/%s/' % username)
def on_status(self, status): # aca levantar el status y encolarlo para el worker. print(status.id, ' ', status.text) new_user, _ = User.get_or_create(user_id=status.user.id) try: tweet = Tweet.select().where(Tweet.status_id == status.id).get() except Tweet.DoesNotExist: tweet = None if tweet is not None: print("stream: already saved", status.user.id, status.id_str) else: new_twit = Tweet.create(user=new_user, status_id=status.id, text=status.text, json=jsonpickle.encode(status._json, unpicklable=False)) new_twit.save() print("stream: new ", status.user.id) return True
def post_tweet(): form = TweetForm() if form.validate(): #inseram in baza de date tweet = Tweet(user_id=current_user.id, text=form.text.data, date_created=datetime.now()) db.session.add(tweet) db.session.commit() # ma intorc pe aceasi pagina return redirect(url_for('timeline')) return 'Something went wrong'
def profile_post(username): """ Update user's profile or password :param username: username of the user :type username: string :rtype: profile page of the user Note: Use 'action' param to judge whether to update profile or password """ user = User.get(username) ownername = request.get_cookie('username', secret=COOKIES_SECRET) owner = User.get(ownername) action = request.GET.get('action', '') if ownername == username: if action == 'profile': avatar = request.files.avatar if avatar and avatar.file: avatar_new = 'images/%s' % username avatar_file = file(avatar_new, 'w') print >> avatar_file, avatar.file.read() avatar_file.close() res, msg = user.update( name=request.forms.name, gender=request.forms.gender, hometown=request.forms.hometown, ) elif action == 'password': old_pw = request.forms.get('old_pw') new_pw1 = request.forms.get('new_pw1') new_pw2 = request.forms.get('new_pw2') res, msg = user.update_password(old_pw, new_pw1, new_pw2) elif action == 'tweet': res, msg = Tweet.add( username = username, text = request.forms.text, created_at = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ) if res: redirect('/%s/' % username) else: return template('%s_update' % action, user=user, msg=msg, ownername=ownername) elif action == 'follow': owner.follow(username) elif action == 'unfollow': owner.unfollow(username) redirect('/%s/' % username)
def get(self): auth = BasicAuthHandler("user", "password") api = API(auth, source="3990552168") timeline = api.user_timeline(count=1, page=1)[0] tweet = Tweet.all().get() if not tweet: tweet = Tweet(text=timeline.text, date=timeline.created_at) tweet.text = timeline.text tweet.date = timeline.created_at tweet.put()
def add_tweet(): if request.method == 'GET': return render_template('add_tweet.html') else: new_text = request.form.get('text') new_picture_url = request.form.get('picture_url') new_show_location = request.form.get('show_location') new_location = request.form.get('location') new_tweet = Tweet(text=new_text, picture_url=new_picture_url, show_location=new_show_location, location=new_location) session.add(new_tweet) session.commit() return redirect(url_for('my_feed'))
def tweet_to_db(): """Add tweets into db""" output = format_tweets() text_list = [a.text for a in Tweet.query.all()] for tweet in output: if tweet[2] not in text_list: # need to edit this tweet = Tweet(handle=tweet[0], time_created=tweet[1], text=tweet[2], retweets=tweet[3]) db.session.add(tweet) db.session.commit()
def profile_post(username): """ Update user's profile or password :param username: username of the user :type username: string :rtype: profile page of the user Note: Use 'action' param to judge whether to update profile or password """ user = User.get(username) ownername = request.get_cookie("username", secret=COOKIES_SECRET) owner = User.get(ownername) action = request.GET.get("action", "") if ownername == username: if action == "profile": avatar = request.files.avatar username_new = request.forms.username or username avatar_new = user.avatar if avatar and avatar.file: avatar_new = "images/avatar_%s%s" % (username, os.path.splitext(avatar.filename)[-1]) avatar_file = file(avatar_new, "w") print >> avatar_file, avatar.file.read() avatar_file.close() res, msg = user.update(username_new, avatar_new) return template("profile_update", user=user, msg=msg) elif action == "password": old_pw = request.forms.get("old_pw") new_pw1 = request.forms.get("new_pw1") new_pw2 = request.forms.get("new_pw2") res, msg = user.update_password(old_pw, new_pw1, new_pw2) return template("password_update", user=user, msg=msg) elif action == "tweet": param = { "username": username, "text": request.forms.text, "created_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), } res, msg = Tweet.add(**param) return template("tweet_update", user=user, tweet_msg=msg) elif action == "follow": owner.follow(username) elif action == "unfollow": owner.unfollow(username) redirect("/%s/" % username)
def create_tweet(handle, post_or_reply, url, content, comments, views, hearts, last_updated): tweet = Tweet(handle=handle, post_or_reply=post_or_reply, url=url, content=content, comments=comments, comment_summary=comment_summary, views=views, hearts=hearts, last_updated=last_updated) db.session.add(tweet) db.session.commit() return tweet
def get(self): # Get text chosen randomly from DataBase query = Tweet.all() # If there is no bot text if not query.count(): return tweets = query.fetch(query.count()) tweet = tweets[randint(0, query.count() - 1)] # Tweet the text wkm = Wakametter(ACCESS["key"], ACCESS["secret"]) try: wkm.tweet(tweet.text) except TweepError: pass
def run(self): if not self.options.user: raise Exception("Need a user to load from!") last_tweet = Tweet.recent(show_replies=True)[0] api = twitter.Api() tweets = api.GetUserTimeline(self.options.user, count=self.options.count, since_id=last_tweet.status_id) added = set() for raw_tweet in tweets: tweet = Tweet(raw_tweet.id, raw_tweet.user, raw_tweet.text, raw_tweet.created_at) if last_tweet and (tweet.time_created < last_tweet.time_created or tweet.text == last_tweet.text): continue added.add(tweet) meta.session.add(tweet) meta.session.commit() if added: self.log.info("Imported %d new statuses" % len(added))
def save_results(): """Saving the img and the text from results""" # Get form variables keyword = request.form["keywords"] text_results = request.form["twitter"] giphy_url = request.form["giphy"] block = request.form["b_text"] sentiment = request.form["sentiment"] lat = request.form["lat"] lng = request.form["long"] #Saving current user info into the db user_id = session.get("user_id") #Saving tweet data to db save_twit = Tweet(tweet_text=text_results) db.session.add(save_twit) db.session.commit() #Saving giphy data to db save_gif = Picture(giphy_url=giphy_url) db.session.add(save_gif) db.session.commit() new_keyword = Result(keywords=keyword, tweet_id=save_twit.tweet_id, giphy_id=save_gif.giphy_id, block_text=block, sentiment=sentiment, generated_at=datetime.datetime.now(), user_id=user_id, lat=lat, lng=lng) db.session.add(new_keyword) db.session.commit() return redirect("/users/%s" % user_id)
def post_tweet(uid, pipe=None, **tweet_info): """Create a new tweet with basic infomation. Parameters ---------- uid: str User ID. pipe: Pipeline object A Redis pipeline object, if given, need to execute manually outside this function. body: str The body of the tweet. location: str The location when posting this tweet. original: bool Whether this tweet is an original tweet or just a retweet. """ tweet_info['uid'] = uid tweet_info['tid'] = uuid4().hex tweet_info['datetime'] = datetime.now() tweet_info.setdefault('original', True) tweet = Tweet.load_dict(tweet_info) if pipe is None: pipe = conn.pipeline(False) execute = True else: execute = False pipe.hmset(utils.get_tweet_key(tweet.tid), tweet.dump_dict()) pipe.zadd(utils.get_user_tweets_key(uid), tweet.tid, tweet.ts) pipe.zadd(utils.get_timeline_key(uid), tweet.tid, tweet.ts) if users.get_followers_count(uid) < config.DIRECT_INSERT_THRES: # Perform direct insertion followers = users.get_followers(uid) for follower_uid in followers: pipe.zadd(utils.get_timeline_key(follower_uid), tweet.tid, tweet.ts) if execute: pipe.execute()
def initialize(self, request, response): webapp.RequestHandler.initialize(self, request, response) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from model import g_blog,User self.blog = g_blog self.login_user = users.get_current_user() self.is_login = (self.login_user != None) self.loginurl=users.create_login_url(self.request.uri) self.logouturl=users.create_logout_url(self.request.uri) self.is_admin = users.is_current_user_admin() self.tweet = Tweet.all().get(); if self.is_admin: self.auth = 'admin' self.author=User.all().filter('email =',self.login_user.email()).get() if not self.author: self.author=User(dispname=self.login_user.nickname(),email=self.login_user.email()) self.author.isadmin=True self.author.user=self.login_user self.author.put() elif self.is_login: self.author=User.all().filter('email =',self.login_user.email()).get() if self.author: self.auth='author' else: self.auth = 'login' else: self.auth = 'guest' try: self.referer = self.request.headers['referer'] except: self.referer = None self.template_vals = {'self':self,'blog':self.blog,'current':self.current,'tweet':self.tweet}
def get(self): # Make new session when there is no session. if not self.session.get("user"): new_id = binascii.hexlify(os.urandom(8)) me = Wakametter(ACCESS["key"], ACCESS["secret"]).me self.session["user"] = {"id" : new_id, "name": me.screen_name, "icon": me.profile_image_url } # Get User user = self.session.get("user") # Get texts of twitter-bot query = Tweet.all().order("-date") tweets = query.fetch(query.count()) # Render template_values = { "user_name": user["name"], "icon_src" : user["icon"], "tweets" : tweets } self.render("home.html", template_values)
def load_tweets(): i = 0 while i < 30: new_feeling = random.choice(feels) print new_feeling get_tweets = tApi.get_tweets(query=new_feeling, count=5) print get_tweets try: tweet = random.choice(get_tweets)['text'] except IndexError: continue query = Tweet.query.filter_by(tweet_text=tweet).first() if query == None: tweet_text = Tweet(tweet_text=tweet) db.session.add(tweet_text) i = i + 1 else: continue db.session.commit()
def add_tweet(): if request.method == 'GET': return render_template("add_tweet.html") else: # read form data new_text = request.form.get('text') new_picture_url = request.form.get('picture_url') new_show_location = request.form.get('show_location') new_location = request.form.get('location') # MISSING CODE HERE FOR UPDATING THE TWEET tweet = Tweet() tweet.text = new_text tweet.picture_url = new_picture_url tweet.show_location = new_show_location tweet.location = new_location session.add(tweet) session.commit() # redirect user to the page that views all tweets return redirect(url_for('my_feed'))
def post(self): # Save new text of twitter-bot new_tweet = Tweet(text = self.request.get("text")) new_tweet.put() self.redirect("/")
def batch_ids(): """ Get the tweet ids, for all the tweets that are due to be updated. """ return [tweet.id for tweet in Tweet.batch_to_update()]
def main(): """docstring for main""" data = file('data/petster-hamster/ent.petster-hamster') for item in data.readlines(): item = item.decode('utf8') # print repr(item) # print type(item) # print item if not item.startswith('%'): res = INFO_PATTERN.findall(item) info = dict(zip(KEYS, res)) print info['username'] res, user = User.add( username=info['username'], password='******' % info['username'], password_confirm='pyneo4jet%s' % info['username'], invitation=INVITATION_CODE, ) user.update( name=info['name'], gender=info['gender'], hometown=info['hometown'], ) for key in KEYS: res, msg = Tweet.add( info['username'], 'My %s is %s' % (key, info[key]), (datetime.datetime.now() - datetime.timedelta(days=random.randrange(0, 365)) - datetime.timedelta(seconds=random.randrange(0, 86400)) ).strftime('%Y-%m-%d %H:%M:%S'), ) if not res: print msg print key print info[key] raw_input() data.close() data = file('data/petster-hamster/out.petster-hamster') for item in data.readlines(): # print repr(item) # raw_input() if not item.startswith('%'): username1, username2 = item.strip().split(' ') print username1, username2 # raw_input() user1 = User.get(username1) user2 = User.get(username2) if user1 and user2: if (int(username1) + int(username2)) % 2 == 0: res, msg = user2.follow(username1) else: res, msg = user1.follow(username2) if not res: print msg raw_input() data.close() db.shutdown()
access_token = config['credentials']['access_token'] access_token_secret = config['credentials']['access_token_secret'] auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) # horrible #while True: for n in range(1): #time.sleep(1) #get 1 twtit to process from database try: tweet = Tweet.select().where(Tweet.processed==False).get() except Tweet.DoesNotExist: tweet = None continue #if tweet is None: # continue #check if user had more than 3 beers if (tweet.user.beers < int(config['sysarmy']['max_beers'])): print ("Assign Beer to user", tweet.user.user_id) # Get Free Beer Code try: beer_code = BeerCode.select().where(BeerCode.used == False).get() except BeerCode.DoesNotExist: beer_code = None
def get_tweet(tid): """Return a tweet with specified tid.""" return Tweet.load_dict(conn.hgetall(utils.get_tweet_key(tid)))
def main(): """docstring for main""" data = file('data/petster-hamster/ent.petster-hamster') for item in data.readlines(): item = item.decode('utf8') # print repr(item) # print type(item) # print item if not item.startswith('%'): res = INFO_PATTERN.findall(item) info = dict(zip(KEYS, res)) print info['username'] res, user = User.add( username=info['username'], password='******' % info['username'], password_confirm='pyneo4jet%s' % info['username'], invitation=INVITATION_CODE, ) user.update( name=info['name'], gender=info['gender'], hometown=info['hometown'], ) for key in KEYS: res, msg = Tweet.add(info['username'], 'My %s is %s' % (key, info[key]), ( datetime.datetime.now() - datetime.timedelta(days=random.randrange(0, 365)) - datetime.timedelta(seconds=random.randrange(0, 86400)) ).strftime('%Y-%m-%d %H:%M:%S'), ) if not res: print msg print key print info[key] raw_input() data.close() data = file('data/petster-hamster/out.petster-hamster') for item in data.readlines(): # print repr(item) # raw_input() if not item.startswith('%'): username1, username2 = item.strip().split(' ') print username1, username2 # raw_input() user1 = User.get(username1) user2 = User.get(username2) if user1 and user2: if (int(username1) + int(username2)) % 2 == 0: res, msg = user2.follow(username1) else: res, msg = user1.follow(username2) if not res: print msg raw_input() data.close() db.shutdown()
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) searchQuery = '%23downtime99999' # Maximum number of tweets we want to collect maxTweets = 1000000 tweetCount = 0 # Tell the Cursor method that we want to use the Search API (api.search) # Also tell Cursor our query, and the maximum number of tweets to return for status in Cursor(api.search, q=searchQuery).items(maxTweets): new_user, _ = User.get_or_create(user_id=status.user.id) try: tweet = Tweet.select().where(Tweet.status_id == status.id).get() except Tweet.DoesNotExist: tweet = None if tweet is not None: print("search: already saved", status.user.id, status.id_str) else: new_twit = Tweet.create(user=new_user, status_id=status.id, text=status.text, json=jsonpickle.encode(status._json, unpicklable=False)) new_twit.save() print("search: new ", status.user.id) # Write the JSON format to the text file, and add one to the number of tweets we've collected
def save_tweet(): tweets = get_tweet() for tweet in tweets: tweet_id = tweet["tweet_id"] if session.query(Tweet).filter(Tweet.tweet_id == tweet_id).first(): break tw = Tweet() tw.tweet_id = tweet["tweet_id"] tw.twitter_id = tweet["twitter_id"] tw.twitter_name = tweet["twitter_name"] tw.tweeted_at = tweet["datetime"] tw.rank_tier = tweet["rank_tier"] tw.player_name = tweet["main"]["player_name"] tw.character = tweet["main"]["character"] tw.rank = tweet["main"]["rank"] tw.vsid = tweet["main"]["vsid"] tw.comment = tweet["main"]["comment"] session.add(tw) session.commit()