0

I have this code:

import jinja2
import webapp2
import os
from google.appengine.ext import db
import feedparser
from xml.dom import minidom
from google.appengine.api import memcache

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

class News(db.Model):
    title = db.StringProperty(required=True)
    description = db.StringProperty(required=True)
    url = db.StringProperty(required=True)
    imageurl = db.StringProperty(required=True)

feeds = [   'https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=e&ict=ln&output=rss'   # Entertainment
        'https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=snc&ict=ln&output=rss' # Science
        'https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=s&ict=ln&output=rss'   # Sports
        'https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=b&ict=ln&output=rss'   # Business
        'https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=tc&ict=ln&output=rss'  # Technology
]

def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.write(feedparser.__file__)
        self.response.write(render_str('mainpage.html'))

class Entertainment(webapp2.RequestHandler):
    def get(self):
        rssfeed = feedparser.parse(feeds[0])
        self.response.write(rsfeed.entries[0].title)
        self.response.write(rsfeed.entries[0].link)
        self.response.write(rsfeed.entries[0].published)
        self.response.write(rsfeed.entries[0].description)


app = webapp2.WSGIApplication([('/', MainPage),
                           ('/entertainment', Entertainment)
                            ], debug = True)

mainpage.html has nothing except five <p></p> tags with one of them hyperlinked to /entertainment.

When I run it and click on the hyperlinked paragraph, I get this

AttributeError: 'module' object has no attribute 'parse'

I've followed this question, which is why I printed out the filepath in the get of MainPage.

My folder structure is :

  • templates

    • mainpage.html
  • app.yaml

  • feedparser.py

  • newsapp.py

  • newsapp.pyc

When I ran it the first time, feedparser pointed to C:\Users\IBM_ADMIN\Downloads\7c\News Aggregator GAE\feedparser.pyc. That was wrong, so I deleted the .pyc file, and then it pointed to C:\Users\IBM_ADMIN\Downloads\7c\News Aggregator GAE\feedparser.py. But it still gave the same error and the .pyc file got generated again. I know it gets generated because I'm compiling the feedparser file, but why does the feedparser module point to that location? And how can I get around that error?

Community
  • 1
  • 1
Sidharth Samant
  • 714
  • 8
  • 28

1 Answers1

0

The GAE app code is not necessarily able to run as a standalone application.

The proper way to execute it locally is through the SDK's local development server, see Using the Local Development Server.

The local development server will properly set the execution environment to not accidentally reach modules outside your application. It should address your import issue.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • When I do `dev_appserver.py newsapp` in Powershell while located in the folder, it just opens the `dev_appserver.py` file in my editor. Nothing else happens and localhost:8080 shows unable to connect. – Sidharth Samant Aug 09 '16 at 03:05
  • I'm not familiar w/ powershell, seems it may be a tad too smart. Try it in a regular terminal. This *might* also help: http://stackoverflow.com/a/33132299/4495081 – Dan Cornilescu Aug 09 '16 at 03:10
  • Same thing. Never mind. I'll move onto Flask. Hopefully the import problems won't be there. – Sidharth Samant Aug 09 '16 at 03:16
  • Also, the `newsapp` argument is not OK. I'd maybe try `dev_appserver.py app.yaml` or some other invocation as needed - check the docs. – Dan Cornilescu Aug 09 '16 at 03:17
  • Got the problem. My `feedparser.py` file wasn't right. It works now. – Sidharth Samant Aug 09 '16 at 06:05