Advertisement
Guest User

Untitled

a guest
Jun 4th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import logging.handlers
  2. import re
  3. import sys
  4. from datetime import datetime
  5.  
  6. import requests
  7. from pgdb import connect
  8.  
  9. try:
  10. from urlparse import urlparse
  11. except ImportError:
  12. from urllib.parse import urlparse, urlencode
  13.  
  14. log = logging.getLogger(__name__)
  15.  
  16. log.setLevel(logging.DEBUG)
  17.  
  18. handler = logging.handlers.SysLogHandler(address='/dev/log')
  19.  
  20. formatter = logging.Formatter('%(module)s.%(funcName)s [%(asctime)s]: %(message)s')
  21. handler.setFormatter(formatter)
  22.  
  23. log.addHandler(handler)
  24.  
  25. log.addHandler(handler)
  26.  
  27. log.info('Processing started')
  28.  
  29.  
  30. def striphtml(text):
  31. p = re.compile(r'<.*?>')
  32. return p.sub('', text)
  33.  
  34.  
  35. try:
  36. con = connect(database='wooservers', host='localhost:5432', user='wooservers_user', password='12345678')
  37. cursor = con.cursor()
  38. log.info('DB connected')
  39.  
  40. url_getCarslist = 'https://car5.ru/car5/rs/car5.get.list.php'
  41.  
  42. r = requests.Session()
  43.  
  44. r.post('http://delimobil.ru/site/auth')
  45.  
  46. data = r.post(url_getCarslist)
  47.  
  48. if data.status_code == 200:
  49. update = 0
  50. new = 0
  51. for items in data.json():
  52. arrCar = items['data']['arrCar']
  53. for car in arrCar:
  54. item_id = int(car['id'])
  55. item_engineOn = int(car['engineOn'])
  56. item_fuel = int(car['fuel'])
  57. item_fuel1 = int(car['fuel1'])
  58. item_fuelmax = int(car['fuelmax'])
  59. item_gnum = striphtml(car['gnum'])
  60. item_lon = float(car['longitude'])
  61. item_lat = float(car['latitude'])
  62. item_mileage = int(car['mileage'])
  63. item_name = striphtml(car['name'])
  64. item_preview = car['preview']
  65. item_sstatus = car['sstatus']
  66. item_statein = int(car['statein'])
  67. item_stateout = int(car['stateout'])
  68. item_status = int(car['status'])
  69. dt = datetime.now()
  70.  
  71. cursor.execute('SELECT id FROM wooservers."public".car5 WHERE id = %i', [item_id])
  72. row = cursor.fetchone()
  73. if row is None:
  74. t = (
  75. item_id,
  76. item_engineOn,
  77. item_fuel,
  78. item_fuel1,
  79. item_fuelmax,
  80. item_gnum,
  81. item_lon,
  82. item_lat,
  83. item_mileage,
  84. item_name,
  85. item_preview,
  86. item_sstatus,
  87. item_statein,
  88. item_stateout,
  89. item_status,
  90. dt
  91. )
  92. cursor.execute(
  93. 'INSERT INTO wooservers."public".car5 VALUES (%i,%i,%i,%i,%i,%s,%f,%f,%i,%s,%s,%s,%i,%i,%i,%s)',
  94. t)
  95. new += 1
  96. elif row is not None:
  97. query = ('UPDATE wooservers."public".car5 SET '
  98. 'engineon=%i, '
  99. 'fuel=%i, '
  100. 'fuel1=%i, '
  101. 'fuelmax=%i, '
  102. 'gnum=\'%s\', '
  103. 'latitude=%f, '
  104. 'longitude=%f, '
  105. 'mileage=%i, '
  106. 'name=\'%s\', '
  107. 'preview=\'%s\', '
  108. 'sstatus=\'%s\', '
  109. 'statein=%i, '
  110. 'stateout=%i, '
  111. 'status=%i, '
  112. 'last_update=\'%s\' '
  113. 'WHERE id = \'%s\'' % (
  114. item_engineOn,
  115. item_fuel,
  116. item_fuel1,
  117. item_fuelmax,
  118. item_gnum,
  119. item_lon,
  120. item_lat,
  121. item_mileage,
  122. item_name,
  123. item_preview,
  124. item_sstatus,
  125. item_statein,
  126. item_stateout,
  127. item_status,
  128. dt,
  129. item_id
  130. ))
  131. cursor.execute(query)
  132. update += 1
  133.  
  134. con.commit()
  135. cursor.close()
  136. con.close()
  137. log.info('Processing done (update: %s, new: %s)', update, new)
  138.  
  139. except Exception as e:
  140. log.critical(str(e))
  141. sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement