def get(self): changesets = Changeset.all().order('-created_at').fetch(20) config = get_config() options = {'config': config, 'changesets': changesets} path = os.path.join(os.path.dirname(__file__), 'templates/rss.html') page = template.render(path,options) self.response.out.write(page)
def TweetHandler(self): """tweeting does not tweet from localhost""" self.response.out.write('<br/><br/>Tweeting<br/>') self.response.out.write('this info will be tweeted:<br/>') # oldest non-tweeted and prepared oldest_changeset = Changeset.all().order('created_at').filter('is_tweeted =', False).filter('is_prepared =', True).fetch(1) if not oldest_changeset: self.response.out.write('nothing to tweet') return else: c = oldest_changeset[0] config = get_config() # do not tweet from localhost if not 'localhost' in self.request.url: auth = tweepy.OAuthHandler(config["consumer_key"], config["consumer_secret"]) auth_data = OAuthAccessToken.all().filter('specifier =', config["twitter_username"]).fetch(1)[0] auth.set_access_token(auth_data.oauth_token, auth_data.oauth_token_secret) self.response.out.write('<br/>tweeting with oauth:<br/>') api = tweepy.API(auth) self.response.out.write("id: %d" % c.id) self.response.out.write("user: %s" % c.user) self.response.out.write("comment: %s" % c.comment) self.response.out.write("tweet: %s" % c.tweet) try: api.update_status(c.tweet) except tweepy.error.TweepError, e: self.response.out.write( 'failed: %s' % e.reason ) if "Status is a duplicate" in e.reason: c.is_tweeted = True c.put() return
def get(self): changesets = Changeset.all().order('-created_at').fetch(20) counts = Count.all().order('-date').fetch(30) counts_list = [c.count for c in counts] counts_list.reverse() counts_list_str = ','.join(map(str,counts_list)) config = get_config() description = Description.get_or_insert('description').text options = { 'url': self.request.url, 'config': config, 'changesets': changesets, 'description': description, } if counts_list: options.update({ 'counts_list': counts_list_str, 'max_count': max(counts_list), 'average': sum(counts_list)/len(counts_list), }) path = os.path.join(os.path.dirname(__file__), 'templates/index.html') page = template.render(path,options) self.response.out.write(page)
def PrepareHandler(self): """prepare means: - find the oldest non-prepared - create bit.ly link if does not exist - create trimmed tweet text if the link exists""" config = get_config() self.response.out.write('<br/><br/>Preparing to tweet') # get oldest non-prepared changeset in the datastore oldest_changeset = Changeset.all().order('created_at').filter('is_prepared =', False).fetch(1) if oldest_changeset: c = oldest_changeset[0] self.response.out.write('<br/>There is non-prepared<br/>') self.response.out.write("id: %d<br/>" % c.id) self.response.out.write("created_at: %s<br/>" % c.created_at) self.response.out.write("comment: %s<br/>" % c.comment) self.response.out.write("user: %s<br/>" % c.user) # if no shortened link - create it if not c.link_url: long_url = "http://www.openstreetmap.org/browse/changeset/%d" % c.id bitly = BitLy(config["bitly_username"], config["bitly_key"]) short_url = bitly.short_url(long_url) if not short_url: self.response.out.write('could not shorten') return self.response.out.write(short_url) c.link_url = short_url if not c.tweet: if c.comment: tweet = "%s: %s %s" % (c.user, c.comment, c.link_url) else: tweet = "%s: %s" % (c.user, c.link_url) c.tweet = trim_to_tweet(tweet) if c.link_url and c.tweet: c.is_prepared = True self.response.out.write('tweet: %s' % c.tweet) c.put()
def UpdateCountsHandler(self): """updates day counts count = number of changesets in a day - get MAX_COUNT (200) changesets - get the day of the first changeset - compare dates of first and last changeset if the same write MAX_COUNT, skip next steps - count changeset for first day - if a changeset is from older day and it is the same as for the last stop updating (chansets are probably missing for the same day as the last changeset) - if changeset day is not the same as for the first and count for this date exists in datastore stop updating - if more than 1 day between changesets insert 0 for counts in between """ self.response.out.write('<br/><br/>Updating counts<br/>') MAX_COUNT = 200 changesets = Changeset.all().order('-created_at').fetch(MAX_COUNT) date_of_first_changeset = changesets[0].created_at.date() date_of_last_changeset = changesets[-1].created_at.date() # if the same day for first and last write MAX_COUNT, skip next steps if date_of_last_changeset == date_of_first_changeset: update_count(date_of_first_changeset, MAX_COUNT) self.response.out.write('MAX_COUNT (%d) in this date (%s)<br/>' % (MAX_COUNT, str(date_of_first_changeset)) ) return date_last = changesets[0].created_at.date() count_last = 0 one_day = timedelta(days=1) for c in changesets: date_current = c.created_at.date() if date_current == date_last: count_last += 1 else: if date_last - date_current > one_day: self.response.out.write('need to iterate between dates<br/>') d = date_current + one_day # iterate between dates, set counts to 0 while d < date_last: self.response.out.write(str(d) + '<br/>') update_count(d, 0) d += one_day self.response.out.write(str(date_last)+': '+str(count_last)+'<br/>') is_new_entry = update_count(date_last, count_last) if not is_new_entry: self.response.out.write('not new entry<br/>') if not date_last == date_of_first_changeset: self.response.out.write( 'count for %s is already in datastore' % str(date_last) ) return date_last = c.created_at.date() count_last = 1 if c.created_at.date() == date_of_last_changeset: break self.response.out.write(str(changesets[0].created_at)+'<br/>') self.response.out.write(str(changesets[-1].created_at)+'<br/>')