ZHGXTV.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. import time
  2. import datetime
  3. import concurrent.futures
  4. import requests
  5. import re
  6. import os
  7. import threading
  8. from queue import Queue
  9. import eventlet
  10. eventlet.monkey_patch()
  11. urls = [
  12. "http://1.183.139.1:9001",
  13. "http://1.183.141.1:8001",
  14. "http://1.191.154.1:9000",
  15. "http://1.191.155.1:9000",
  16. "http://1.193.112.1:808",
  17. "http://1.193.179.1:808",
  18. "http://1.196.106.1:888",
  19. "http://1.196.152.1:888",
  20. "http://1.196.249.1:808",
  21. "http://1.196.57.1:808",
  22. "http://1.197.168.1:9999",
  23. "http://1.197.251.1:885",
  24. "http://1.197.34.1:808",
  25. "http://1.199.136.1:8878",
  26. "http://1.24.190.1:10080",
  27. "http://1.24.190.1:10089",
  28. "http://1.59.160.1:9000",
  29. "http://1.59.161.1:9000",
  30. "http://1.59.163.1:9000",
  31. "http://1.59.164.1:9000",
  32. "http://1.59.165.1:9000",
  33. "http://1.59.167.1:9000",
  34. "http://1.70.10.1:808",
  35. "http://1.70.11.1:808",
  36. "http://1.70.14.1:808",
  37. "http://1.70.8.1:808",
  38. "http://1.80.12.1:10000",
  39. "http://1.87.218.1:7878",
  40. "http://101.17.32.1:8899",
  41. "http://101.18.25.1:808",
  42. "http://101.18.30.1:808",
  43. "http://101.224.201.1:12345",
  44. "http://101.229.56.1:12345",
  45. "http://101.23.217.1:8090",
  46. "http://101.74.233.1:9001",
  47. "http://101.75.215.1:8008",
  48. "http://106.42.108.1:888",
  49. "http://106.42.34.1:888",
  50. "http://106.42.35.1:888",
  51. "http://106.46.33.1:808",
  52. "http://106.46.37.1:808",
  53. "http://106.46.38.1:808",
  54. "http://110.181.109.1:8081",
  55. "http://110.183.50.1:808",
  56. "http://110.183.56.1:808",
  57. "http://110.19.98.1:808",
  58. "http://110.241.188.1:808",
  59. "http://110.241.189.1:808",
  60. "http://110.241.190.1:808",
  61. "http://110.241.191.1:808",
  62. "http://110.243.76.1:8899",
  63. "http://110.243.78.1:8899",
  64. "http://110.243.80.1:8899",
  65. "http://110.250.214.1:8899",
  66. "http://110.250.215.1:8899",
  67. "http://110.251.158.1:8008",
  68. "http://110.253.83.1:888",
  69. "http://110.52.8.1:808",
  70. "http://110.53.122.1:808",
  71. "http://110.72.17.1:808",
  72. "http://110.72.20.1:808",
  73. "http://110.72.25.1:808",
  74. "http://110.72.28.1:808",
  75. "http://110.72.35.1:808",
  76. "http://110.72.37.1:999",
  77. "http://110.72.39.1:808",
  78. "http://110.72.5.1:999",
  79. "http://111.112.254.1:808",
  80. "http://111.126.255.1:9999",
  81. "http://111.126.255.1:808",
  82. "http://111.22.215.1:808",
  83. "http://111.61.236.1:9081",
  84. "http://111.72.204.1:808",
  85. "http://111.72.246.1:808",
  86. "http://111.72.250.1:808",
  87. "http://111.73.151.1:808",
  88. "http://111.73.152.1:808",
  89. "http://111.73.159.1:808",
  90. "http://111.73.180.1:808",
  91. "http://112.112.249.1:808",
  92. "http://112.114.137.1:888",
  93. "http://112.115.131.1:8005",
  94. "http://112.115.132.1:8005",
  95. "http://112.123.206.1:808",
  96. "http://112.243.226.1:808",
  97. "http://112.244.224.1:808",
  98. "http://112.244.71.1:808",
  99. "http://112.244.84.1:808",
  100. "http://112.244.86.1:808",
  101. "http://112.46.85.1:8001",
  102. "http://112.46.85.1:8002",
  103. "http://112.46.85.1:8008",
  104. "http://112.54.220.1:9088",
  105. "http://112.54.220.1:9099",
  106. "http://113.101.119.1:808",
  107. "http://113.101.119.1:8090",
  108. "http://113.102.160.1:8089",
  109. "http://113.102.162.1:8089",
  110. "http://113.109.117.1:8081",
  111. "http://113.109.119.1:8081",
  112. "http://113.110.72.1:8090",
  113. "http://113.110.72.1:808",
  114. "http://113.116.181.1:8089",
  115. "http://113.116.183.1:8089",
  116. "http://113.118.13.1:808",
  117. "http://113.119.160.1:8073",
  118. "http://113.140.86.1:8009",
  119. "http://113.195.172.1:808",
  120. "http://113.200.75.1:1688",
  121. "http://113.227.108.1:777",
  122. "http://113.231.97.1:8818",
  123. "http://113.231.98.1:8818",
  124. "http://113.234.55.1:777",
  125. "http://113.235.144.1:777",
  126. "http://113.235.196.1:777",
  127. "http://113.239.88.1:8818",
  128. "http://113.239.91.1:8818",
  129. "http://113.240.156.1:8082",
  130. "http://113.240.177.1:8082",
  131. "http://113.45.171.1:465",
  132. "http://113.78.30.1:808",
  133. "http://113.81.20.1:18081",
  134. "http://113.81.21.1:18081",
  135. "http://113.86.205.1:8090",
  136. "http://113.86.205.1:808",
  137. "http://114.223.32.1:12999",
  138. "http://114.223.33.1:12999",
  139. "http://114.225.153.1:12999",
  140. "http://115.148.193.1:808",
  141. "http://115.148.193.1:8088",
  142. "http://115.152.245.1:808",
  143. "http://115.56.156.1:808",
  144. "http://116.112.42.1:808",
  145. "http://116.128.226.1:808",
  146. "http://116.131.150.1:8011",
  147. "http://116.131.173.1:809",
  148. "http://116.136.0.1:888",
  149. "http://116.136.4.1:888",
  150. "http://116.138.153.1:8888",
  151. "http://116.138.153.1:88",
  152. "http://116.138.154.1:88",
  153. "http://116.138.154.1:8888",
  154. "http://116.21.254.1:9901",
  155. "http://116.52.172.1:808",
  156. "http://117.43.20.1:808",
  157. "http://117.43.216.1:20000",
  158. "http://117.43.22.1:808",
  159. "http://117.44.0.1:20000",
  160. "http://117.44.1.1:20000",
  161. "http://117.70.230.1:808",
  162. "http://117.70.233.1:888",
  163. "http://117.70.233.1:808",
  164. "http://117.70.234.1:808",
  165. "http://117.70.234.1:888",
  166. "http://117.70.235.1:808",
  167. "http://117.72.36.1:9099",
  168. "http://117.80.41.1:8888",
  169. "http://118.249.4.1:8082",
  170. "http://118.81.213.1:8088",
  171. "http://118.81.214.1:8088",
  172. "http://118.81.220.1:8088",
  173. "http://118.81.221.1:8088",
  174. "http://118.81.222.1:8088",
  175. "http://118.81.223.1:8088",
  176. "http://119.123.79.1:9600",
  177. "http://119.123.79.1:808",
  178. "http://119.165.87.1:9901",
  179. "http://119.179.7.1:808",
  180. "http://119.251.162.1:8880",
  181. "http://119.36.176.1:18080",
  182. "http://119.54.188.1:5000",
  183. "http://120.14.172.1:8885",
  184. "http://120.192.226.1:8009",
  185. "http://120.211.138.1:808",
  186. "http://120.228.210.1:808",
  187. "http://120.234.40.1:8088",
  188. "http://120.34.136.1:8090",
  189. "http://120.4.49.1:8899",
  190. "http://120.4.50.1:8899",
  191. "http://120.7.84.1:8008",
  192. "http://120.79.17.1:808",
  193. "http://120.79.26.1:808",
  194. "http://121.18.158.1:808",
  195. "http://121.19.134.1:808",
  196. "http://121.21.213.1:8899",
  197. "http://121.21.222.1:8899",
  198. "http://121.224.161.1:8888",
  199. "http://121.224.163.1:8888",
  200. "http://121.236.157.1:8888",
  201. "http://121.238.31.1:12999",
  202. "http://121.24.98.1:8090",
  203. "http://121.24.99.1:8090",
  204. "http://121.29.191.1:8000",
  205. "http://121.29.191.1:8800",
  206. "http://121.56.37.1:808",
  207. "http://121.56.39.1:808",
  208. "http://122.142.169.1:5000",
  209. "http://122.142.171.1:5000",
  210. "http://122.142.172.1:5000",
  211. "http://122.142.178.1:5000",
  212. "http://122.142.181.1:5000",
  213. "http://122.142.185.1:5000",
  214. "http://122.142.92.1:5000",
  215. "http://122.142.95.1:5000",
  216. "http://122.4.199.1:9001",
  217. "http://122.4.199.1:9101",
  218. "http://122.4.199.1:9201",
  219. "http://123.10.77.1:808",
  220. "http://123.10.78.1:808",
  221. "http://123.10.79.1:808",
  222. "http://123.101.157.1:808",
  223. "http://123.101.159.1:808",
  224. "http://123.101.96.1:3333",
  225. "http://123.11.144.1:22222",
  226. "http://123.12.185.1:808",
  227. "http://123.12.186.1:808",
  228. "http://123.13.152.1:808",
  229. "http://123.13.155.1:808",
  230. "http://123.13.159.1:808",
  231. "http://123.13.244.1:7000",
  232. "http://123.13.245.1:7000",
  233. "http://123.13.247.1:7000",
  234. "http://123.130.175.1:808",
  235. "http://123.130.65.1:808",
  236. "http://123.130.70.1:808",
  237. "http://123.161.117.1:9999",
  238. "http://123.162.61.1:888",
  239. "http://123.182.247.1:8092",
  240. "http://123.182.60.1:9002",
  241. "http://123.184.70.1:801",
  242. "http://123.187.46.1:65000",
  243. "http://123.187.59.1:65000",
  244. "http://123.53.236.1:808",
  245. "http://123.53.36.1:9999",
  246. "http://123.53.37.1:9999",
  247. "http://123.53.38.1:9999",
  248. "http://123.53.39.1:9999",
  249. "http://123.54.198.1:808",
  250. "http://123.7.236.1:8081",
  251. "http://123.7.97.1:8099",
  252. "http://124.135.87.1:9901",
  253. "http://124.238.110.1:8880",
  254. "http://124.238.95.1:808",
  255. "http://125.41.230.1:8888",
  256. "http://125.42.173.1:808",
  257. "http://125.42.228.1:9999",
  258. "http://125.42.229.1:9999",
  259. "http://125.42.249.1:9999",
  260. "http://125.44.241.1:8008",
  261. "http://125.44.245.1:90",
  262. "http://125.44.247.1:8008",
  263. "http://125.47.117.1:888",
  264. "http://125.47.84.1:8888",
  265. "http://14.19.142.1:8089",
  266. "http://14.29.76.1:8800",
  267. "http://140.224.183.1:8090",
  268. "http://144.7.38.1:808",
  269. "http://159.75.75.1:8888",
  270. "http://171.116.70.1:8888",
  271. "http://171.13.115.1:808",
  272. "http://171.14.86.1:3333",
  273. "http://171.14.94.1:808",
  274. "http://171.15.151.1:9999",
  275. "http://171.15.5.1:808",
  276. "http://171.217.53.1:808",
  277. "http://171.221.14.1:808",
  278. "http://171.34.76.1:808",
  279. "http://171.41.123.1:88",
  280. "http://171.9.68.1:8099",
  281. "http://171.9.69.1:8099",
  282. "http://171.9.70.1:8099",
  283. "http://171.9.71.1:8099",
  284. "http://175.10.196.1:8082",
  285. "http://175.13.225.1:808",
  286. "http://175.147.19.1:88",
  287. "http://175.147.19.1:8888",
  288. "http://175.166.217.1:777",
  289. "http://175.166.218.1:777",
  290. "http://175.170.18.1:777",
  291. "http://175.171.103.1:777",
  292. "http://175.171.13.1:5111",
  293. "http://175.171.13.1:777",
  294. "http://175.171.17.1:777",
  295. "http://175.171.2.1:777",
  296. "http://175.171.24.1:5111",
  297. "http://175.171.30.1:5111",
  298. "http://175.171.8.1:777",
  299. "http://180.114.60.1:12999",
  300. "http://180.114.62.1:12999",
  301. "http://180.117.28.1:8888",
  302. "http://180.117.31.1:8888",
  303. "http://180.213.169.1:9901",
  304. "http://182.105.168.1:8088",
  305. "http://182.109.59.1:8088",
  306. "http://182.109.59.1:808",
  307. "http://182.113.198.1:888",
  308. "http://182.116.116.1:808",
  309. "http://182.116.123.1:808",
  310. "http://182.117.68.1:808",
  311. "http://182.119.164.1:8081",
  312. "http://182.122.72.1:8089",
  313. "http://182.122.73.1:8089",
  314. "http://182.123.72.1:8090",
  315. "http://182.123.73.1:8090",
  316. "http://182.123.76.1:8090",
  317. "http://182.123.77.1:8090",
  318. "http://182.123.78.1:8090",
  319. "http://182.123.79.1:8090",
  320. "http://182.150.23.1:808",
  321. "http://182.45.186.1:808",
  322. "http://182.45.241.1:808",
  323. "http://182.46.8.1:8018",
  324. "http://182.87.8.1:808",
  325. "http://182.88.216.1:808",
  326. "http://182.99.101.1:808",
  327. "http://182.99.101.1:8088",
  328. "http://183.0.214.1:2233",
  329. "http://183.166.62.1:81",
  330. "http://183.234.70.1:8088",
  331. "http://183.242.1.1:10080",
  332. "http://211.149.143.1:88",
  333. "http://211.97.63.1:801",
  334. "http://218.79.8.1:2001",
  335. "http://218.87.145.1:20000",
  336. "http://219.140.50.1:808",
  337. "http://219.140.51.1:808",
  338. "http://219.144.235.1:8090",
  339. "http://219.155.13.1:8888",
  340. "http://219.155.202.1:808",
  341. "http://219.155.227.1:8008",
  342. "http://219.156.193.1:808",
  343. "http://219.156.197.1:808",
  344. "http://219.157.193.1:888",
  345. "http://220.165.241.1:808",
  346. "http://220.176.50.1:20000",
  347. "http://220.176.60.1:20000",
  348. "http://220.202.92.1:888",
  349. "http://220.202.93.1:888",
  350. "http://220.202.95.1:888",
  351. "http://221.0.78.1:1681",
  352. "http://221.0.78.1:2082",
  353. "http://221.0.78.1:2581",
  354. "http://221.0.78.1:1181",
  355. "http://221.0.78.1:881",
  356. "http://221.201.131.1:771",
  357. "http://221.201.131.1:777",
  358. "http://221.205.239.1:8088",
  359. "http://221.211.232.1:9000",
  360. "http://221.211.233.1:9000",
  361. "http://221.214.139.1:808",
  362. "http://221.237.25.1:808",
  363. "http://222.136.210.1:808",
  364. "http://222.137.25.1:8081",
  365. "http://222.137.85.1:8081",
  366. "http://222.138.101.1:808",
  367. "http://222.138.78.1:808",
  368. "http://222.140.160.1:808",
  369. "http://222.172.183.1:808",
  370. "http://222.211.198.1:808",
  371. "http://222.211.199.1:808",
  372. "http://222.219.183.1:8089",
  373. "http://222.223.158.1:50009",
  374. "http://222.78.16.1:8090",
  375. "http://222.88.210.1:808",
  376. "http://223.166.92.1:12999",
  377. "http://27.128.203.1:808",
  378. "http://27.185.1.1:8092",
  379. "http://27.192.122.1:808",
  380. "http://27.223.246.1:9901",
  381. "http://27.41.248.1:801",
  382. "http://27.41.248.1:808",
  383. "http://27.41.249.1:801",
  384. "http://27.41.250.1:808",
  385. "http://27.41.250.1:801",
  386. "http://27.41.251.1:801",
  387. "http://27.41.251.1:808",
  388. "http://36.129.204.1:8083",
  389. "http://36.129.204.1:50001",
  390. "http://36.141.28.1:5004",
  391. "http://36.141.28.1:8083",
  392. "http://36.141.28.1:8085",
  393. "http://36.99.128.1:808",
  394. "http://36.99.134.1:808",
  395. "http://39.150.50.1:808",
  396. "http://42.176.182.1:8818",
  397. "http://42.202.19.1:8818",
  398. "http://42.224.8.1:808",
  399. "http://42.224.91.1:808",
  400. "http://42.224.93.1:808",
  401. "http://42.225.144.1:8089",
  402. "http://42.225.145.1:8089",
  403. "http://42.225.147.1:8089",
  404. "http://42.227.174.1:9999",
  405. "http://42.229.219.1:8099",
  406. "http://42.235.53.1:888",
  407. "http://42.238.169.1:888",
  408. "http://42.238.170.1:888",
  409. "http://42.48.2.1:808",
  410. "http://42.48.25.1:808",
  411. "http://42.49.216.1:808",
  412. "http://42.80.41.1:888",
  413. "http://47.100.188.1:808",
  414. "http://47.104.102.1:808",
  415. "http://47.108.221.1:9089",
  416. "http://47.108.221.1:9099",
  417. "http://47.109.181.1:88",
  418. "http://47.99.164.1:808",
  419. "http://49.76.42.1:12999",
  420. "http://58.16.141.1:8088",
  421. "http://58.17.48.1:808",
  422. "http://58.20.44.1:808",
  423. "http://58.20.87.1:808",
  424. "http://59.173.108.1:808",
  425. "http://59.173.109.1:808",
  426. "http://59.173.110.1:808",
  427. "http://59.173.111.1:808",
  428. "http://59.38.45.1:8090",
  429. "http://59.38.46.1:8090",
  430. "http://59.38.47.1:8090",
  431. "http://59.39.200.1:808",
  432. "http://59.42.60.1:808",
  433. "http://59.44.192.1:65000",
  434. "http://59.47.118.1:801",
  435. "http://59.52.184.1:8888",
  436. "http://59.52.185.1:8888",
  437. "http://59.52.186.1:8888",
  438. "http://59.52.187.1:8888",
  439. "http://59.62.94.1:808",
  440. "http://59.62.94.1:8088",
  441. "http://59.62.95.1:808",
  442. "http://60.10.139.1:8801",
  443. "http://60.10.139.1:8800",
  444. "http://60.16.18.1:4949",
  445. "http://60.17.187.1:88",
  446. "http://60.175.86.1:81",
  447. "http://60.220.147.1:808",
  448. "http://60.223.255.1:8800",
  449. "http://60.29.124.1:6080",
  450. "http://60.4.229.1:808",
  451. "http://60.6.111.1:808",
  452. "http://60.9.236.1:50003",
  453. "http://61.140.233.1:9990",
  454. "http://61.140.235.1:9990",
  455. "http://61.146.224.1:8088",
  456. "http://61.53.141.1:808",
  457. "http://61.53.82.1:808",
  458. "http://61.53.95.1:808",
  459. "http://61.54.253.1:808",
  460. "http://8.130.21.1:880",
  461. "http://122.138.218.1:50085",
  462. "http://60.22.88.1:8002",
  463. "http://58.243.9.1:808",
  464. "http://219.157.13.1:808",
  465. "http://114.104.171.1:20080",
  466. "http://120.238.53.1:2223",
  467. "http://42.49.34.1:808",
  468. "http://120.194.237.1:808",
  469. "http://218.59.233.1:3456",
  470. "http://118.122.2.1:9999",
  471. "http://221.229.246.1:808",
  472. "http://58.17.77.1:808",
  473. "http://58.20.243.1:808",
  474. "http://49.221.17.1:808",
  475. "http://58.20.202.1:808",
  476. "http://220.167.113.1:8033"
  477. ]
  478. def modify_urls(url):
  479. modified_urls = []
  480. ip_start_index = url.find("//") + 2
  481. ip_end_index = url.find(":", ip_start_index)
  482. base_url = url[:ip_start_index] # http:// or https://
  483. ip_address = url[ip_start_index:ip_end_index]
  484. port = url[ip_end_index:]
  485. ip_end = "/ZHGXTV/Public/json/live_interface.txt"
  486. for i in range(1, 256):
  487. modified_ip = f"{ip_address[:-1]}{i}"
  488. modified_url = f"{base_url}{modified_ip}{port}{ip_end}"
  489. modified_urls.append(modified_url)
  490. return modified_urls
  491. def is_url_accessible(url):
  492. try:
  493. response = requests.get(url, timeout=0.2)
  494. if response.status_code == 200:
  495. return url
  496. except requests.exceptions.RequestException:
  497. pass
  498. return None
  499. results = []
  500. x_urls = []
  501. for url in urls: # 对urls进行处理,ip第四位修改为1,并去重
  502. url = url.strip()
  503. ip_start_index = url.find("//") + 2
  504. ip_end_index = url.find(":", ip_start_index)
  505. ip_dot_start = url.find(".") + 1
  506. ip_dot_second = url.find(".", ip_dot_start) + 1
  507. ip_dot_three = url.find(".", ip_dot_second) + 1
  508. base_url = url[:ip_start_index] # http:// or https://
  509. ip_address = url[ip_start_index:ip_dot_three]
  510. port = url[ip_end_index:]
  511. ip_end = "1"
  512. modified_ip = f"{ip_address}{ip_end}"
  513. x_url = f"{base_url}{modified_ip}{port}"
  514. x_urls.append(x_url)
  515. urls = set(x_urls) # 去重得到唯一的URL列表
  516. valid_urls = []
  517. # 多线程获取可用url
  518. with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
  519. futures = []
  520. for url in urls:
  521. url = url.strip()
  522. modified_urls = modify_urls(url)
  523. for modified_url in modified_urls:
  524. futures.append(executor.submit(is_url_accessible, modified_url))
  525. for future in concurrent.futures.as_completed(futures):
  526. result = future.result()
  527. if result:
  528. valid_urls.append(result)
  529. print(result)
  530. #for url in valid_urls:
  531. # print(url)
  532. #now_today = datetime.date.today()
  533. #with open("ip.txt", 'a', encoding='utf-8') as file:
  534. # file.write(f"{now_today}更新\n")
  535. # for url in valid_urls:
  536. # file.write(url + "\n")
  537. # 遍历网址列表,获取JSON文件并解析
  538. for url in valid_urls:
  539. try:
  540. # 发送GET请求获取JSON文件,设置超时时间为0.5秒
  541. json_url = f"{url}"
  542. response = requests.get(json_url, timeout=1)
  543. json_data = response.content.decode('utf-8')
  544. try:
  545. # 按行分割数据
  546. lines = json_data.split('\n')
  547. for line in lines:
  548. line = line.strip()
  549. if line:
  550. name, channel_url = line.split(',')
  551. urls = channel_url.split('/', 3)
  552. url_data = json_url.split('/', 3)
  553. if len(urls) >= 4:
  554. urld = (f"{urls[0]}//{url_data[2]}/{urls[3]}")
  555. else:
  556. urld = (f"{urls[0]}//{url_data[2]}")
  557. print(f"{name},{urld}")
  558. if name and urld:
  559. # 删除特定文字
  560. name = name.replace("cctv", "CCTV")
  561. name = name.replace("中央", "CCTV")
  562. name = name.replace("央视", "CCTV")
  563. name = name.replace("高清", "")
  564. name = name.replace("超高", "")
  565. name = name.replace("HD", "")
  566. name = name.replace("标清", "")
  567. name = name.replace("频道", "")
  568. name = name.replace("-", "")
  569. name = name.replace(" ", "")
  570. name = name.replace("PLUS", "+")
  571. name = name.replace("+", "+")
  572. name = name.replace("(", "")
  573. name = name.replace(")", "")
  574. name = re.sub(r"CCTV(\d+)台", r"CCTV\1", name)
  575. name = name.replace("CCTV1综合", "CCTV1")
  576. name = name.replace("CCTV2财经", "CCTV2")
  577. name = name.replace("CCTV3综艺", "CCTV3")
  578. name = name.replace("CCTV4国际", "CCTV4")
  579. name = name.replace("CCTV4中文国际", "CCTV4")
  580. name = name.replace("CCTV4欧洲", "CCTV4")
  581. name = name.replace("CCTV5体育", "CCTV5")
  582. name = name.replace("CCTV6电影", "CCTV6")
  583. name = name.replace("CCTV7军事", "CCTV7")
  584. name = name.replace("CCTV7军农", "CCTV7")
  585. name = name.replace("CCTV7农业", "CCTV7")
  586. name = name.replace("CCTV7国防军事", "CCTV7")
  587. name = name.replace("CCTV8电视剧", "CCTV8")
  588. name = name.replace("CCTV9记录", "CCTV9")
  589. name = name.replace("CCTV9纪录", "CCTV9")
  590. name = name.replace("CCTV10科教", "CCTV10")
  591. name = name.replace("CCTV11戏曲", "CCTV11")
  592. name = name.replace("CCTV12社会与法", "CCTV12")
  593. name = name.replace("CCTV13新闻", "CCTV13")
  594. name = name.replace("CCTV新闻", "CCTV13")
  595. name = name.replace("CCTV14少儿", "CCTV14")
  596. name = name.replace("CCTV15音乐", "CCTV15")
  597. name = name.replace("CCTV16奥林匹克", "CCTV16")
  598. name = name.replace("CCTV17农业农村", "CCTV17")
  599. name = name.replace("CCTV17农业", "CCTV17")
  600. name = name.replace("CCTV5+体育赛视", "CCTV5+")
  601. name = name.replace("CCTV5+体育赛事", "CCTV5+")
  602. name = name.replace("CCTV5+体育", "CCTV5+")
  603. results.append(f"{name},{urld}")
  604. except:
  605. continue
  606. except:
  607. continue
  608. channels = []
  609. for result in results:
  610. line = result.strip()
  611. if result:
  612. channel_name, channel_url = result.split(',')
  613. channels.append((channel_name, channel_url))
  614. # 线程安全的队列,用于存储下载任务
  615. task_queue = Queue()
  616. # 线程安全的列表,用于存储结果
  617. results = []
  618. error_channels = []
  619. # 定义工作线程函数
  620. def worker():
  621. while True:
  622. # 从队列中获取一个任务
  623. channel_name, channel_url = task_queue.get()
  624. try:
  625. channel_url_t = channel_url.rstrip(channel_url.split('/')[-1]) # m3u8链接前缀
  626. lines = requests.get(channel_url, timeout = 1).text.strip().split('\n') # 获取m3u8文件内容
  627. ts_lists = [line.split('/')[-1] for line in lines if line.startswith('#') == False] # 获取m3u8文件下视频流后缀
  628. ts_lists_0 = ts_lists[0].rstrip(ts_lists[0].split('.ts')[-1]) # m3u8链接前缀
  629. ts_url = channel_url_t + ts_lists[0] # 拼接单个视频片段下载链接
  630. # 多获取的视频数据进行5秒钟限制
  631. with eventlet.Timeout(5, False):
  632. start_time = time.time()
  633. content = requests.get(ts_url, timeout = 1).content
  634. end_time = time.time()
  635. response_time = (end_time - start_time) * 1
  636. if content:
  637. with open(ts_lists_0, 'ab') as f:
  638. f.write(content) # 写入文件
  639. file_size = len(content)
  640. # print(f"文件大小:{file_size} 字节")
  641. download_speed = file_size / response_time / 1024
  642. # print(f"下载速度:{download_speed:.3f} kB/s")
  643. normalized_speed = min(max(download_speed / 1024, 0.001), 100) # 将速率从kB/s转换为MB/s并限制在1~100之间
  644. #print(f"标准化后的速率:{normalized_speed:.3f} MB/s")
  645. # 删除下载的文件
  646. os.remove(ts_lists_0)
  647. result = channel_name, channel_url, f"{normalized_speed:.3f} MB/s"
  648. results.append(result)
  649. numberx = (len(results) + len(error_channels)) / len(channels) * 100
  650. print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
  651. except:
  652. error_channel = channel_name, channel_url
  653. error_channels.append(error_channel)
  654. numberx = (len(results) + len(error_channels)) / len(channels) * 100
  655. print(f"可用频道:{len(results)} 个 , 不可用频道:{len(error_channels)} 个 , 总频道:{len(channels)} 个 ,总进度:{numberx:.2f} %。")
  656. # 标记任务完成
  657. task_queue.task_done()
  658. # 创建多个工作线程
  659. num_threads = 10
  660. for _ in range(num_threads):
  661. t = threading.Thread(target=worker, daemon=True) # 将工作线程设置为守护线程
  662. t.start()
  663. # 添加下载任务到队列
  664. for channel in channels:
  665. task_queue.put(channel)
  666. # 等待所有任务完成
  667. task_queue.join()
  668. def channel_key(channel_name):
  669. match = re.search(r'\d+', channel_name)
  670. if match:
  671. return int(match.group())
  672. else:
  673. return float('inf') # 返回一个无穷大的数字作为关键字
  674. # 对频道进行排序
  675. results.sort(key=lambda x: (x[0], -float(x[2].split()[0])))
  676. results.sort(key=lambda x: channel_key(x[0]))
  677. result_counter = 8 # 每个频道需要的个数
  678. with open("itvlist.txt", 'w', encoding='utf-8') as file:
  679. channel_counters = {}
  680. file.write('央视频道,#genre#\n')
  681. for result in results:
  682. channel_name, channel_url, speed = result
  683. if 'CCTV' in channel_name:
  684. if channel_name in channel_counters:
  685. if channel_counters[channel_name] >= result_counter:
  686. continue
  687. else:
  688. file.write(f"{channel_name},{channel_url}\n")
  689. channel_counters[channel_name] += 1
  690. else:
  691. file.write(f"{channel_name},{channel_url}\n")
  692. channel_counters[channel_name] = 1
  693. channel_counters = {}
  694. file.write('卫视频道,#genre#\n')
  695. for result in results:
  696. channel_name, channel_url, speed = result
  697. if '卫视' in channel_name:
  698. if channel_name in channel_counters:
  699. if channel_counters[channel_name] >= result_counter:
  700. continue
  701. else:
  702. file.write(f"{channel_name},{channel_url}\n")
  703. channel_counters[channel_name] += 1
  704. else:
  705. file.write(f"{channel_name},{channel_url}\n")
  706. channel_counters[channel_name] = 1
  707. channel_counters = {}
  708. file.write('其他频道,#genre#\n')
  709. for result in results:
  710. channel_name, channel_url, speed = result
  711. if 'CCTV' not in channel_name and '卫视' not in channel_name and '测试' not in channel_name:
  712. if channel_name in channel_counters:
  713. if channel_counters[channel_name] >= result_counter:
  714. continue
  715. else:
  716. file.write(f"{channel_name},{channel_url}\n")
  717. channel_counters[channel_name] += 1
  718. else:
  719. file.write(f"{channel_name},{channel_url}\n")
  720. channel_counters[channel_name] = 1
  721. with open("itvlist.m3u", 'w', encoding='utf-8') as file:
  722. channel_counters = {}
  723. file.write('#EXTM3U\n')
  724. for result in results:
  725. channel_name, channel_url, speed = result
  726. if 'CCTV' in channel_name:
  727. if channel_name in channel_counters:
  728. if channel_counters[channel_name] >= result_counter:
  729. continue
  730. else:
  731. file.write(f"#EXTINF:-1 group-title=\"央视频道\",{channel_name}\n")
  732. file.write(f"{channel_url}\n")
  733. channel_counters[channel_name] += 1
  734. else:
  735. file.write(f"#EXTINF:-1 group-title=\"央视频道\",{channel_name}\n")
  736. file.write(f"{channel_url}\n")
  737. channel_counters[channel_name] = 1
  738. channel_counters = {}
  739. #file.write('卫视频道,#genre#\n')
  740. for result in results:
  741. channel_name, channel_url, speed = result
  742. if '卫视' in channel_name:
  743. if channel_name in channel_counters:
  744. if channel_counters[channel_name] >= result_counter:
  745. continue
  746. else:
  747. file.write(f"#EXTINF:-1 group-title=\"卫视频道\",{channel_name}\n")
  748. file.write(f"{channel_url}\n")
  749. channel_counters[channel_name] += 1
  750. else:
  751. file.write(f"#EXTINF:-1 group-title=\"卫视频道\",{channel_name}\n")
  752. file.write(f"{channel_url}\n")
  753. channel_counters[channel_name] = 1
  754. channel_counters = {}
  755. #file.write('其他频道,#genre#\n')
  756. for result in results:
  757. channel_name, channel_url, speed = result
  758. if 'CCTV' not in channel_name and '卫视' not in channel_name and '测试' not in channel_name:
  759. if channel_name in channel_counters:
  760. if channel_counters[channel_name] >= result_counter:
  761. continue
  762. else:
  763. file.write(f"#EXTINF:-1 group-title=\"其他频道\",{channel_name}\n")
  764. file.write(f"{channel_url}\n")
  765. channel_counters[channel_name] += 1
  766. else:
  767. file.write(f"#EXTINF:-1 group-title=\"其他频道\",{channel_name}\n")
  768. file.write(f"{channel_url}\n")
  769. channel_counters[channel_name] = 1