pipelines.py 130 KB

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