Example #1
0
 def get(self):
   self.session = Session()
   if CheckAuth(self) is False:
     return DoAuth(self, '/twitter')
   template_values = {}
   api   = twitter.new(Datum)
   account = api.VerifyCredentials()
   limit = api.GetRateLimit()
   template_values['limit'] = limit
   lists = api.GetLists(account.screen_name)
   template_values['lists'] = lists
   tweets = None
   tweets = memcache.get('twitter_home')
   if tweets is None:
     try:
       tweets = api.GetHomeTimeline(count=100)
     except:
       api = None
     if tweets is not None:
       i = 0;
       for tweet in tweets:
         tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
         tweets[i].text = api.ConvertMentions(tweet.text)
         tweets[i].text = api.ExpandBitly(tweet.text)
         i = i + 1
       memcache.set('twitter_home', tweets, 120)
     template_values['tweets'] = tweets
   else:
     template_values['tweets'] = tweets
   template_values['system_version'] = VERSION
   template_values['mode_twitter'] = True
   template_values['page_title'] = 'Twitter'
   path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter.html')
   self.response.out.write(template.render(path, template_values))
Example #2
0
  def get(self, user):
    self.session = Session()
    if CheckAuth(self) is False:
      return DoAuth(self, '/twitter/user/' + user)
    template_values = {}

    api = twitter.new(Datum)
    try:
      limit = api.GetRateLimit()
      template_values['limit'] = limit
      account = api.VerifyCredentials()
      lists = api.GetLists(account.screen_name)
      template_values['lists'] = lists
      if account.screen_name == user:
        template_values['me'] = True
      else:
        template_values['me'] = False
      friendships_ab = False
      friendships_ba = False
      friendships_ab = api.GetFriendshipsExists(account.screen_name, user)
      friendships_ba = api.GetFriendshipsExists(user, account.screen_name)
      tweets = None
      tweets = memcache.get('twitter_user_' + user)
      if tweets is None:
        try:
          tweets = api.GetUserTimeline(user, count=100)
        except:
          api = None
        if tweets is not None:
          i = 0;
          for tweet in tweets:
            tweets[i].datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
            tweets[i].text = api.ConvertMentions(tweet.text)
            tweets[i].text = api.ExpandBitly(tweet.text)
            i = i + 1
          memcache.set('twitter_user_' + user, tweets, 120)
        template_values['tweets'] = tweets
      else:
        template_values['tweets'] = tweets
      template_values['friendships_ab'] = friendships_ab
      template_values['friendships_ba'] = friendships_ba
      template_values['twitter_user'] = tweets[0].user
      template_values['system_version'] = VERSION
      template_values['mode_twitter'] = True
      template_values['page_title'] = 'Twitter User'
      path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_user.html')
      self.response.out.write(template.render(path, template_values))
    except:
      template_values['system_version'] = VERSION
      template_values['mode_twitter'] = True
      template_values['page_title'] = 'Twitter Fail'
      path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'twitter_fail.html')
      self.response.out.write(template.render(path, template_values))
Example #3
0
 def post(self):
   self.session = Session()
   if CheckAuth(self) is False:
     return DoAuth(self, '/twitter')
   tweet = self.request.get('status')
   if tweet != '':
     api = twitter.new(Datum)
     try:
       api.PostUpdate(tweet)
     except:
       api = None
   memcache.delete('twitter_home')
   self.redirect('/twitter')
Example #4
0
  def get(self, method, user):
    self.session = Session()
    if CheckAuth(self) is False:
      return DoAuth(self, '/twitter/user/' + user)

    api = twitter.new(Datum)
    account = api.VerifyCredentials()

    if account.screen_name == user:
      self.redirect('/twitter/user/' + user)
    else:
      if method == 'follow':
        twitter_user = api.CreateFriendship(user)
      if method == 'unfollow':
        twitter_user = api.DestroyFriendship(user)
      self.redirect('/twitter/user/' + user)
