Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging.handlers
- import re
- import sys
- from datetime import datetime
- import requests
- from pgdb import connect
- try:
- from urlparse import urlparse
- except ImportError:
- from urllib.parse import urlparse, urlencode
- log = logging.getLogger(__name__)
- log.setLevel(logging.DEBUG)
- handler = logging.handlers.SysLogHandler(address='/dev/log')
- formatter = logging.Formatter('%(module)s.%(funcName)s [%(asctime)s]: %(message)s')
- handler.setFormatter(formatter)
- log.addHandler(handler)
- log.addHandler(handler)
- log.info('Processing started')
- def striphtml(text):
- p = re.compile(r'<.*?>')
- return p.sub('', text)
- try:
- con = connect(database='wooservers', host='localhost:5432', user='wooservers_user', password='12345678')
- cursor = con.cursor()
- log.info('DB connected')
- url_getCarslist = 'https://car5.ru/car5/rs/car5.get.list.php'
- r = requests.Session()
- r.post('http://delimobil.ru/site/auth')
- data = r.post(url_getCarslist)
- if data.status_code == 200:
- update = 0
- new = 0
- for items in data.json():
- arrCar = items['data']['arrCar']
- for car in arrCar:
- item_id = int(car['id'])
- item_engineOn = int(car['engineOn'])
- item_fuel = int(car['fuel'])
- item_fuel1 = int(car['fuel1'])
- item_fuelmax = int(car['fuelmax'])
- item_gnum = striphtml(car['gnum'])
- item_lon = float(car['longitude'])
- item_lat = float(car['latitude'])
- item_mileage = int(car['mileage'])
- item_name = striphtml(car['name'])
- item_preview = car['preview']
- item_sstatus = car['sstatus']
- item_statein = int(car['statein'])
- item_stateout = int(car['stateout'])
- item_status = int(car['status'])
- dt = datetime.now()
- cursor.execute('SELECT id FROM wooservers."public".car5 WHERE id = %i', [item_id])
- row = cursor.fetchone()
- if row is None:
- t = (
- item_id,
- item_engineOn,
- item_fuel,
- item_fuel1,
- item_fuelmax,
- item_gnum,
- item_lon,
- item_lat,
- item_mileage,
- item_name,
- item_preview,
- item_sstatus,
- item_statein,
- item_stateout,
- item_status,
- dt
- )
- cursor.execute(
- 'INSERT INTO wooservers."public".car5 VALUES (%i,%i,%i,%i,%i,%s,%f,%f,%i,%s,%s,%s,%i,%i,%i,%s)',
- t)
- new += 1
- elif row is not None:
- query = ('UPDATE wooservers."public".car5 SET '
- 'engineon=%i, '
- 'fuel=%i, '
- 'fuel1=%i, '
- 'fuelmax=%i, '
- 'gnum=\'%s\', '
- 'latitude=%f, '
- 'longitude=%f, '
- 'mileage=%i, '
- 'name=\'%s\', '
- 'preview=\'%s\', '
- 'sstatus=\'%s\', '
- 'statein=%i, '
- 'stateout=%i, '
- 'status=%i, '
- 'last_update=\'%s\' '
- 'WHERE id = \'%s\'' % (
- item_engineOn,
- item_fuel,
- item_fuel1,
- item_fuelmax,
- item_gnum,
- item_lon,
- item_lat,
- item_mileage,
- item_name,
- item_preview,
- item_sstatus,
- item_statein,
- item_stateout,
- item_status,
- dt,
- item_id
- ))
- cursor.execute(query)
- update += 1
- con.commit()
- cursor.close()
- con.close()
- log.info('Processing done (update: %s, new: %s)', update, new)
- except Exception as e:
- log.critical(str(e))
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement