Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix logging #39

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions check_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import requests
from datetime import date
import logging
import tweepy
from secrets import *
import sqlite3
from sqlite3 import Error
from bs4 import BeautifulSoup

def define_price():
response = requests.get(
url='https://proxy.scrapeops.io/v1/',
params={
'api_key': '(api_key)',
'api_key': 'API_KEY',
'url': 'https://www.k-ruoka.fi/kauppa/tuote/pirkka-iii-olut-033l-45-tlk-si-6410405091260',
'render_js': 'true',
'residential': 'true',
Expand All @@ -35,4 +31,4 @@ def define_price():
finally:
pass

print(price.text)
print(price)
32 changes: 23 additions & 9 deletions tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@
from sqlite3 import Error
from bs4 import BeautifulSoup

logger = logging.getLogger(__name__)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# Add the stream handler to the logger
logger.addHandler(ch)

def define_price():
response = requests.get(
url='https://proxy.scrapeops.io/v1/',
params={
'api_key': '(api_key)',
'api_key': 'API_KEY',
'url': 'https://www.k-ruoka.fi/kauppa/tuote/pirkka-iii-olut-033l-45-tlk-si-6410405091260',
'render_js': 'true',
'residential': 'true',
Expand All @@ -23,20 +30,27 @@ def define_price():
)
soup = BeautifulSoup(response.content, 'html.parser')
price = soup.find('span', class_='price')
return price
if price is None:
raise ValueError('Price is None')
else:
return price

while True:
try:
price = define_price()
break
except NameError:
logging.exception("An error occurred: %s", e)
logger.exception("An error occurred: %s", e)
pass
finally:
pass

# Transform price into float for the database
price_float = float(price.text.replace(',','.'))
try:
price_float = float(price.text.replace(',','.'))
logger.info('Price converted to float')
except ValueError:
logger.error('Could not convert price to a float')

# Get today's date and format it to day month year
date_today = date.today()
Expand All @@ -52,17 +66,17 @@ def define_price():
# Catch error if Tweet is unsuccessful
try:
api.update_status(status=tweet)
logging.info('Tweet "' + tweet + '" sent.')
logger.info('Tweet "' + tweet + '" sent.')
except:
logging.error('Tweet not sent')
logger.error('Tweet not sent')
exit()

## Database section ##
try:
# Connect to DB and create a cursor
sqliteConnection = sqlite3.connect('db/pirkka_price.db')
cursor = sqliteConnection.cursor()
print('DB Init')
logger.info('DB Init')

## Create Pirkka Price table
pirkka_table = """ CREATE TABLE IF NOT EXISTS PIRKKA_PRICE (
Expand All @@ -77,11 +91,11 @@ def define_price():

# Handle errors
except sqlite3.Error as error:
print('Error occured - ', error)
logger.error('Error occured - ', error)

# Close the connection
finally:

if sqliteConnection:
sqliteConnection.close()
print('SQLite Connection closed')
logger.info('All good, SQLite Connection closed')