Example #5
0
  def get(self):
    site_domain = Datum.get('site_domain')
    site_name = Datum.get('site_name')
    site_author = Datum.get('site_author')
    site_slogan = Datum.get('site_slogan')
    site_analytics = Datum.get('site_analytics')
    site_updated = Datum.get('site_updated')
    if site_updated is None:
      site_updated = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
    feed_url = Datum.get('feed_url')
    if feed_url is None:
      feed_url = '/index.xml'
    else:
      if len(feed_url) == 0:
        feed_url = '/index.xml'

    template_values = {
      'site_domain' : site_domain,
      'site_name' : site_name,
      'site_author' : site_author,
      'site_slogan' : site_slogan,
      'feed_url' : feed_url
    }

    if site_analytics is not None:
      template_values['site_analytics'] = site_analytics

    output = memcache.get('tweets_output')
    api    = twitter.new(Datum)

    if api and output is not None:
      account = memcache.get('twitter_account')

      if account is None:
        account = api.VerifyCredentials()
        memcache.set("twitter_account", account, 3600)

      tweets = memcache.get('tweets')
      if account and tweets is None:
        tweets  = api.GetUserTimeline(account.screen_name, count=20)
        memcache.set("tweets", tweets, 600)
      elif not account:
        tweets = []

      for tweet in tweets:
        tweet.datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(tweet.created_at, '%a %b %d %H:%M:%S +0000 %Y')))
        tweet.text = api.ConvertMentions(tweet.text)
        tweet.text = api.ExpandBitly(tweet.text)

      pages = db.GqlQuery("SELECT * FROM Article WHERE is_page = TRUE AND is_for_sidebar = TRUE ORDER BY title ASC")

      if account is None:
        template_values['page_title'] = site_name + u' › Error'
      else:
        template_values['page_title'] = site_name + u' › Latest 20 Tweets by @' + account.screen_name
        template_values['twitter_account'] = account.name
        template_values['twitter_followers'] = account.followers_count
        template_values['twitter_avatar'] = account.profile_image_url

      template_values['tweets'] = tweets
      template_values['tweets_total'] = len(tweets)

      template_values['pages'] = pages
      template_values['pages_total'] = pages.count()
      template_values['page_top'] = True
      site_theme = Datum.get('site_theme')
      if site_theme is None:
        site_theme = 'default'

      themes = os.listdir(os.path.join(os.path.dirname(__file__), 'tpl', 'themes'))
      if site_theme not in themes:
        site_theme = 'default'

      path = os.path.join(os.path.dirname(__file__), 'tpl', 'themes', site_theme, 'tweets.html')
      output = template.render(path, template_values)
      memcache.set('tweets_output', output, 600)
    elif not api:
        output = 'please config your twitter.'
    self.response.out.write(output)
