pipelines.py 138 KB

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