pipelines.py 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  1. import json
  2. import time
  3. import redis
  4. import logging
  5. import requests
  6. # from twisted.enterprise import adbapi
  7. from scrapy.conf import settings
  8. import psycopg2
  9. import psycopg2.extras
  10. from .ball_func import zqone_intodb, get_pcode, zqtwo_intodb, new_time, out_time, zqone_competition, zqtwo_competition, \
  11. zqthree_competition, two_intodb, one_intodb, three_intodb, wqone_intodb, hash_func, r_func, bqone_intodb, gjz_into, \
  12. gjl_into, gjw_into, gjb_into, new_times, zqone_intodbs, zqtwo_intodbs, zqone_competitions
  13. # 足球 赔率 赛事---------------------------------------------------------------------------------------------------------
  14. class Zuqiupipeline(object):
  15. def open_spider(self, spider):
  16. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  17. password=settings["R_PASSWORD"])
  18. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  19. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  20. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  21. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  22. def process_item(self, item, spider):
  23. # 使用twisted将mysql插入变成异步执行
  24. logger = logging.getLogger(__name__)
  25. # logger.warning(query.addErrback(self.handle_error, item, spider))
  26. # 比赛日期
  27. try:
  28. data_game = item['data_game'].split("/")
  29. print(data_game)
  30. month = str(data_game[1].strip())
  31. day = str(data_game[0])
  32. except Exception as e:
  33. logger.warning(e)
  34. data_game = item['data_game'].split(" ")
  35. months = str(data_game[1].strip())
  36. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  37. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  38. month = month_dict[months]
  39. day = str(data_game[0])
  40. # 比赛时间
  41. time_game = str(item['time_game'])
  42. # 比赛时间,时间戳
  43. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  44. r_ctime = "2019" + "-" + month + "-" + day
  45. # 现在时间,时间戳
  46. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  47. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  48. # 比赛id
  49. competition_id = item['game_id']
  50. # 联赛id
  51. league_id = item['league_id']
  52. # 联赛name
  53. league_name = item['league_name']
  54. # # 主队
  55. team_home = item['team_home']
  56. # 客队
  57. team_guest = item['team_guest']
  58. # number
  59. number = item['number']
  60. pt = item['pt']
  61. corner_ball = item['corner_ball']
  62. p_code, p_id = get_pcode(corner_ball, 'goal_size')
  63. # 构建唯一索引
  64. half_size_guest = item["half_size_guest"]
  65. half_size_guest_rule = item["half_size_guest_rule"]
  66. half_size_home = item["half_size_home"]
  67. half_size_home_rule = item["half_size_home_rule"]
  68. zuqiu = item['zuqiu']
  69. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  70. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  71. # 让球 数据插入数据库
  72. try:
  73. zqone_intodb(data1=half_size_home, data2=size_data, data3="half_goal_size_big", data4=half_size_home_rule,
  74. cursor=self.cursor, redis_db=self.redis_db)
  75. except Exception as e:
  76. logger.warning('足球half_size_home插入错误')
  77. logger.warning(e)
  78. # print(item)
  79. # # 让球 数据插入数据库
  80. try:
  81. zqone_intodb(data1=half_size_guest, data2=size_data, data3="half_goal_size_small", data4=half_size_guest_rule,
  82. cursor=self.cursor, redis_db=self.redis_db)
  83. except Exception as e:
  84. logger.warning('足球half_size_guest插入错误')
  85. logger.warning(e)
  86. # 全场场大小
  87. size_guest = item["size_guest"]
  88. size_guest_rule = item["size_guest_rule"]
  89. size_home = item["size_home"]
  90. size_home_rule = item["size_home_rule"]
  91. try:
  92. zqone_intodb(data1=size_guest, data2=size_data, data3="goal_size_small", data4=size_guest_rule,
  93. cursor=self.cursor, redis_db=self.redis_db)
  94. except Exception as e:
  95. logger.warning('足球size_guest插入错误')
  96. logger.warning(e)
  97. try:
  98. zqone_intodb(data1=size_home, data2=size_data, data3="goal_size_big", data4=size_home_rule,
  99. cursor=self.cursor, redis_db=self.redis_db)
  100. except Exception as e:
  101. logger.warning('足球size_home插入错误')
  102. logger.warning(e)
  103. p_code, p_id = get_pcode(corner_ball, 'concede')
  104. concede_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  105. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  106. # 上半场大小
  107. half_concede_home_rule = item["half_concede_home_rule"]
  108. half_concede_home = item["half_concede_home"]
  109. half_concede_guest_rule = item["half_concede_guest_rule"]
  110. half_concede_guest = item["half_concede_guest"]
  111. # 上半场让球
  112. try:
  113. zqone_intodb(data1=half_concede_home, data2=concede_data, data3="half_concede_home", data4=half_concede_home_rule,
  114. cursor=self.cursor, redis_db=self.redis_db)
  115. except Exception as e:
  116. logger.warning('足球half_concede_home插入错误')
  117. logger.warning(e)
  118. try:
  119. zqone_intodb(data1=half_concede_guest, data2=concede_data, data3="half_concede_guest", data4=half_concede_guest_rule, cursor=self.cursor, redis_db=self.redis_db)
  120. except Exception as e:
  121. logger.warning('足球half_concede_guest插入错误')
  122. logger.warning(e)
  123. concede_guest = item["concede_guest"]
  124. concede_guest_rule = item["concede_guest_rule"]
  125. concede_home = item["concede_home"]
  126. concede_home_rule = item["concede_home_rule"]
  127. # 全场让球
  128. try:
  129. zqone_intodb(data1=concede_guest, data2=concede_data, data3="concede_guest", data4=concede_guest_rule,
  130. cursor=self.cursor, redis_db=self.redis_db)
  131. except Exception as e:
  132. logger.warning('足球concede_guest插入错误')
  133. logger.warning(e)
  134. try:
  135. zqone_intodb(data1=concede_home, data2=concede_data, data3="concede_home", data4=concede_home_rule,
  136. cursor=self.cursor,redis_db=self.redis_db)
  137. except Exception as e:
  138. logger.warning('足球concede_home插入错误')
  139. logger.warning(e)
  140. p_code, p_id = get_pcode(corner_ball, 'capot')
  141. # 独赢----------------------------------------------------------------------------------------------------------
  142. half_capot_home = item["half_capot_home"]
  143. half_capot_guest = item["half_capot_guest"]
  144. half_capot_dogfall = item["half_capot_dogfall"]
  145. capot_home = item["capot_home"]
  146. capot_guest = item["capot_guest"]
  147. capot_dogfall = item["capot_dogfall"]
  148. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  149. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  150. # 上半场独赢 主队
  151. try:
  152. zqtwo_intodb(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  153. except Exception as e:
  154. logger.warning('足球half_capot_home插入错误')
  155. logger.warning(e)
  156. # 上半场独赢 客队
  157. try:
  158. zqtwo_intodb(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  159. except Exception as e:
  160. logger.warning('足球half_capot_guest插入错误')
  161. logger.warning(e)
  162. # 上半场独赢 和
  163. try:
  164. zqtwo_intodb(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  165. except Exception as e:
  166. logger.warning('足球half_capot_dogfall插入错误')
  167. logger.warning(e)
  168. #
  169. # 全场独赢 主队
  170. try:
  171. zqtwo_intodb(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  172. except Exception as e:
  173. logger.warning('足球capot_home插入错误')
  174. logger.warning(e)
  175. # 全场独赢 客队
  176. try:
  177. zqtwo_intodb(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  178. except Exception as e:
  179. logger.warning('足球capot_guest插入错误')
  180. logger.warning(e)
  181. # 全场独赢 和
  182. try:
  183. zqtwo_intodb(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  184. except Exception as e:
  185. logger.warning('足球capot_dogfall插入错误')
  186. logger.warning(e)
  187. #
  188. # 入球数单双-------------------------------------------------------------------------------------------------------------
  189. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  190. odd_even_odd = item["odd_even_odd"]
  191. odd_even_even = item["odd_even_even"]
  192. half_odd_even_odd = item["half_odd_even_odd"]
  193. half_odd_even_even = item["half_odd_even_even"]
  194. # 全场入球数 单双
  195. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  196. 'p_code': p_code,
  197. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  198. # 上半场入球数 单双
  199. try:
  200. zqtwo_intodb(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  201. except Exception as e:
  202. logger.warning('足球odd_even_odd插入错误')
  203. logger.warning(e)
  204. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  205. # data = {
  206. # "game_code": "zq",
  207. # "title": "odds",
  208. # "data": {
  209. # "match_id": competition_id,
  210. # "lg_id": 1123,
  211. # "odds_code": "concede_guest",
  212. # "status": 0,
  213. # "sort": 0,
  214. # "p_code": "concede_size",
  215. # "odds": 1.2333,
  216. # "condition": "+1.5",
  217. # "odds_only": "b15bad3fc8f09924874fef0e19f63d001",
  218. # "sole": "5e2225944d8cdc44cd7943330986978d2",
  219. # "source": "c22123",
  220. # "type": 0,
  221. # "team": ""
  222. # }
  223. # }
  224. # data = json.dumps(data)
  225. # print(data)
  226. # r = requests.post(url, data=data)
  227. # print(r.content)
  228. try:
  229. zqtwo_intodb(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  230. except Exception as e:
  231. logger.warning('足球odd_even_even插入错误')
  232. logger.warning(e)
  233. # 全场入球数 单双
  234. try:
  235. zqtwo_intodb(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  236. cursor=self.cursor, redis_db=self.redis_db)
  237. except Exception as e:
  238. logger.warning('足球half_odd_even_odd插入错误')
  239. logger.warning(e)
  240. try:
  241. zqtwo_intodb(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  242. cursor=self.cursor, redis_db=self.redis_db)
  243. except Exception as e:
  244. logger.warning('足球half_odd_even_even插入错误')
  245. logger.warning(e)
  246. # 总入球数 --------------------------------------------------------------------------------------------------------------
  247. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  248. total_goals = item['total_goal']
  249. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  250. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  251. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  252. # 全场入球数 单双
  253. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  254. 'p_code': p_code,
  255. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  256. # 上半场入球数 单双
  257. for key, value in total_goals.items():
  258. if value:
  259. try:
  260. zqtwo_intodb(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  261. except Exception as e:
  262. logger.warning('足球total_dict插入错误')
  263. logger.warning(e)
  264. # 全场半场 --------------------------------------------------------------------------------------------------------------
  265. half_fulls = item['half_full']
  266. p_code, p_id = get_pcode(corner_ball, 'half_full')
  267. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  268. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  269. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  270. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  271. "half_full_guest_guest": "客客"}
  272. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  273. 'p_code': p_code,
  274. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  275. if half_fulls:
  276. for key, value in half_fulls.items():
  277. if value:
  278. try:
  279. zqtwo_intodb(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  280. except Exception as e:
  281. logger.warning('足球full_dict插入错误')
  282. logger.warning(e)
  283. # 波胆------------------------------------------------------------------------------------------------------------------
  284. bodan_datas = item['bodan_data']
  285. p_code, p_id = get_pcode(corner_ball, 'bodan')
  286. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  287. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  288. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  289. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  290. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  291. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  292. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  293. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  294. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  295. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  296. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  297. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  298. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  299. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  300. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  301. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  302. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  303. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  304. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  305. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  306. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  307. "halfbodanother": "其他"}
  308. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  309. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  310. if bodan_datas:
  311. for key, value in bodan_datas.items():
  312. try:
  313. zqtwo_intodb(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  314. except Exception as e:
  315. logger.warning('足球bodan_datas插入错误')
  316. logger.warning(e)
  317. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  318. first_last_balls = item['first_last_ball']
  319. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  320. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  321. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  322. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  323. 'p_code': p_code,
  324. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  325. if first_last_balls:
  326. for key, value in first_last_balls.items():
  327. # 构建唯一索引
  328. try:
  329. zqtwo_intodb(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  330. except Exception as e:
  331. logger.warning('足球first_last_balls插入错误')
  332. logger.warning(e)
  333. p_code, p_id = get_pcode(corner_ball, 'tema_ball')
  334. full_dicts = item['full_data']
  335. half_dicts = item['half_data']
  336. full_dict_rules = item['full_data_rule']
  337. half_dict_rules = item['half_data_rule']
  338. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  339. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  340. if full_dicts:
  341. for key, value in full_dicts.items():
  342. try:
  343. zqtwo_intodb(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  344. except Exception as e:
  345. logger.warning('足球full_dicts插入错误')
  346. logger.warning(e)
  347. if half_dicts:
  348. for key, value in half_dicts.items():
  349. try:
  350. zqtwo_intodb(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  351. except Exception as e:
  352. logger.warning('足球half_dicts插入错误')
  353. logger.warning(e)
  354. match_date, match_time, time3 = new_time(ctime)
  355. n_time = out_time(time3, 1.5)
  356. # 插入st_zq_competition表
  357. if zuqiu == "足球":
  358. try:
  359. if pt == 1:
  360. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  361. 'game_id': competition_id,
  362. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  363. 'source': "hg3535", "expire_time": n_time, 'is_today': 1, "us_time": ctime}
  364. zqone_competition(data=data_competition, cursor=self.cursor)
  365. if pt == 2:
  366. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  367. 'game_id': competition_id,
  368. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  369. 'source': "hg3535", "expire_time": n_time, 'is_morningplate': 1, "us_time": ctime}
  370. zqtwo_competition(data=data_competition, cursor=self.cursor)
  371. if pt == 3:
  372. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  373. 'game_id': competition_id,
  374. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  375. 'source': "hg3535", "expire_time": n_time, "is_stringscene": 1, "us_time": ctime}
  376. zqthree_competition(data=data_competition, cursor=self.cursor)
  377. except Exception as e:
  378. logger.warning('足球st_zq_competition插入错误')
  379. logger.warning(e)
  380. return item
  381. def close_spider(self, spider):
  382. self.conn.commit()
  383. self.conn.close()
  384. # 篮球 让球大小----------------------------------------------------------------------------------------------------------
  385. class Lanqiupipeline(object):
  386. def open_spider(self, spider):
  387. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  388. password=settings["R_PASSWORD"])
  389. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  390. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  391. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  392. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  393. def process_item(self, item, spider):
  394. # 使用twisted将mysql插入变成异步执行
  395. logger = logging.getLogger(__name__)
  396. # # logger.warning(query.addErrback(self.handle_error, item, spider))
  397. # 联赛id
  398. league_id = item['league_id']
  399. # 联赛名
  400. league_name = item['league_name']
  401. # result = item['result']
  402. # 比赛id
  403. game_id = item['game_id']
  404. # 球队1
  405. team_home = item['team_home']
  406. # 球队2
  407. team_guest = item['team_guest']
  408. # 数量(97>)
  409. number = item['number']
  410. # 比赛状态
  411. zhuangtai = item['zhuangtai']
  412. # 日期
  413. # data_game = item['data_game']
  414. try:
  415. data_game = item['data_game'].split("/")
  416. month = str(data_game[1].strip())
  417. day = str(data_game[0])
  418. except Exception as e:
  419. logger.warning(e)
  420. data_game = item['data_game'].split(" ")
  421. months = str(data_game[1].strip())
  422. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  423. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  424. month = month_dict[months]
  425. day = str(data_game[0])
  426. # 比赛时间
  427. time_game = str(item['time_game'])
  428. # 比赛时间,时间戳
  429. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  430. r_ctime = "2019" + "-" + month + "-" + day
  431. # 现在时间,时间戳
  432. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  433. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  434. # 队1分数
  435. score_home = item['score_home']
  436. # 队2分数
  437. score_guest = item['score_guest']
  438. # 第几节
  439. jijie = item['jijie']
  440. # 球队得分
  441. qiudui = item['qiudui']
  442. pt = item['pt']
  443. concedes_dict = item['concede']
  444. concedes_dict_rule = item['concede_rule']
  445. odd_evens_dict = item['odd_even']
  446. odd_evens_dict_rule = item['odd_even_rule']
  447. total_sizes_dict = item['total_size']
  448. total_sizes_dict_rule = item['total_size_rule']
  449. last_numbers_dict = item['last_number']
  450. capots_dict = item['capot']
  451. team_scores_dict = item['team_score']
  452. team_scores_dict_rule = item['team_score_rule']
  453. # 让球
  454. lanqiu = item['lanqiu']
  455. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  456. 'source': "hg3535", 'expire_time': expire_time}
  457. # 让球 数据插入数据库
  458. try:
  459. one_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  460. except Exception as e:
  461. logger.warning('篮球concedes_dict插入错误')
  462. logger.warning(e)
  463. two_sides = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "two_sides",
  464. 'source': "hg3535", 'expire_time': expire_time}
  465. # 总分单双 数据插入数据库
  466. try:
  467. one_intodb(data1=odd_evens_dict, data2=two_sides, data3=odd_evens_dict_rule, cursor=self.cursor,
  468. redis_db=self.redis_db)
  469. except Exception as e:
  470. logger.warning('篮球odd_evens_dict插入错误')
  471. logger.warning(e)
  472. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "total_size",
  473. 'source': "hg3535", 'expire_time': expire_time}
  474. # 全场总分大小 数据插入数据库
  475. try:
  476. one_intodb(data1=total_sizes_dict, data2=total_size, data3=total_sizes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  477. except Exception as e:
  478. logger.warning('篮球total_sizes_dict插入错误')
  479. logger.warning(e)
  480. data4 = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 6, 'p_code': "team_score",
  481. 'source': "hg3535", 'expire_time': expire_time}
  482. # 全场总分大小 数据插入数据库
  483. try:
  484. one_intodb(data1=team_scores_dict, data2=data4, data3=team_scores_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  485. except Exception as e:
  486. logger.warning('篮球team_scores_dict插入错误')
  487. logger.warning(e)
  488. # 主队进球最后一位数
  489. last_number_home = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  490. 'p_code': "last_number",
  491. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  492. try:
  493. two_intodb(data1=last_numbers_dict['last_home'], data2=last_number_home, data3="last_home", cursor=self.cursor,redis_db=self.redis_db)
  494. except Exception as e:
  495. logger.warning('篮球last_numbers_dict主队插入错误')
  496. logger.warning(e)
  497. # print(last_numbers_dict['last_home'])
  498. # 客队进球最后一位数
  499. last_number_guest = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  500. 'p_code': "last_number",
  501. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  502. try:
  503. two_intodb(data1=last_numbers_dict['last_guest'], data2=last_number_guest, data3="last_guest",
  504. cursor=self.cursor, redis_db=self.redis_db)
  505. except Exception as e:
  506. logger.warning('篮球last_numbers_dict客队插入错误')
  507. logger.warning(e)
  508. # print(last_numbers_dict['last_guest'])
  509. # 独赢
  510. capot_data = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 5, 'p_code': "capot",
  511. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  512. try:
  513. three_intodb(data1=capots_dict, data2=capot_data, cursor=self.cursor, redis_db=self.redis_db)
  514. except Exception as e:
  515. logger.warning('篮球capots_dict插入错误')
  516. logger.warning(e)
  517. match_date, match_time, time3 = new_time(ctime)
  518. n_time = out_time(time3, 1.5)
  519. us_time = ctime
  520. # 插入赛事表
  521. if lanqiu == "篮球":
  522. try:
  523. if pt == 1:
  524. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  525. self.cursor.execute(Competition_sql, (
  526. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  527. n_time,1, us_time, number, n_time, 1))
  528. if pt == 2:
  529. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  530. self.cursor.execute(Competition_sql, (
  531. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  532. n_time,1, us_time, number, n_time, 1))
  533. if pt == 3:
  534. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  535. self.cursor.execute(Competition_sql, (
  536. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  537. n_time,1, us_time, number, n_time, 1))
  538. except Exception as e:
  539. logger.warning('篮球st_lq_competition插入错误')
  540. logger.warning(e)
  541. return item
  542. def close_spider(self, spider):
  543. self.conn.commit()
  544. self.conn.close()
  545. # 网球 让球&大小盘--------------------------------------------------------------------------------------------------------
  546. class Wangqiupipeline(object):
  547. def open_spider(self, spider):
  548. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  549. password=settings["R_PASSWORD"])
  550. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  551. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  552. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  553. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  554. def process_item(self, item, spider):
  555. logger = logging.getLogger(__name__)
  556. # logger.warning(query.addErrback(self.handle_error, item, spider))
  557. # 联赛id
  558. league_id = item['league_id']
  559. # 联赛名
  560. league_name = item['league_name']
  561. # result = item['result']
  562. # 比赛id
  563. game_id = item['game_id']
  564. # 球队1
  565. team_home = item['team_home']
  566. # 球队2
  567. team_guest = item['team_guest']
  568. # 数量(97>)
  569. number = item['number']
  570. # 比赛状态
  571. zhuangtai = item['zhuangtai']
  572. # 日期
  573. # data_game = item['data_game']
  574. try:
  575. data_game = item['data_game'].split("/")
  576. month = str(data_game[1].strip())
  577. day = str(data_game[0])
  578. except Exception as e:
  579. logger.warning(e)
  580. data_game = item['data_game'].split(" ")
  581. months = str(data_game[1].strip())
  582. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  583. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  584. month = month_dict[months]
  585. day = str(data_game[0])
  586. # 比赛时间
  587. time_game = str(item['time_game'])
  588. # 比赛时间,时间戳
  589. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  590. r_ctime = "2019" + "-" + month + "-" + day
  591. # 现在时间,时间戳
  592. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  593. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  594. # 队1分数
  595. score_home = item['score_home']
  596. # 队2分数
  597. score_guest = item['score_guest']
  598. # 第几节
  599. jijie = item['jijie']
  600. # 球队得分
  601. qiudui = item['qiudui']
  602. pt = item['pt']
  603. # 让盘
  604. concedes_dict = item['concedes_dict']
  605. concedes_dict_rule = item['concedes_dict_rule']
  606. # 冠军
  607. kemps_dict = item['kemps_dict']
  608. # 让局
  609. bureaus_dict = item['bureaus_dict']
  610. bureaus_dict_rule = item['bureaus_dict_rule']
  611. # 总局数大小
  612. total_number_dict = item['total_number_dict']
  613. total_number_dict_rule = item['total_number_dict_rule']
  614. # 总局数单双
  615. odd_evens_dict = item['odd_evens_dict']
  616. odd_evens_dict_rule = item['odd_evens_dict_rule']
  617. wangqiu = item['wangqiu']
  618. # 让盘
  619. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "dishes",
  620. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  621. try:
  622. wqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  623. except Exception as e:
  624. logger.warning('网球concedes_dict插入错误')
  625. logger.warning(e)
  626. # 让局
  627. bureaus = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "bureau",
  628. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  629. try:
  630. wqone_intodb(data1=bureaus_dict, data2=bureaus, data3=bureaus_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  631. except Exception as e:
  632. logger.warning('网球bureaus_dict插入错误')
  633. logger.warning(e)
  634. total_number = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  635. 'p_code': "total_number", 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  636. try:
  637. wqone_intodb(data1=total_number_dict, data2=total_number, data3=total_number_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  638. except Exception as e:
  639. logger.warning('网球total_number_dict插入错误')
  640. logger.warning(e)
  641. odd_evens = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 5, 'p_code': "two_game_sides",
  642. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  643. try:
  644. wqone_intodb(data1=odd_evens_dict, data2=odd_evens, data3=odd_evens_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  645. except Exception as e:
  646. logger.warning('网球odd_evens_dict插入错误')
  647. logger.warning(e)
  648. match_date, match_time, time3 = new_time(ctime)
  649. n_time = out_time(time3, 3)
  650. if kemps_dict:
  651. for key, value in kemps_dict.items():
  652. if value:
  653. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  654. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2, odd=value)
  655. odds_only = r_hash
  656. if self.redis_db.hexists("hg3535_wangqiu", r_hash):
  657. pass
  658. else:
  659. self.redis_db.hset("hg3535_wangqiu", r_hash, 0)
  660. try:
  661. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  662. self.cursor.execute(sql1, (
  663. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", new_hash,
  664. odds_only,expire_time, utime, value, odds_only))
  665. # 更新主队st_zq_odds_record表
  666. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s);"
  667. self.cursor.execute(sql2, (
  668. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", odds_only))
  669. except Exception as e:
  670. logger.warning('网球kemps_dict插入错误')
  671. logger.warning(e)
  672. us_time = ctime
  673. # 插入赛事表
  674. if wangqiu == "网球":
  675. try:
  676. if pt is 1:
  677. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  678. self.cursor.execute(Competition_sql, (
  679. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  680. n_time,1, us_time, number, n_time, 1))
  681. if pt is 2:
  682. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  683. self.cursor.execute(Competition_sql, (
  684. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  685. n_time,1, us_time, number, n_time, 1))
  686. if pt is 3:
  687. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  688. self.cursor.execute(Competition_sql, (
  689. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  690. n_time,1, us_time, number, n_time, 1))
  691. except Exception as e:
  692. logger.warning('网球st_wq_competition插入错误')
  693. logger.warning(e)
  694. return item
  695. def close_spider(self, spider):
  696. self.conn.commit()
  697. self.conn.close()
  698. # 网球 波胆--------------------------------------------------------------------------------------------------------------
  699. class Wqbodanpipeline(object):
  700. def open_spider(self, spider):
  701. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  702. password=settings["R_PASSWORD"])
  703. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  704. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  705. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  706. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  707. def process_item(self, item, spider):
  708. # 使用twisted将mysql插入变成异步执行
  709. logger = logging.getLogger(__name__)
  710. # logger.warning(query.addErrback(self.handle_error, item, spider))
  711. # 比赛日期
  712. try:
  713. data_game = item['data_game'].split("/")
  714. month = str(data_game[1].strip())
  715. day = str(data_game[0])
  716. except Exception as e:
  717. logger.warning(e)
  718. data_game = item['data_game'].split(" ")
  719. months = str(data_game[1].strip())
  720. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  721. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  722. month = month_dict[months]
  723. day = str(data_game[0])
  724. # 比赛时间
  725. time_game = str(item['time_game'])
  726. # 比赛时间,时间戳
  727. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  728. r_ctime = "2019" + "-" + month + "-" + day
  729. # 现在时间,时间戳
  730. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  731. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  732. # 比赛id
  733. competition_id = item['game_id']
  734. # 联赛id
  735. league_id = item['league_id']
  736. # 联赛name
  737. league_name = item['league_name']
  738. # 主队
  739. team_home = item['team_home']
  740. # 客队
  741. team_guest = item['team_guest']
  742. # 主队得分
  743. score_home = item['score_home']
  744. # 客队得分
  745. score_guest = item['score_guest']
  746. # number
  747. number = item['number']
  748. corner_ball = item['corner_ball']
  749. half_way = item['half_way']
  750. # 类型早盘,今日,滚球,串场
  751. pt = item['pt']
  752. bodan_datas = item['bodan_data']
  753. p_code = "bodan"
  754. p_id = 7
  755. bodan_dict = {"bodanhome_two_zero": "2-0", "bodanhome_two_one": "2-1",
  756. "bodanhome_three_zero": "3-0", "bodanhome_three_one": "3-1",
  757. "bodanhome_three_two": "3-2",
  758. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  759. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  760. "bodanguest_two_zero": "0-2", "bodanguest_two_one": "1-2",
  761. "bodanguest_three_zero": "0-3",
  762. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  763. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  764. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4"}
  765. if bodan_datas:
  766. for key, value in bodan_datas.items():
  767. if value:
  768. new_hash = hash_func(match_id=competition_id, odds_code=key, sort=0, p_id=7)
  769. r_hash = r_func(match_id=competition_id, odds_code=key, sort=0, p_id=7, odd=value)
  770. odds_only = r_hash
  771. if self.redis_db.hexists("hg3535_wangqiu", r_hash):
  772. pass
  773. else:
  774. self.redis_db.hset("hg3535_wangqiu", r_hash, 0)
  775. try:
  776. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole, condition, expire_time,odds_only) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime=%s,odds=%s,expire_time=%s,odds_only=%s;"
  777. self.cursor.execute(sql1, (
  778. int(league_id), key, int(competition_id), utime, utime, value, p_id, p_code, 0, "hg3535",
  779. new_hash, bodan_dict[key], expire_time, odds_only,utime, value, expire_time, odds_only))
  780. # 更新主队st_zq_odds_record表
  781. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s);"
  782. self.cursor.execute(sql2, (
  783. int(league_id), key, int(competition_id), utime, utime, value, p_id, p_code, 0, "hg3535", key,odds_only))
  784. except Exception as e:
  785. logger.warning('网球波胆插入错误')
  786. logger.warning(e)
  787. return item
  788. def close_spider(self, spider):
  789. self.conn.commit()
  790. self.conn.close()
  791. # 棒球 让球&大小盘--------------------------------------------------------------------------------------------------------
  792. class Bangqiupipeline(object):
  793. def open_spider(self, spider):
  794. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  795. password=settings["R_PASSWORD"])
  796. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  797. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  798. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  799. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  800. def process_item(self, item, spider):
  801. # print(item)
  802. logger = logging.getLogger(__name__)
  803. # logger.warning(query.addErrback(self.handle_error, item, spider))
  804. # 联赛id
  805. league_id = item['league_id']
  806. # 联赛名
  807. league_name = item['league_name']
  808. # result = item['result']
  809. # 比赛id
  810. game_id = item['game_id']
  811. # 球队1
  812. team_home = item['team_home']
  813. # 球队2
  814. team_guest = item['team_guest']
  815. # 数量(97>)
  816. number = item['number']
  817. # 比赛状态
  818. zhuangtai = item['zhuangtai']
  819. # 日期
  820. # data_game = item['data_game']
  821. try:
  822. data_game = item['data_game'].split("/")
  823. month = str(data_game[1].strip())
  824. day = str(data_game[0])
  825. except Exception as e:
  826. logger.warning(e)
  827. data_game = item['data_game'].split(" ")
  828. months = str(data_game[1].strip())
  829. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  830. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  831. month = month_dict[months]
  832. day = str(data_game[0])
  833. # 比赛时间
  834. time_game = str(item['time_game'])
  835. # 比赛时间,时间戳
  836. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  837. r_ctime = "2019" + "-" + month + "-" + day
  838. # 现在时间,时间戳
  839. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  840. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  841. # 队1分数
  842. score_home = item['score_home']
  843. # 队2分数
  844. score_guest = item['score_guest']
  845. # 第几节
  846. jijie = item['jijie']
  847. # 球队得分
  848. qiudui = item['qiudui']
  849. pt = item['pt']
  850. # 让球
  851. concedes_dict = item['concedes_dict']
  852. concedes_dict_rule = item['concedes_dict_rule']
  853. # 独赢
  854. capots_dict = item['capots_dict']
  855. # 总得分大小
  856. total_size_dict = item['total_size_dict']
  857. total_size_dict_rule = item['total_size_dict_rule']
  858. # 总得分单双
  859. odd_evens_dict = item['odd_evens_dict']
  860. odd_evens_dict_rule = item['odd_evens_dict_rule']
  861. bangqiu = item['bangqiu']
  862. # 让球
  863. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  864. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  865. try:
  866. bqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  867. except Exception as e:
  868. logger.warning('棒球concede插入错误')
  869. logger.warning(e)
  870. # 总得分:大/小
  871. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "total_size",
  872. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  873. try:
  874. bqone_intodb(data1=total_size_dict, data2=total_size, data3=total_size_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  875. except Exception as e:
  876. logger.warning('棒球total_size插入错误')
  877. logger.warning(e)
  878. odd_even = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "two_sides",
  879. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  880. try:
  881. bqone_intodb(data1=odd_evens_dict, data2=odd_even, data3=odd_evens_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  882. except Exception as e:
  883. logger.warning('棒球odd_even插入错误')
  884. logger.warning(e)
  885. # 赛事失效时间
  886. match_date, match_time, time3 = new_time(ctime)
  887. n_time = out_time(time3, 3.5)
  888. # 插入独赢
  889. if capots_dict:
  890. for key, value in capots_dict.items():
  891. if value:
  892. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  893. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4, odd=value)
  894. odds_only = r_hash
  895. if self.redis_db.hexists("hg3535_bangqiu", r_hash):
  896. pass
  897. else:
  898. self.redis_db.hset("hg3535_bangqiu", r_hash, 0)
  899. try:
  900. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s,expire_time=%s;"
  901. self.cursor.execute(sql1, (
  902. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", new_hash,
  903. odds_only, expire_time,utime, value, odds_only, expire_time))
  904. # 更新主队st_zq_odds_record表
  905. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s);"
  906. self.cursor.execute(sql2, (
  907. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", odds_only))
  908. except Exception as e:
  909. logger.warning('棒球独赢插入错误')
  910. logger.warning(e)
  911. us_time = ctime
  912. # 插入赛事表
  913. if bangqiu == '棒球':
  914. try:
  915. if pt == 1:
  916. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  917. self.cursor.execute(Competition_sql, (
  918. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  919. n_time, 1, us_time, number, n_time, 1))
  920. if pt == 2:
  921. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  922. self.cursor.execute(Competition_sql, (
  923. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  924. n_time,1, us_time, number, n_time, 1))
  925. if pt == 3:
  926. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  927. self.cursor.execute(Competition_sql, (
  928. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  929. n_time,1, us_time, number, n_time, 1))
  930. except Exception as e:
  931. logger.warning('棒球st_bq_competition插入错误')
  932. logger.warning(e)
  933. return item
  934. def close_spider(self, spider):
  935. self.conn.commit()
  936. self.conn.close()
  937. # 足球 篮球 网球 棒球 冠军------------------------------------------------------------------------------------------------
  938. class Guanjunpipeline(object):
  939. def open_spider(self, spider):
  940. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  941. password=settings["R_PASSWORD"])
  942. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  943. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  944. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  945. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  946. def process_item(self, item, spider):
  947. logger = logging.getLogger(__name__)
  948. data_game = item['data_game'].replace('年', "-").replace('月', "-").replace('日', "")
  949. time_game = item['time_game']
  950. ctime = data_game + " " + time_game + ":00"
  951. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  952. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  953. league_name = item['league_name']
  954. tema_home = item['tema_home']
  955. league_id = item['league_id']
  956. game_id = item['game_id']
  957. new_league_name = item['new_league_name']
  958. # 冠军赔率
  959. champion_team = item['champion_team']
  960. new_champion = item['new_champion']
  961. # 构建唯一哈希索引
  962. time1 = time.mktime(time.strptime(ctime, '%Y-%m-%d %H:%M:%S')) + 43200
  963. time2 = time.localtime(time1)
  964. time3 = time.strftime('%Y-%m-%d %H:%M:%S', time2)
  965. # data_time = str(time3).split(" ")
  966. # match_date,match_time = new_time(ctime)
  967. pt = item['pt']
  968. new_hash = hash_func(match_id=champion_team, odds_code=new_champion, sort=new_league_name, p_id=tema_home)
  969. ball = item['ball']
  970. # 插入st_zq_odds表
  971. r_hash = hash_func(league_id, new_champion, tema_home, champion_team)
  972. odds_only = r_hash
  973. s_hash = hash_func(league_id, league_name, ball, 0)
  974. if ball == "足球":
  975. n_gameid = int('1' + str(league_id))
  976. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  977. 'champion_team': champion_team, "p_id": 8, 'new_league_name': new_league_name,
  978. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  979. "odds_only": odds_only,
  980. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  981. try:
  982. gjz_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  983. Competition_sql = "insert into st_zq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  984. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  985. except Exception as e:
  986. logger.warning('足球冠军插入错误')
  987. logger.warning(e)
  988. elif ball == "篮球":
  989. n_gameid = int('2' + str(league_id))
  990. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  991. 'champion_team': champion_team, "p_id": 7, 'new_league_name': new_league_name,
  992. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  993. "odds_only": odds_only,
  994. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  995. try:
  996. gjl_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  997. Competition_sql = "insert into st_lq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  998. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  999. except Exception as e:
  1000. logger.warning('篮球冠军插入错误')
  1001. logger.warning(e)
  1002. elif ball == "网球":
  1003. n_gameid = int('3' + str(league_id))
  1004. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1005. 'champion_team': champion_team, "p_id": 6, 'new_league_name': new_league_name,
  1006. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1007. "odds_only": odds_only,
  1008. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1009. try:
  1010. gjw_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1011. Competition_sql = "insert into st_wq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  1012. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1013. except Exception as e:
  1014. logger.warning('网球冠军插入错误')
  1015. logger.warning(e)
  1016. elif ball == "棒球":
  1017. n_gameid = int('4' + str(league_id))
  1018. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1019. 'champion_team': champion_team, "p_id": 5, 'new_league_name': new_league_name,
  1020. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1021. "odds_only": odds_only,
  1022. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1023. try:
  1024. gjb_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1025. Competition_sql = "insert into st_bq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  1026. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1027. except Exception as e:
  1028. logger.warning('棒球冠军插入错误')
  1029. logger.warning(e)
  1030. return item
  1031. def close_spider(self, spider):
  1032. self.conn.commit()
  1033. self.conn.close()
  1034. # 足球 篮球 网球 棒球 联赛------------------------------------------------------------------------------------------------
  1035. class Liansaipipeline(object):
  1036. def open_spider(self, spider):
  1037. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1038. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1039. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1040. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1041. def process_item(self, item, spider):
  1042. logger = logging.getLogger(__name__)
  1043. area_id = item["area_id"]
  1044. area_name = item["area_name"]
  1045. st_league = item["st_league"]
  1046. name_chinese = item["name_chinese"]
  1047. print(name_chinese)
  1048. print(area_name)
  1049. ball = item['ball']
  1050. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1051. area_dict = {"南美洲": 3, "北美洲": 4, "欧洲": 5, "大洋洲": 6, "南极洲": 7, "非洲": 1, "世界": 8, "亚洲": 2}
  1052. redis_data_dict = "hg3535_liansai"
  1053. r_hash = hash_func(st_league, name_chinese, ball, 0)
  1054. if self.redis_db.hexists(redis_data_dict, r_hash):
  1055. pass
  1056. else:
  1057. self.redis_db.hset(redis_data_dict, r_hash, 0)
  1058. if ball == "足球":
  1059. try:
  1060. if area_name in area_dict:
  1061. sql1 = "insert into st_zq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1062. self.cursor.execute(sql1, (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name], "hg3535"))
  1063. else:
  1064. if area_name == "足球":
  1065. sql2 = "insert into st_zq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s, %s) on conflict(lg_id) do update set source=%s;"
  1066. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1067. else:
  1068. sql2 = "insert into st_zq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1069. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1070. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1071. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1072. except Exception as e:
  1073. logger.warning("足球联赛插入错误")
  1074. logger.warning(e)
  1075. elif ball == "篮球":
  1076. try:
  1077. if area_name in area_dict:
  1078. sql1 = "insert into st_lq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1079. self.cursor.execute(sql1,
  1080. (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1081. else:
  1082. if area_name == "篮球":
  1083. sql2 = "insert into st_lq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s, %s) on conflict(lg_id) do update set source=%s;"
  1084. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1085. else:
  1086. sql2 = "insert into st_lq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1087. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1088. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1089. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1090. except Exception as e:
  1091. logger.warning("篮球联赛插入错误")
  1092. logger.warning(e)
  1093. elif ball == "网球":
  1094. try:
  1095. if area_name in area_dict:
  1096. sql1 = "insert into st_wq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1097. self.cursor.execute(sql1, (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1098. else:
  1099. if area_name == "网球":
  1100. sql2 = "insert into st_wq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s,%s) on conflict(lg_id) do update set source=%s;"
  1101. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1102. else:
  1103. sql2 = "insert into st_wq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1104. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1105. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1106. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1107. except Exception as e:
  1108. logger.warning("网球联赛插入错误")
  1109. logger.warning(e)
  1110. elif ball == "棒球":
  1111. try:
  1112. if area_name in area_dict:
  1113. sql1 = "insert into st_bq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1114. self.cursor.execute(sql1,(name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1115. else:
  1116. if area_name == "棒球":
  1117. sql2 = "insert into st_bq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s,%s) on conflict(lg_id) do update set source=%s;"
  1118. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1119. else:
  1120. sql2 = "insert into st_bq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1121. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1122. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1123. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1124. except Exception as e:
  1125. logger.warning("棒球联赛插入错误")
  1126. logger.warning(e)
  1127. return item
  1128. def close_spider(self, spider):
  1129. self.conn.commit()
  1130. self.conn.close()
  1131. # ---------------------------------------------------------滚球---------------------------------------------------------
  1132. # 滚球足球 插入
  1133. class Roll_Zuqiupipeline(object):
  1134. def open_spider(self, spider):
  1135. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1136. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1137. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1138. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1139. def process_item(self, item, spider):
  1140. logger = logging.getLogger(__name__)
  1141. # 比赛日期
  1142. try:
  1143. data_game = item['data_game'].split("/")
  1144. month = str(data_game[1].strip())
  1145. day = str(data_game[0])
  1146. except Exception as e:
  1147. logger.warning(e)
  1148. data_game = item['data_game'].split(" ")
  1149. months = str(data_game[1].strip())
  1150. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1151. month = month_dict[months]
  1152. day = str(data_game[0])
  1153. # 比赛时间
  1154. time_game = str(item['time_game'])
  1155. # print(time_game)
  1156. # 比赛时间,时间戳
  1157. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1158. r_ctime = "2019" + "-" + month + "-" + day
  1159. # 现在时间,时间戳
  1160. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1161. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1162. # 比赛id
  1163. competition_id = item['game_id']
  1164. # 联赛id
  1165. league_id = item['league_id']
  1166. # 联赛name
  1167. league_name = item['league_name']
  1168. # 主队
  1169. team_home = item['team_home']
  1170. print(team_home)
  1171. # 客队
  1172. team_guest = item['team_guest']
  1173. # number
  1174. number = item['number']
  1175. score_home = item['score_home']
  1176. score_guest = item['score_guest']
  1177. half_way = item['half_way']
  1178. pt = item['pt']
  1179. match_score = "{}:{}".format(score_home,score_guest)
  1180. corner_ball = item['corner_ball']
  1181. # 构建唯一索引
  1182. half_size_guest = item["half_size_guest"]
  1183. half_size_guest_rule = item["half_size_guest_rule"]
  1184. half_size_home = item["half_size_home"]
  1185. half_size_home_rule = item["half_size_home_rule"]
  1186. try:
  1187. p_code, p_id = get_pcode(corner_ball, 'goal_size')
  1188. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1189. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1190. # 让球 数据插入数据库
  1191. zqone_intodbs(data1=half_size_home, data2=size_data, data3="half_goal_size_big", data4=half_size_home_rule,
  1192. cursor=self.cursor, redis_db=self.redis_db)
  1193. # 让球 数据插入数据库
  1194. zqone_intodbs(data1=half_size_guest, data2=size_data, data3="half_goal_size_small", data4=half_size_guest_rule,
  1195. cursor=self.cursor, redis_db=self.redis_db)
  1196. # 全场场大小
  1197. size_guest = item["size_guest"]
  1198. size_guest_rule = item["size_guest_rule"]
  1199. size_home = item["size_home"]
  1200. size_home_rule = item["size_home_rule"]
  1201. zqone_intodbs(data1=size_guest, data2=size_data, data3="goal_size_small", data4=size_guest_rule,
  1202. cursor=self.cursor, redis_db=self.redis_db)
  1203. zqone_intodbs(data1=size_home, data2=size_data, data3="goal_size_big", data4=size_home_rule,
  1204. cursor=self.cursor, redis_db=self.redis_db)
  1205. # 上半场大小
  1206. p_code, p_id = get_pcode(corner_ball, 'concede')
  1207. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1208. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1209. # 让球 数据插入数据库
  1210. half_concede_home_rule = item["half_concede_home_rule"]
  1211. half_concede_home = item["half_concede_home"]
  1212. half_concede_guest_rule = item["half_concede_guest_rule"]
  1213. half_concede_guest = item["half_concede_guest"]
  1214. # 上半场让球
  1215. zqone_intodbs(data1=half_concede_home, data2=size_data, data3="half_concede_home", data4=half_concede_home_rule,
  1216. cursor=self.cursor, redis_db=self.redis_db)
  1217. zqone_intodbs(data1=half_concede_guest, data2=size_data, data3="half_concede_guest", data4=half_concede_guest_rule, cursor=self.cursor, redis_db=self.redis_db)
  1218. concede_guest = item["concede_guest"]
  1219. concede_guest_rule = item["concede_guest_rule"]
  1220. concede_home = item["concede_home"]
  1221. concede_home_rule = item["concede_home_rule"]
  1222. # 全场让球
  1223. zqone_intodbs(data1=concede_guest, data2=size_data, data3="concede_guest", data4=concede_guest_rule,
  1224. cursor=self.cursor, redis_db=self.redis_db)
  1225. zqone_intodbs(data1=concede_home, data2=size_data, data3="concede_home", data4=concede_home_rule,
  1226. cursor=self.cursor,redis_db=self.redis_db)
  1227. #
  1228. p_code, p_id = get_pcode(corner_ball, 'capot')
  1229. # 独赢-------------------------------------------------------------------------------------------------------------------
  1230. half_capot_home = item["half_capot_home"]
  1231. half_capot_guest = item["half_capot_guest"]
  1232. half_capot_dogfall = item["half_capot_dogfall"]
  1233. capot_home = item["capot_home"]
  1234. capot_guest = item["capot_guest"]
  1235. capot_dogfall = item["capot_dogfall"]
  1236. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1237. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1238. # 上半场独赢 主队
  1239. zqtwo_intodbs(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  1240. # 上半场独赢 客队
  1241. zqtwo_intodbs(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  1242. # 上半场独赢 和
  1243. zqtwo_intodbs(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  1244. #
  1245. # 全场独赢 主队
  1246. zqtwo_intodbs(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  1247. # 全场独赢 客队
  1248. zqtwo_intodbs(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  1249. # 全场独赢 和
  1250. zqtwo_intodbs(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  1251. #
  1252. # 入球数单双-------------------------------------------------------------------------------------------------------------
  1253. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  1254. odd_even_odd = item["odd_even_odd"]
  1255. odd_even_even = item["odd_even_even"]
  1256. half_odd_even_odd = item["half_odd_even_odd"]
  1257. half_odd_even_even = item["half_odd_even_even"]
  1258. # 全场入球数 单双
  1259. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1260. 'p_code': p_code,
  1261. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1262. # 上半场入球数 单双
  1263. zqtwo_intodbs(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  1264. zqtwo_intodbs(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  1265. # 全场入球数 单双
  1266. zqtwo_intodbs(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  1267. cursor=self.cursor, redis_db=self.redis_db)
  1268. zqtwo_intodbs(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  1269. cursor=self.cursor, redis_db=self.redis_db)
  1270. # 总入球数 --------------------------------------------------------------------------------------------------------------
  1271. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  1272. total_goals = item['total_goal']
  1273. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  1274. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  1275. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  1276. # 全场入球数 单双
  1277. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1278. 'p_code': p_code,
  1279. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1280. # 上半场入球数 单双
  1281. for key, value in total_goals.items():
  1282. if value:
  1283. zqtwo_intodbs(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1284. # 全场半场 --------------------------------------------------------------------------------------------------------------
  1285. half_fulls = item['half_full']
  1286. p_code, p_id = get_pcode(corner_ball, 'half_full')
  1287. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  1288. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  1289. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  1290. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  1291. "half_full_guest_guest": "客客"}
  1292. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1293. 'p_code': p_code,
  1294. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1295. if half_fulls:
  1296. for key, value in half_fulls.items():
  1297. if value:
  1298. zqtwo_intodbs(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1299. # 波胆------------------------------------------------------------------------------------------------------------------
  1300. bodan_datas = item['bodan_data']
  1301. p_code, p_id = get_pcode(corner_ball, 'bodan')
  1302. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  1303. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  1304. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  1305. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  1306. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  1307. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  1308. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  1309. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  1310. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  1311. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  1312. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  1313. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  1314. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  1315. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  1316. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  1317. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  1318. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  1319. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  1320. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  1321. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  1322. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  1323. "halfbodanother": "其他"}
  1324. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1325. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1326. if bodan_datas:
  1327. for key, value in bodan_datas.items():
  1328. zqtwo_intodbs(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1329. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  1330. first_last_balls = item['first_last_ball']
  1331. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  1332. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  1333. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  1334. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1335. 'p_code': p_code,
  1336. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1337. if first_last_balls:
  1338. for key, value in first_last_balls.items():
  1339. # 构建唯一索引
  1340. zqtwo_intodbs(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1341. p_code, p_id = get_pcode(corner_ball, 'tema_ball')
  1342. full_dicts = item['full_data']
  1343. half_dicts = item['half_data']
  1344. full_dict_rules = item['full_data_rule']
  1345. half_dict_rules = item['half_data_rule']
  1346. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1347. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1348. if full_dicts:
  1349. for key, value in full_dicts.items():
  1350. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  1351. if half_dicts:
  1352. for key, value in half_dicts.items():
  1353. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  1354. #这日期在这里有问题 先行注释
  1355. # match_date, match_time, time3 = news_time(ctime)
  1356. # n_time = out_time(time3, 1.5)
  1357. # 插入st_zq_competition表
  1358. # if pt == 0:
  1359. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  1360. 'game_id': competition_id,
  1361. 'match_date': r_ctime, 'match_time': '00:00:00', 'utime': utime, 'number': number,
  1362. 'source': "hg3535", "expire_time": expire_time, 'is_rollball': 1, "us_time":'2019-01-01 00:00:00','time_game':time_game}
  1363. zqone_competitions(data=data_competition, cursor=self.cursor)
  1364. except Exception as e:
  1365. logger.warning(e)
  1366. return item
  1367. def close_spider(self, spider):
  1368. # self.conn.close()
  1369. self.conn.commit()
  1370. self.conn.close()
  1371. # 滚球篮球 插入
  1372. class Roll_Lanqiupipeline(object):
  1373. def open_spider(self, spider):
  1374. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1375. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1376. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1377. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1378. def process_item(self, item, spider):
  1379. # 使用twisted将mysql插入变成异步执行
  1380. logger = logging.getLogger(__name__)
  1381. # 联赛id
  1382. league_id = item['league_id']
  1383. # 联赛名
  1384. league_name = item['league_name']
  1385. # result = item['result']
  1386. # 比赛id
  1387. game_id = item['game_id']
  1388. # 球队1 #home_team
  1389. team_home = item['team_home']
  1390. # 球队2 # guest_team
  1391. team_guest = item['team_guest']
  1392. # 数量(97>)
  1393. number = item['number']
  1394. # 比赛状态
  1395. zhuangtai = item['zhuangtai']
  1396. # 日期
  1397. # data_game = item['data_game']
  1398. try:
  1399. data_game = item['data_game'].split("/")
  1400. month = str(data_game[1].strip())
  1401. day = str(data_game[0])
  1402. except Exception as e:
  1403. logger.warning(e)
  1404. data_game = item['data_game'].split(" ")
  1405. months = str(data_game[1].strip())
  1406. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1407. month = month_dict[months]
  1408. day = str(data_game[0])
  1409. # 比赛时间
  1410. time_game = str(item['time_game'])
  1411. # 比赛时间,时间戳
  1412. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1413. r_ctime = "2019" + "-" + month + "-" + day
  1414. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))#过期时间
  1415. # 现在时间,时间戳
  1416. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1417. # 队1分数
  1418. score_home = item['score_home']
  1419. # 队2分数
  1420. score_guest = item['score_guest']
  1421. # 第几节
  1422. jijie = item['jijie']
  1423. # 球队得分
  1424. qiudui = item['qiudui']
  1425. pt = item['pt']
  1426. match_score = "{}:{}".format(score_home,score_guest)
  1427. concedes_dict = item['concede']
  1428. concedes_dict_rule = item['concede_rule']
  1429. odd_evens_dict = item['odd_even']
  1430. odd_evens_dict_rule = item['odd_even_rule']
  1431. total_sizes_dict = item['total_size']
  1432. total_sizes_dict_rule = item['total_size_rule']
  1433. last_numbers_dict = item['last_number']
  1434. capots_dict = item['capot']
  1435. team_scores_dict = item['team_score']
  1436. team_scores_dict_rule = item['team_score_rule']
  1437. try:
  1438. if concedes_dict:
  1439. for key, value in concedes_dict.items():
  1440. if value:
  1441. for x, y in enumerate(value):
  1442. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=1)
  1443. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=1,odd=y)
  1444. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1445. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, y, 1, "concede",x, "hg3535", new_hash, concedes_dict_rule[key][x],odds_only,1,utime, y,odds_only))
  1446. # res = cursor.fetchone()
  1447. # 更新主队st_zq_odds_record表
  1448. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1449. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 1, "concede", x, "hg3535", concedes_dict_rule[key][x],odds_only,1))
  1450. #插入结果表
  1451. sql3 = "insert into st_lq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,status) values (%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1452. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, 0, '', '',match_score,'',utime,time_game,jijie,number,int(game_id),0,0,'hg3535',1,utime,match_score,time_game,jijie,score_home,score_guest))
  1453. #插入记录表
  1454. sql4 = "insert into st_lq_result_record(lg_id, home_team, guest_team, home_score, guest_score, first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1455. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '',match_score,'',utime,time_game,jijie,number,int(game_id),'hg3535',1))
  1456. if odd_evens_dict:
  1457. for key, value in odd_evens_dict.items():
  1458. if value:
  1459. # for x, y in enumerate(value):
  1460. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=2)
  1461. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=2,odd=y)
  1462. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1463. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime,y, 2, "two_sides",x, "hg3535", new_hash, odd_evens_dict_rule[key],odds_only,1,utime, y,odds_only))
  1464. # 更新主队st_zq_odds_record表
  1465. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s);"
  1466. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 2, "two_sides", x, "hg3535", odd_evens_dict_rule[key],odds_only,1))
  1467. if total_sizes_dict:
  1468. for key, value in total_sizes_dict.items():
  1469. if value:
  1470. for x, y in enumerate(value):
  1471. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=3)
  1472. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=3,odd=y)
  1473. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1474. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime,y, 3, "total_size",x, "hg3535", new_hash, total_sizes_dict_rule[key][x],odds_only,1,utime, y,odds_only))
  1475. # 更新主队st_zq_odds_record表
  1476. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1477. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime, y, 3, "total_size", x, "hg3535", total_sizes_dict_rule[key][x],odds_only,1))
  1478. last_home = last_numbers_dict['last_home']
  1479. if last_home:
  1480. for key, value in last_home.items():
  1481. if value:
  1482. new_hash = hash_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4)
  1483. odds_only = r_func(match_id=game_id, odds_code="last_home", sort=0, p_id=4,odd=value)
  1484. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1485. self.cursor.execute(sql1, (int(league_id), "last_home", int(game_id), utime, utime, value, 4, "last_number",0, "hg3535", new_hash, key,odds_only,1,utime, value,odds_only))
  1486. # 更新主队st_zq_odds_record表
  1487. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1488. self.cursor.execute(sql2, (int(league_id), "last_home", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  1489. last_guest = last_numbers_dict['last_guest']
  1490. if last_guest:
  1491. for key, value in last_guest.items():
  1492. if value:
  1493. new_hash = hash_func(match_id=game_id, odds_code="last_guest" + key, sort=0, p_id=4)
  1494. odds_only = r_func(match_id=game_id, odds_code="last_guest", sort=0, p_id=4,odd=value)
  1495. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1496. self.cursor.execute(sql1, (int(league_id), "last_guest", int(game_id), utime, utime, value, 4, "last_number",0, "hg3535", new_hash, key,odds_only,1,utime, value,odds_only))
  1497. # 更新主队st_zq_odds_record表
  1498. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1499. self.cursor.execute(sql2, (int(league_id), "last_guest", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  1500. if capots_dict:
  1501. for key, value in capots_dict.items():
  1502. if value:
  1503. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  1504. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  1505. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,is_rollball) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1506. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, value, 5, "capot",0, "hg3535", new_hash, odds_only,1,utime, value,odds_only))
  1507. # 更新主队st_zq_odds_record表
  1508. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1509. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime, value, 5, "capot", 0, "hg3535",odds_only,1))
  1510. if team_scores_dict:
  1511. for key, value in team_scores_dict.items():
  1512. if value:
  1513. for x, y in enumerate(value):
  1514. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=6)
  1515. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=6,odd=y)
  1516. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1517. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, y, 6, "team_score", x, "hg3535", new_hash,team_scores_dict_rule[key][x], odds_only,1,utime, y,odds_only))
  1518. # 更新主队st_zq_odds_record表
  1519. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1520. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 6, "team_score", x, "hg3535",team_scores_dict_rule[key][x],odds_only,1))
  1521. # 插入赛事表
  1522. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,type,source,expire_time,is_rollball,status) values (%s,%s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s,%s,%s) on conflict(match_id) do update set tag = %s,expire_time = %s;"
  1523. print(Competition_sql)
  1524. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, game_id, r_ctime, time_game, utime, utime, number, pt, "hg3535",expire_time,1,1,number,expire_time))
  1525. except Exception as e:
  1526. logger.warning(e)
  1527. return item
  1528. def close_spider(self, spider):
  1529. self.conn.commit()
  1530. self.conn.close()
  1531. # 滚球网球 插入
  1532. class Roll_Wangqiupipeline(object):
  1533. def open_spider(self, spider):
  1534. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1535. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1536. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1537. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1538. def process_item(self, item, spider):
  1539. logger = logging.getLogger(__name__)
  1540. # 联赛id
  1541. league_id = item['league_id']
  1542. # 联赛名
  1543. league_name = item['league_name']
  1544. # result = item['result']
  1545. # 比赛id
  1546. game_id = item['game_id']
  1547. # 球队1
  1548. team_home = item['team_home']
  1549. # 球队2
  1550. team_guest = item['team_guest']
  1551. # 数量(97>)
  1552. number = item['number']
  1553. # 比赛状态
  1554. zhuangtai = item['zhuangtai']
  1555. # 日期
  1556. # data_game = item['data_game']
  1557. try:
  1558. data_game = item['data_game'].split("/")
  1559. month = str(data_game[1].strip())
  1560. day = str(data_game[0])
  1561. except Exception as e:
  1562. logger.warning(e)
  1563. data_game = item['data_game'].split(" ")
  1564. months = str(data_game[1].strip())
  1565. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1566. month = month_dict[months]
  1567. day = str(data_game[0])
  1568. # 比赛时间
  1569. time_game = str(item['time_game'])
  1570. # 比赛时间,时间戳
  1571. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1572. r_ctime = "2019" + "-" + month + "-" + day
  1573. # 现在时间,时间戳
  1574. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1575. # 队1分数
  1576. score_home = item['score_home']
  1577. # 队2分数
  1578. score_guest = item['score_guest']
  1579. # 第几节
  1580. jijie = item['jijie']
  1581. # 球队得分
  1582. qiudui = item['qiudui']
  1583. pt = item['pt']
  1584. #取不到 暂时注掉
  1585. match_date, match_time = new_times(ctime)
  1586. # 让盘
  1587. concedes_dict = item['concedes_dict']
  1588. concedes_dict_rule = item['concedes_dict_rule']
  1589. # 冠军 独赢
  1590. kemps_dict = item['kemps_dict']
  1591. # 让局
  1592. bureaus_dict = item['bureaus_dict']
  1593. bureaus_dict_rule = item['bureaus_dict_rule']
  1594. # 总局数大小
  1595. total_number_dict = item['total_number_dict']
  1596. total_number_dict_rule = item['total_number_dict_rule']
  1597. # 总局数单双
  1598. odd_evens_dict = item['odd_evens_dict']
  1599. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1600. try:
  1601. if concedes_dict:
  1602. for key, value in concedes_dict.items():
  1603. if value:
  1604. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1605. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  1606. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1607. self.cursor.execute(sql1, (
  1608. int(league_id), key, int(game_id), utime, utime,value, 1, "dishes_home", 0, "hg3535", new_hash,
  1609. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  1610. # 更新主队st_zq_odds_record表
  1611. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1612. self.cursor.execute(sql2, (
  1613. int(league_id), key, int(game_id), utime, utime, value, 1, "dishes_home", 0, "hg3535",
  1614. concedes_dict_rule[key],odds_only,1))
  1615. #插入结果表
  1616. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1617. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1618. # 插入记录表
  1619. sql4 = "insert into st_wq_result_record(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
  1620. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00'))
  1621. if kemps_dict:
  1622. for key, value in kemps_dict.items():
  1623. if value:
  1624. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1625. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1626. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1627. self.cursor.execute(sql1, (
  1628. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", new_hash,odds_only,1,utime, value,odds_only))
  1629. # 更新主队st_zq_odds_record表
  1630. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s,%s,%s);"
  1631. self.cursor.execute(sql2, (
  1632. int(league_id), key, int(game_id), utime, utime,value, 2, "kemp", 0, "hg3535",odds_only,1))
  1633. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1634. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1635. if bureaus_dict:
  1636. for key, value in bureaus_dict.items():
  1637. if value:
  1638. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1639. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  1640. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1641. self.cursor.execute(sql1, (
  1642. int(league_id), key, int(game_id), utime, utime, value, 3, "bureau", 0, "hg3535", new_hash,
  1643. bureaus_dict_rule[key],odds_only,1,utime, value,odds_only))
  1644. # 更新主队st_zq_odds_record表
  1645. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1646. self.cursor.execute(sql2, (
  1647. int(league_id), key, int(game_id), utime, utime,value, 3, "bureau", 0, "hg3535",
  1648. bureaus_dict_rule[key],odds_only,1))
  1649. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1650. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1651. if total_number_dict:
  1652. for key, value in total_number_dict.items():
  1653. if value:
  1654. # for x, y in enumerate(value):
  1655. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  1656. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4,odd=value)
  1657. # print(value)
  1658. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1659. self.cursor.execute(sql1, (
  1660. int(league_id), key, int(game_id), utime, utime,value, 4, "total_number", 0, "hg3535", new_hash,
  1661. total_number_dict_rule[key],odds_only,1,utime, value,odds_only))
  1662. # 更新主队st_zq_odds_record表
  1663. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1664. self.cursor.execute(sql2, (
  1665. int(league_id), key, int(game_id), utime, utime, value, 4, "total_number", 0, "hg3535",
  1666. total_number_dict_rule[key],odds_only,1))
  1667. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1668. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1669. if odd_evens_dict:
  1670. for key, value in odd_evens_dict.items():
  1671. if value:
  1672. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  1673. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  1674. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1675. self.cursor.execute(sql1, (
  1676. int(league_id), key, int(game_id), utime, utime,value, 5, "two_game_sides", 0, "hg3535", new_hash,
  1677. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  1678. # 更新主队st_zq_odds_record表
  1679. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1680. self.cursor.execute(sql2, (
  1681. int(league_id), key, int(game_id), utime, utime, value, 5, "two_game_sides", 0, "hg3535",odd_evens_dict_rule[key],odds_only,1))
  1682. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1683. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1684. except Exception as e:
  1685. print(e)
  1686. # 插入赛事表
  1687. try:
  1688. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,type,source,is_rollball,status) values (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s,%s) on conflict(match_id) do update set tag = %s, status = %s;"
  1689. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, pt, "hg3535", '1', '1', number, '1'))
  1690. except Exception as e:
  1691. print(e)
  1692. return item
  1693. def close_spider(self, spider):
  1694. self.conn.commit()
  1695. self.conn.close()
  1696. # 滚球棒球 插入
  1697. class Roll_Banqiupipeline(object):
  1698. def open_spider(self, spider):
  1699. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1700. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1701. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1702. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1703. def process_item(self, item, spider):
  1704. # 使用twisted将mysql插入变成异步执行
  1705. # 联赛id
  1706. league_id = item['league_id']
  1707. # 联赛名
  1708. league_name = item['league_name']
  1709. # result = item['result']
  1710. # 比赛id
  1711. game_id = item['game_id']
  1712. # 球队1
  1713. team_home = item['team_home']
  1714. # 球队2
  1715. team_guest = item['team_guest']
  1716. # 数量(97>)
  1717. number = item['number']
  1718. #新增 match_score_dict
  1719. match_score_dict = item['match_score_dict']
  1720. # 比赛状态
  1721. zhuangtai = item['zhuangtai']
  1722. # 日期
  1723. # data_game = item['data_game']
  1724. data_game = item['data_game'].split("/")
  1725. month = str(data_game[1].strip())
  1726. day = str(data_game[0])
  1727. # 比赛时间
  1728. # time_game = str(item['time_game'])
  1729. # 比赛时间,时间戳
  1730. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00".strip()
  1731. r_ctime = "2019" + "-" + month + "-" + day
  1732. # 现在时间,时间戳
  1733. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1734. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1735. # 队1分数
  1736. score_home = item['score_home']
  1737. # 队2分数
  1738. score_guest = item['score_guest']
  1739. # 第几节
  1740. jijie = item['jijie']
  1741. # 球队得分
  1742. qiudui = item['qiudui']
  1743. pt = item['pt']
  1744. #滚球这个位置获取不到这些字段
  1745. # match_date, match_time = new_time(ctime)
  1746. # 让球
  1747. concedes_dict = item['concedes_dict']
  1748. concedes_dict_rule = item['concedes_dict_rule']
  1749. # 独赢
  1750. capots_dict = item['capots_dict']
  1751. # 总得分大小
  1752. total_size_dict = item['total_size_dict']
  1753. total_size_dict_rule = item['total_size_dict_rule']
  1754. # 总得分单双
  1755. odd_evens_dict = item['odd_evens_dict']
  1756. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1757. # 让球
  1758. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  1759. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1760. # bqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor)
  1761. # 总得分:大/小
  1762. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "total_size",
  1763. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1764. # bqone_intodb(data1=total_size_dict, data2=total_size, data3=total_size_dict_rule, cursor=self.cursor)
  1765. odd_even = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "two_sides",
  1766. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1767. # bqone_intodb(data1=odd_evens_dict, data2=odd_even, data3=odd_evens_dict_rule, cursor=self.cursor)
  1768. # 赛事失效时间
  1769. # n_time = out_time(ctime, 3.5)
  1770. # 插入独赢
  1771. try:
  1772. if capots_dict:
  1773. for key, value in capots_dict.items():
  1774. if value:
  1775. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1776. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1777. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s,expire_time=%s;"
  1778. self.cursor.execute(sql1, (
  1779. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", new_hash,
  1780. odds_only, expire_time,1,utime, value, odds_only,expire_time))
  1781. # 更新主队st_zq_odds_record表
  1782. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s,%s);"
  1783. self.cursor.execute(sql2, (
  1784. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", odds_only,1))
  1785. res = json.dumps(match_score_dict)
  1786. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1787. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, 0, 1,'','',qiudui,'',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,qiudui,r_ctime,jijie,score_home,score_guest))
  1788. # 插入赛事表
  1789. Competition_sql = "insert into st_bq_competition(home_team,guest_team,lg_id,status,match_id,ctime,utime,tag,source,type,is_rollball) values(%s, %s, %s, %s, %s, %s, %s,%s, %s,%s,%s) on conflict(match_id) do update set tag=%s,is_rollball=%s,utime=%s;"
  1790. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, 1,game_id,utime, utime, number,"hg3535",1,1,number,1,utime))
  1791. #让球
  1792. if concedes_dict:
  1793. for key, value in concedes_dict.items():
  1794. if value:
  1795. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1796. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  1797. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1798. self.cursor.execute(sql1, (
  1799. int(league_id), key, int(game_id), utime, utime,value, 1, "concede", 0, "hg3535", new_hash,
  1800. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  1801. # 更新主队st_zq_odds_record表
  1802. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1803. self.cursor.execute(sql2, (
  1804. int(league_id), key, int(game_id), utime, utime, value, 1, "concede", 0, "hg3535",
  1805. concedes_dict_rule[key],odds_only,1))
  1806. #插入结果表
  1807. res = json.dumps(match_score_dict)
  1808. # r = pymysql.escape_string(res)
  1809. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1810. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, 0, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1811. # 插入记录表
  1812. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1813. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',0))
  1814. if total_size_dict:
  1815. for key, value in total_size_dict.items():
  1816. if value:
  1817. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1818. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1819. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1820. self.cursor.execute(sql1, (
  1821. int(league_id), key, int(game_id), utime, utime,value, 2, "total_size", 0, "hg3535", new_hash,
  1822. total_size_dict_rule[key],odds_only,1,utime, value,odds_only))
  1823. # 更新主队st_zq_odds_record表
  1824. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1825. self.cursor.execute(sql2, (
  1826. int(league_id), key, int(game_id), utime, utime, value, 2, "total_size", 0, "hg3535",
  1827. total_size_dict_rule[key],odds_only,1))
  1828. #插入结果表
  1829. res = json.dumps(match_score_dict)
  1830. # r = pymysql.escape_string(res)
  1831. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1832. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, 0, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1833. # 插入记录表
  1834. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1835. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',1))
  1836. if odd_evens_dict:
  1837. for key, value in odd_evens_dict.items():
  1838. if value:
  1839. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1840. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  1841. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1842. self.cursor.execute(sql1, (
  1843. int(league_id), key, int(game_id), utime, utime,value, 3, "two_sides", 0, "hg3535", new_hash,
  1844. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  1845. # 更新主队st_zq_odds_record表
  1846. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1847. self.cursor.execute(sql2, (
  1848. int(league_id), key, int(game_id), utime, utime, value, 3, "two_sides", 0, "hg3535",
  1849. odd_evens_dict_rule[key],odds_only,1))
  1850. #插入结果表
  1851. res = json.dumps(match_score_dict)
  1852. # r = pymysql.escape_string(res)
  1853. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1854. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, 0, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1855. # 插入记录表
  1856. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1857. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',1))
  1858. except Exception as e:
  1859. print(e)
  1860. return item
  1861. def close_spider(self, spider):
  1862. self.conn.commit()
  1863. self.conn.close()
  1864. # 足球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1865. class Zujieshuqiupipeline(object):
  1866. def open_spider(self, spider):
  1867. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1868. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1869. def process_item(self, item, spider):
  1870. logger = logging.getLogger(__name__)
  1871. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1872. # 赛事id
  1873. match_id = item['id_score'][0]
  1874. # 全场比分
  1875. score = item['id_score'][1]
  1876. status = 0
  1877. game_code = 'zq'
  1878. try:
  1879. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1880. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1881. self.cursor.execute("update st_zq_result set status=2 where match_id={}".format(match_id))
  1882. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1883. self.cursor.execute("update st_zq_competition set status=2 where match_id={}".format(match_id))
  1884. except Exception as e:
  1885. logger.warning('足球 赛事结束 结果插入错误')
  1886. logger.warning(e)
  1887. return item
  1888. def close_spider(self, spider):
  1889. self.conn.commit()
  1890. self.conn.close()
  1891. # 篮球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1892. class Lanjieshuqiupipeline(object):
  1893. def open_spider(self, spider):
  1894. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1895. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1896. def process_item(self, item, spider):
  1897. logger = logging.getLogger(__name__)
  1898. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1899. # 赛事id
  1900. match_id = item['id_score'][0]
  1901. # 全场比分
  1902. score = item['id_score'][1]
  1903. status = 0
  1904. game_code = 'lq'
  1905. try:
  1906. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1907. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1908. self.cursor.execute("update st_lq_result set status=2 where match_id={}".format(match_id))
  1909. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1910. self.cursor.execute("update st_lq_competition set status=2 where match_id={}".format(match_id))
  1911. except Exception as e:
  1912. logger.warning('篮球 赛事结束 结果插入错误')
  1913. logger.warning(e)
  1914. return item
  1915. def close_spider(self, spider):
  1916. self.conn.commit()
  1917. self.conn.close()
  1918. # 棒球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1919. class Bangjieshuqiupipeline(object):
  1920. def open_spider(self, spider):
  1921. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  1922. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1923. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1924. def process_item(self, item, spider):
  1925. logger = logging.getLogger(__name__)
  1926. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1927. # 赛事id
  1928. match_id = item['id_score'][0]
  1929. # 全场比分
  1930. score = item['id_score'][1]
  1931. status = 0
  1932. game_code = 'lq'
  1933. try:
  1934. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1935. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1936. self.cursor.execute("update st_bq_result set status=2 where match_id={}".format(match_id))
  1937. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1938. self.cursor.execute("update st_bq_competition set status=2 where match_id={}".format(match_id))
  1939. except Exception as e:
  1940. logger.warning('棒球 赛事结束 结果插入错误')
  1941. logger.warning(e)
  1942. return item
  1943. def close_spider(self, spider):
  1944. self.conn.commit()
  1945. self.conn.close()
  1946. # 网球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1947. class Wangjieshuqiupipeline(object):
  1948. def open_spider(self, spider):
  1949. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  1950. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1951. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1952. def process_item(self, item, spider):
  1953. logger = logging.getLogger(__name__)
  1954. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1955. # 赛事id
  1956. match_id = item['id_score'][0]
  1957. # 全场比分
  1958. score = item['id_score'][1]
  1959. status = 0
  1960. game_code = 'lq'
  1961. try:
  1962. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1963. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1964. self.cursor.execute("update st_wq_result set status=2 where match_id={}".format(match_id))
  1965. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1966. self.cursor.execute("update st_wq_competition set status=2 where match_id={}".format(match_id))
  1967. except Exception as e:
  1968. logger.warning('网球 赛事结束 结果插入错误')
  1969. logger.warning(e)
  1970. return item
  1971. def close_spider(self, spider):
  1972. self.conn.commit()
  1973. self.conn.close()