Skip to content

Commit

Permalink
fixed rendering issue
Browse files Browse the repository at this point in the history
  • Loading branch information
HurinHu committed Aug 12, 2023
1 parent d57194d commit 2d19cb4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
27 changes: 14 additions & 13 deletions GoogleNews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import datetime
from dateutil.relativedelta import relativedelta

import logging
### METHODS

def lexical_date_parser(date_to_check):
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(self,lang="en",period="",start="",end="",encode="utf-8",region=None
self.__end = end
self.__encode = encode
self.__exception = False
self.__version = '1.6.8'
self.__version = '1.6.9'

def getVersion(self):
return self.__version
Expand Down Expand Up @@ -141,7 +141,8 @@ def build_response(self):
self.__totalcount = int(stats.group().replace(',', ''))
else:
#TODO might want to add output for user to know no data was found
return
self.__totalcount = None
logging.debug('Total count is not available when sort by date')
result = self.content.find_all("a",{"jsname" : re.compile(r".*")})[3:-1]
return result

Expand All @@ -165,29 +166,29 @@ def page_at(self, page=1):
result = self.build_response()
for item in result:
try:
tmp_text = item.find("div", {"role" : "heading"}).text.replace("\n","")
tmp_text = item.find("h3").text.replace("\n","")
except Exception:
tmp_text = ''
try:
tmp_link = item.get("href")
except Exception:
tmp_link = ''
try:
tmp_media = item.findAll("g-img")[0].parent.text
tmp_media = item.find('div').find('div').find('div').find_next_sibling('div').text
except Exception:
tmp_media = ''
try:
tmp_date = item.find("div", {"role" : "heading"}).next_sibling.findNext('div').text
tmp_date = item.find('div').find_next_sibling('div').find('span').text
tmp_date,tmp_datetime=lexical_date_parser(tmp_date)
except Exception:
tmp_date = ''
tmp_datetime=None
try:
tmp_desc = item.find("div", {"role" : "heading"}).next_sibling.text
tmp_desc = item.find_next_sibling('div').find('div').find_next_sibling('div').find('div').find('div').find('div').contents[0].replace('\n','')
except Exception:
tmp_desc = ''
try:
tmp_img = item.findAll("g-img")[0].find("img").get("src")
tmp_img = item.find("img").get("src")
except Exception:
tmp_img = ''
self.__texts.append(tmp_text)
Expand Down Expand Up @@ -221,29 +222,29 @@ def get_page(self, page=1):
result = self.build_response()
for item in result:
try:
tmp_text = item.find("div", {"role" : "heading"}).text.replace("\n","")
tmp_text = item.find("h3").text.replace("\n","")
except Exception:
tmp_text = ''
try:
tmp_link = item.get("href")
except Exception:
tmp_link = ''
try:
tmp_media = item.findAll("g-img")[0].parent.text
tmp_media = item.find('div').find('div').find('div').find_next_sibling('div').text
except Exception:
tmp_media = ''
try:
tmp_date = item.find("div", {"role" : "heading"}).next_sibling.findNext('div').text
tmp_date = item.find('div').find_next_sibling('div').find('span').text
tmp_date,tmp_datetime=lexical_date_parser(tmp_date)
except Exception:
tmp_date = ''
tmp_datetime=None
try:
tmp_desc = item.find("div", {"role" : "heading"}).next_sibling.text.replace('\n','')
tmp_desc = item.find_next_sibling('div').find('div').find_next_sibling('div').find('div').find('div').find('div').contents[0].replace('\n','')
except Exception:
tmp_desc = ''
try:
tmp_img = item.findAll("g-img")[0].find("img").get("src")
tmp_img = item.find("img").get("src")
except Exception:
tmp_img = ''
self.__texts.append(tmp_text)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ googlenews.get_page(2)
```
result = googlenews.page_at(2)
```
- If you want to get the total result number of the search(this is approximate number, not exact number, it is the number showing on the google search page)
- If you want to get the total result number of the search(this is approximate number, not exact number, it is the number showing on the google search page) (Note: this function is not available for `googlenews.search()`)
```
googlenews.total_count()
```
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="GoogleNews",
version="1.6.8",
version="1.6.9",
author="Hurin Hu",
author_email="hurin@live.ca",
description="Google News search for Python",
Expand Down
14 changes: 7 additions & 7 deletions test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def testEncode(self):
self.assertNotEqual(length, 0)
print('Encoding result is not empty')

def testTotalCountGreaterThanZero(self):
googlenews = GoogleNews()
googlenews.search(keyword)
count = googlenews.total_count()
self.assertGreater(count, 0)
print('Total count is greater than zero')
# def testTotalCountGreaterThanZero(self):
# googlenews = GoogleNews()
# googlenews.search(keyword)
# count = googlenews.total_count()
# self.assertGreater(count, 0)
# print('Total count is greater than zero')

def testResultNumberAtTwoPages(self):
googlenews = GoogleNews()
Expand All @@ -51,7 +51,7 @@ class TestStringMethods(unittest.TestCase):

def testVersion(self):
googlenews = GoogleNews()
version = '1.6.8'
version = '1.6.9'
self.assertIn(version, googlenews.getVersion())
print('Latest version matched')

Expand Down

0 comments on commit 2d19cb4

Please sign in to comment.