Created
June 7, 2014 07:46
-
-
Save ehsansh84/40ed795f67a4f31ee1cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'Adel' | |
from shared_connections import SharedConnections | |
from datetime import datetime,timedelta | |
import json | |
es = SharedConnections.ElasticSearchConnection | |
uiFrom = 0 | |
uiSize = 9 | |
uiPubLast = 6 | |
uiSortBy = "RE" | |
uiPublishers = "all" | |
uiCategories = [0,1,2,3] | |
uicountry = [10,2,3] | |
uilanguage = [1,2,3] | |
uiKeywords = "shopping,clothing,clothes,women,toast" | |
def createDelta(delta): | |
deltaTime = timedelta(hours=delta) | |
return datetime.utcnow() - deltaTime | |
def sortBy(x): | |
return { | |
"pubDate" : [ { "pub_date" : {"order" : "desc"}}], | |
"popular" : [ { "visit_count" : {"order" : "desc"}}], | |
"trending" : [ { "re_max" : {"order" : "desc"}}], | |
}.get(x, [ { "re_max" : {"order" : "desc"}}] ) | |
def makeKeyboard(uiKeywords): | |
uiKeywordslist="" | |
if uiKeywords == "": | |
uiKeywordslist="*" | |
else: | |
for item in uiKeywords.split(","): | |
uiKeywordslist = uiKeywordslist + ' \\"' + item + '\\" ' | |
return uiKeywordslist | |
def filterCondition(news_agency_id,category_id,country_id,language_id="2"): | |
result="" | |
if news_agency_id != "all" : | |
result += '{"terms": { "news_agency_id": "%s"} }' %(str(news_agency_id)) | |
if category_id != "all": | |
result += ',{"terms": { "category_id": "%s"} }' %(str(category_id)) | |
if country_id != "all": | |
result += ',{"terms": { "country_id": "%s"} }' %(str(country_id)) | |
if language_id != "all": | |
result += ',{"term": { "language_id": "%s"} }' %(str(language_id)) | |
if result[0]==',': | |
result=result[1:] | |
return result | |
def channelContent(uiFrom,uiSize,uiSortBy,uiKeywords, uiPublishers,uiCategories, uilanguage,uicountry,uiPubLast): | |
SearchDoc = { | |
"from" : uiFrom , "size" : uiSize, | |
"sort" : sortBy(uiSortBy), | |
"query": { | |
"filtered": { | |
"query" : { | |
"query_string" : { | |
"query" : makeKeyboard(uiKeywords), | |
"fields" : ["tag"] | |
} | |
} | |
, | |
"filter": { | |
"and" : [ | |
filterCondition(uiPublishers,uiCategories,uicountry,language_id="2"), | |
{ "range" : { | |
"pub_date" : { | |
"gte": createDelta(uiPubLast), | |
"lte": datetime.utcnow() | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
} | |
res = es.search(index="republishan2",doc_type='entry', body=SearchDoc) | |
output = [] | |
for i in range(uiSize-1): | |
lst = { | |
"title": res['hits']['hits'][i]['_source']['title'], | |
"image": res['hits']['hits'][i]['_source']['img'], | |
"desc": res['hits']['hits'][i]['_source']['desc'], | |
"site": res['hits']['hits'][i]['_source']['news_agency_id'], | |
"re": res['hits']['hits'][i]['_source']['re_max'], | |
"category": res['hits']['hits'][i]['_source']['category_id'], | |
"link": res['hits']['hits'][i]['_source']['link'], | |
"source": res['hits']['hits'][i]['_source']['original_link'], | |
"publishDate": res['hits']['hits'][i]['_source']['pub_date'] | |
} | |
output.append(lst) | |
return {'recordCount': len(output), 'items': output} | |
def mostPopular(qsize): | |
SearchDoc = { | |
"from": 0, | |
"size": qsize, | |
"sort": [ | |
{ | |
"re_max": {"order": "desc"} | |
} | |
], | |
"filter": | |
{ | |
"range": { | |
"pub_date": { | |
"gte": createDelta(168) | |
, | |
"lte": datetime.utcnow() | |
} | |
} | |
} | |
} | |
res = es.search(index="republishan2",doc_type='entry', body=SearchDoc) | |
return res | |
# j =channelContent(uiFrom,uiSize,uiSortBy,uiKeywords, uiPublishers,uiCategories, uilanguage,uicountry,uiPubLast) | |
# print j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment