Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tapaswenipathak committed Jul 6, 2017
1 parent 6ee8a8c commit 1fc3978
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 66 deletions.
89 changes: 89 additions & 0 deletions pyhoroscope.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import urllib.request
from lxml import etree
import re

####################################################################
# API
####################################################################

class Horoscope:

@staticmethod
def get_todays_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/daily-horoscope/" + sunsign
response = urllib.request.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
date = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
date = date.replace("['", "").replace("']", "").replace("['(", "").replace(")']", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("[\"\\r\\n ", "").replace(" \\r\\n \\r\\n \"]", "")
dict = {
'date': date,
'horoscope': horoscope,
'sunsign': sunsign
}

return dict

@staticmethod
def get_weekly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/weekly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
week = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
week = week.replace("['", "").replace("[u'\\n", "").replace("']", "").replace("\\u2013", "-")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("\\r\\n ", "").replace(" \\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("['", "").replace("']", "")
dict = {
'week': week,
'horoscope': horoscope,
'sunsign': sunsign
}

return dict

@staticmethod
def get_monthly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/monthly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
month = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
month = month.replace("['", "").replace("\\r\\n ", "").replace("['\\n", "").replace("']", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()[1]"))
horoscope = horoscope.replace("\\r\\n ", "").replace("['", "").replace("']", "")
dict = {
'month': month,
'horoscope': horoscope,
'sunsign': sunsign
}

return dict

@staticmethod
def get_yearly_horoscope(sunsign):
url = "http://www.ganeshaspeaks.com/horoscopes/yearly-horoscope/" + sunsign
response = urllib.request.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
year = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[1]/div[2]/div/p/text()"))
year = year.replace("['", "").replace("['\\n", "").replace("']", "")
horoscope = str(tree.xpath(
"//*[@id=\"daily\"]/div/div[1]/div[2]/p[1]/text()"))
horoscope = horoscope.replace("['\\r\\n ", "").replace(" \\r\\n \\r\\n ", "").replace("[u'", "").replace("']", "").replace("\\xe2\\x80\\x99s", "")
dict = {
'year': year,
'horoscope': horoscope,
'sunsign': sunsign
}

return dict
16 changes: 7 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
gunicorn==19.3.0
itsdangerous==0.24
newrelic==2.54.0.41
horoscope==1.0.5
wsgiref==0.1.2
Flask
Jinja2
MarkupSafe
Werkzeug
gunicorn
itsdangerous
newrelic
lxml
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.4.3
102 changes: 45 additions & 57 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,64 @@
from flask import Flask, jsonify
from horoscope import Horoscope
from pyhoroscope import Horoscope

app = Flask(__name__)
app = Flask (__name__)

############################################
# Index
# Index
############################################


@app.route('/', methods=['GET'])
def index_route():
@app.route ('/', methods=['GET'])
def index_route () :
return jsonify({
'author': 'Tapasweni Pathak',
'author_url': 'http://tapasweni-pathak.github.io/',
'base_url': 'horoscope-api.herokuapp.com',
'project_name': 'Horoscope API',
'project_url': 'http://tapasweni-pathak.github.io/Horoscope-API',
'project_issues': 'https://github.com/tapasweni-pathak/Horoscope-API/issues',
'endpoints': {
'Today\'s Horosocope': '/horoscope/today/{sunsign}',
'Weekly Horoscope': 'horoscope/week/{sunsign}',
'Monthly Horoscope': 'horoscope/month/{sunsign}',
'Yearly Horosope': 'horoscope/year/{sunsign}',
}
})
'author' : 'Tapasweni Pathak',
'author_url' : 'http://tapasweni-pathak.github.io/',
'base_url' : 'horoscope-api.herokuapp.com',
'project_name' : 'Horoscope API',
'project_url' : 'http://tapasweni-pathak.github.io/Horoscope-API'
})


############################################
# Horoscopes
###########################################

# Todays' Horoscope
@app.route('/horoscope/today/<sunsign>', methods=['GET'])
def today_horoscope_route(sunsign):
result = dict(Horoscope.get_todays_horoscope(sunsign))
return jsonify(date=result['date'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])


# Current Week Horoscope
@app.route('/horoscope/week/<sunsign>', methods=['GET'])
def weekly_horoscope_route(sunsign):
result = dict(Horoscope.get_weekly_horoscope(sunsign))
return jsonify(week=result['week'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])

# Current Month Horoscope


@app.route('/horoscope/month/<sunsign>', methods=['GET'])
def monthly_horoscope_route(sunsign):
result = dict(Horoscope.get_monthly_horoscope(sunsign))
return jsonify(month=result['month'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])

# Current Year Horoscope


@app.route('/horoscope/year/<sunsign>', methods=['GET'])
def yearly_horoscope_route(sunsign):
result = dict(Horoscope.get_yearly_horoscope(sunsign))
return jsonify(year=result['year'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])
#Todays' Horoscope
@app.route ('/horoscope/today/<sunsign>', methods=['GET'])
def today_horoscope_route (sunsign) :
result = dict (Horoscope.get_todays_horoscope (sunsign))
return jsonify (date=result['date'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])


#Current Week Horoscope
@app.route ('/horoscope/week/<sunsign>', methods=['GET'])
def weekly_horoscope_route (sunsign) :
result = dict (Horoscope.get_weekly_horoscope (sunsign))
return jsonify (week=result['week'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])

#Current Month Horoscope
@app.route ('/horoscope/month/<sunsign>', methods=['GET'])
def monthly_horoscope_route (sunsign) :
result = dict (Horoscope.get_monthly_horoscope (sunsign))
return jsonify (month=result['month'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])

#Current Year Horoscope
@app.route ('/horoscope/year/<sunsign>', methods=['GET'])
def yearly_horoscope_route (sunsign) :
result = dict (Horoscope.get_yearly_horoscope (sunsign))
return jsonify (year=result['year'],
sunsign=result['sunsign'],
horoscope=result['horoscope'])


###########################################
# Start Flask
#Start Flask
###########################################

if __name__ == "__main__":
app.run()
app.run()

0 comments on commit 1fc3978

Please sign in to comment.