pipelines.py 137 KB

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