Example #6
0
 def post(self, key = ''):
   self.session = Session()
   if CheckAuth(self) is False:
     return DoAuth(self, '/writer')
   site_domain = Datum.get('site_domain')
   site_domain_sync = Datum.get('site_domain_sync')
   site_name = Datum.get('site_name')
   site_author = Datum.get('site_author')
   site_slogan = Datum.get('site_slogan')
   site_analytics = Datum.get('site_analytics')
   site_default_format = Datum.get('site_default_format')
   if 'page' in self.session:
     page = self.session['page']
   else:
     page = 0
   site_default_format = Datum.get('site_default_format')
   if (self.request.get('content') != ''):
     if (key):
       article = db.get(db.Key(key))
       article.title = self.request.get('title')
       article.title_link = self.request.get('title_link')
       article.title_url = self.request.get('title_url')
       article.parent_url = self.request.get('parent_url')
       article.content = self.request.get('content')
       article.article_set = self.request.get('article_set')
       article.format = self.request.get('format')
       if article.format not in CONTENT_FORMATS:
         article.format = site_default_format
       if article.format == 'markdown':
         article.content_formatted = markdown.markdown(article.content)
       if (self.request.get('is_page') == 'True'):
         article.is_page = True
       else:
         article.is_page = False
       if (self.request.get('is_for_sidebar') == 'True'):
         article.is_for_sidebar = True
       else:
         article.is_for_sidebar = False
       article.put()
       self.session['message'] = '<div style="float: right;"><a href="http://' + site_domain + '/' + article.title_url + '" target="_blank" class="super normal button">View Now</a></div>Changes has been saved into <a href="/writer/edit/' + key + '">' + article.title + '</a>'
     else:
       article = Article()
       article.title = self.request.get('title')
       article.title_link = self.request.get('title_link')
       article.title_url = self.request.get('title_url')
       article.parent_url = self.request.get('parent_url')
       article.content = self.request.get('content')
       article.article_set = self.request.get('article_set')
       article.format = self.request.get('format')
       if article.format not in CONTENT_FORMATS:
         article.format = site_default_format
       if article.format == 'markdown':
         article.content_formatted = markdown.markdown(article.content)
       if (self.request.get('is_page') == 'True'):
         article.is_page = True
       else:
         article.is_page = False
       if (self.request.get('is_for_sidebar') == 'True'):
         article.is_for_sidebar = True
       else:
         article.is_for_sidebar = False
       article.put()
       self.session['message'] = '<div style="float: right;"><a href="http://' + site_domain + '/' + article.title_url + '" target="_blank" class="super normal button">View Now</a></div>New article <a href="/writer/edit/' + str(article.key()) + '">' + article.title + '</a> has been created'
       # Ping Twitter
       twitter_sync = Datum.get('twitter_sync')
       if twitter_sync == 'True' and article.is_page is False:
         api = twitter.new(Datum)
         if api:
           try:
             status = api.PostUpdate(article.title + ' http://' + site_domain_sync + '/' + article.title_url + ' (Sync via @projectpicky)')
           except:
             api = None
     obsolete = ['archive', 'archive_output', 'feed_output', 'index', 'index_output', 'writer_articles', 'writer_urls']
     memcache.delete_multi(obsolete)
     Datum.set('site_updated', time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
     # Ping Google Blog Search
     if site_domain.find('localhost') == -1:
       try:
         google_ping = 'http://blogsearch.google.com/ping?name=' + urllib.quote(Datum.get('site_name')) + '&url=http://' + urllib.quote(Datum.get('site_domain')) + '/&changesURL=http://' + urllib.quote(Datum.get('site_domain')) + '/sitemap.xml'
         result = urlfetch.fetch(google_ping)
       except:
         taskqueue.add(url='/writer/ping')
     self.redirect('/writer/overview?page=' + str(page))
   else:
     article = Article()
     article.title = self.request.get('title')
     article.title_link = self.request.get('title_link')
     article.title_url = self.request.get('title_url')
     article.content = self.request.get('content')
     article.article_set = self.request.get('article_set')
     article.format = self.request.get('format')
     if article.format not in CONTENT_FORMATS:
       article.format = site_default_format
     if (self.request.get('is_page') == 'True'):
       article.is_page = True
     else:
       article.is_page = False
     if (self.request.get('is_for_sidebar') == 'True'):
       article.is_for_sidebar = True
     else:
       article.is_for_sidebar = False
     template_values = {
       'site_default_format' : site_default_format,
       'article' : article,
       'page_mode' : 'new',
       'page_title' : 'New Article',
       'page_reminder': reminder.writer_write,
       'message' : message.content_empty,
       'user_email' : user.email(),
       'page' : page
     }
     if site_analytics is not None:
       template_values['site_analytics'] = site_analytics
     template_values['system_version'] = VERSION
     path = os.path.join(os.path.dirname(__file__), 'tpl', 'writer', 'write.html')
     self.response.out.write(template.render(path, template_values))