YaakovTooth

Yaakov Paste

Jun 13th, 2011
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. import io
  3. import sys
  4. import json
  5. # import urllib
  6. import urllib2 as urllib
  7. import random
  8. import re
  9. from urlparse import urlparse
  10. from main_form import Ui_MainWindow as main_form
  11. from captcha_form import Ui_MainWindow as captcha_form
  12. from PyQt4 import QtCore, QtGui, uic
  13. from YaakovHTTP import YaakovHTTP
  14. def regex_get(regex,string):
  15. return re.search(regex,string).group(0)
  16.  
  17. def regex_replace(regex,substring,string):
  18. return re.sub(regex,substring,string)
  19.  
  20. class Captcha(QtGui.QMainWindow):
  21. def __init__(self, parent=None):
  22. QtGui.QDialog.__init__(self, parent)
  23. self.ui=captcha_form()
  24. self.ui.setupUi(self)
  25. self.ui.captcha_text.setFocus()
  26. self.connect(self.ui.captcha_text, QtCore.SIGNAL('returnPressed()'), self.form_submit)
  27. self.captcha = u''
  28.  
  29. def set_picture(self, picture):
  30. pic=QtGui.QGraphicsScene()
  31. image=QtGui.QPixmap()
  32. image.loadFromData(picture)
  33. pic.addPixmap(image)
  34. self.ui.captcha_picture.setScene(pic)
  35. self.set_resize(image.width(),image.height())
  36.  
  37. def set_resize(self,x,y):
  38. x = x if x in range(220,600) else 220
  39. y = y if y in range(150,500) else 150
  40. self.setFixedSize(x+10,y+55)
  41. self.ui.captcha_picture.setGeometry(QtCore.QRect(0, 0, x+10, y+10))
  42. self.ui.captcha_text.setGeometry(QtCore.QRect(((x+10-160)/2), y+15, 160, 30))
  43.  
  44. def form_submit(self):
  45. if len(self.ui.captcha_text.text()) > 0:
  46. self.close()
  47. main.send_message(unicode(self.ui.captcha_text.text()))
  48. else: self.close()
  49.  
  50. def form_close(self):
  51. # main.form_close()
  52. self.close()
  53.  
  54. def keyPressEvent(self,event):
  55. if event.nativeScanCode()==1: self.form_close()
  56.  
  57.  
  58. class Main(QtGui.QMainWindow):
  59. def __init__(self, parent=None):
  60. QtGui.QDialog.__init__(self, parent)
  61. self.ui=main_form()
  62. self.ui.setupUi(self)
  63. self.setFixedSize(399, 214)
  64. self.http = YaakovHTTP()
  65.  
  66. self.c={}
  67. self.c['operator']=0
  68. self.c['number']='+7 (922) 612-52_74'
  69. self.c['message']=u''
  70. self.c['proxy_enable']=0
  71. self.c['proxy']=u'microsoft.com'
  72. self.c['port']=666
  73.  
  74. try: self.c = json.load(open('pager.json','r'))
  75. except: pass
  76.  
  77. self.ui.operator.setCurrentIndex(self.c['operator'])
  78. self.ui.text_input.setPlainText(self.c['message'])
  79. self.ui.text_input.selectAll()
  80. self.ui.check_proxy.setCheckState(self.c['proxy_enable'])
  81. self.ui.proxy.setText(self.c['proxy']+':'+str(self.c['port']))
  82. self.ui.number.setText(self.c['number'])
  83. self.ui.number.selectAll()
  84.  
  85. self.__last_scancode=0
  86. self.connect(self.ui.button_send, QtCore.SIGNAL("pressed()"), self.form_submit)
  87. self.connect(self.ui.button_close, QtCore.SIGNAL("pressed()"), self.form_close)
  88. self.connect(self.ui.check_proxy, QtCore.SIGNAL("clicked()"), self.toggle_proxy)
  89. self.connect(self.ui.proxy, QtCore.SIGNAL('returnPressed()'), self.ui.text_input.setFocus)
  90. self.connect(self.ui.proxy, QtCore.SIGNAL("textChanged(QString)"), self.check_proxy)
  91. self.connect(self.ui.number, QtCore.SIGNAL('returnPressed()'), self.ui.text_input.setFocus)
  92. self.connect(self.ui.number, QtCore.SIGNAL("textChanged(QString)"), self.check_number)
  93. self.connect(self.ui.text_input, QtCore.SIGNAL("textChanged()"), self.check_input)
  94. self.toggle_proxy()
  95. self.check_input()
  96.  
  97. def settings_save(self):
  98. self.c['operator'] = self.ui.operator.currentIndex()
  99. self.c['number'] = str(self.ui.number.text())
  100. open('pager.json','w').write(json.dumps(self.c))
  101.  
  102. def form_submit(self):
  103. if not self.check_input():
  104. self.statusBar().showMessage(u'Сообщение должно быть от 10 до 140 символов.')
  105. return
  106.  
  107. if not self.check_number():
  108. self.statusBar().showMessage(u'Неправильный номер.')
  109. return
  110.  
  111. if self.toggle_proxy():
  112. if not self.check_proxy():
  113. self.statusBar().showMessage(u'Прокси записывается в виде хоста и номера порта через двоеточие.')
  114. return
  115. else: self.http.set_proxy(self.c['proxy'] + ':' + str(self.c['port']))
  116.  
  117. self.http.add_header('User-Agent: Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.8.131 Version/11.10');
  118. if self.ui.operator.currentIndex() == 0:
  119. self.http.set_url('http://ural.sendsms.megafon.ru/')
  120. elif self.ui.operator.currentIndex() == 1:
  121. self.http.set_url('http://cell.motivtelecom.ru/ekb/smssend')
  122. self.http.add_header('Referer: http://cell.motivtelecom.ru/ekb/b2c')
  123.  
  124. self.http.perform()
  125. if not self.http.response_code == 200:
  126. self.statusBar().showMessage(u'Ошибка при запросе страницы, сервер ответил: ' + str(self.http.response_code) + ' ' + self.http.response_message + '.')
  127. else:
  128. if self.ui.operator.currentIndex() == 0:
  129. self.http.set_url('http://ural.sendsms.megafon.ru/image.phtml?k=' + regex_get('(?<=value=")\d+',regex_get('(?<=name="codekey").*',self.http.response_content + '')))
  130. elif self.ui.operator.currentIndex() == 1:
  131. if not 'ycc' in self.http.response_cookie:
  132. self.statusBar().showMessage(u'Ошибка: куки сессии не установлены, сервер ответил: ' + str(self.http.response_code) + ' ' + self.http.response_message + '.')
  133. return
  134. else:
  135. self.http.add_header('Cookie: ycc=' + self.http.response_cookie['ycc'])
  136. self.http.set_url('http://cell.motivtelecom.ru/ekb/phpcaptcha/image?rand=' + str(random.randint(1000001,9999999)))
  137.  
  138. self.http.perform()
  139. if not self.http.response_code == 200:
  140. self.statusBar().showMessage(u'Ошибка при запросе капчи, сервер ответил: ' + str(self.http.response_code) + ' ' + self.http.response_message + '.')
  141. else:
  142. captcha.set_picture(self.http.response_content)
  143. captcha.show()
  144. def send_message(self,captcha):
  145. number = self.check_number()
  146. if self.ui.operator.currentIndex() == 0:
  147. self.http.set_url('http://ural.sendsms.megafon.ru/')
  148. elif self.ui.operator.currentIndex() == 1:
  149. self.http.set_url('http://cell.motivtelecom.ru/ekb/smssend')
  150. self.http.add_header('Content-Type: application/x-www-form-urlencoded')
  151. self.http.set_method('POST')
  152. codes = [90019,90020,90021,90287,90416,90417,90438,90454,90498,90863,90890,90891,90892,95019,95020,95063,95064,95065,95213,95214,95272,95273,95274,95300,95303,95304,95305,95338,95360]
  153. if int(number[1:6]) not in codes:
  154. self.statusBar().showMessage(u'Это не номер Мотива.')
  155. else: index = codes.index(int(number[1:6]))
  156. msg = urllib.quote('smssend[name]=Ant&smssend[code]=' + str(index) + '&smssend[phone]=' + str(number[6:]) + '&smssend[txt]=','&=') + urllib.quote(unicode(self.ui.text_input.toPlainText()).encode('UTF-8'),'&=').replace('+','%20') + urllib.quote('&smssend[hdelay]=0&smssend[mdelay]=0&smssend[captcha]=' + captcha,'&=')
  157. self.http.add_request_data(msg)
  158. print(msg)
  159. self.http.perform()
  160. print(self.http.response_message)
  161. if not self.http.response_code == 200:
  162. self.statusBar().showMessage(u'Ошибка отправки:' + str(self.http.response_code) + ' ' + self.http.response_message)
  163. elif regex_get('(?<=Сообщение )[^<.]*',self.http.response_content) == 'отправлено':
  164. self.statusBar().showMessage(u'Сообщение отправлено!')
  165. self.ui.text_input.clear()
  166. else:
  167. self.statusBar().showMessage(u'Ошибка отправки - сервер не принял сообщение.')
  168.  
  169. def form_clear(self):
  170. self.ui.text_input.clear()
  171. self.ui.text_input.setFocus()
  172.  
  173. def form_close(self):
  174. self.settings_save()
  175. self.close()
  176.  
  177. def toggle_proxy(self):
  178. if self.ui.check_proxy.isChecked():
  179. self.ui.proxy.setReadOnly(0)
  180. self.c['proxy_enable'] = 2
  181. return 1
  182. else:
  183. self.ui.proxy.setReadOnly(1)
  184. self.c['proxy_enable'] = 0
  185. return
  186.  
  187. def check_number(self):
  188. __num = str(self.ui.number.text())
  189. __num = regex_replace('[^0-9]','',__num)
  190. __num = '7' + __num[1:] if len(__num) == 11 and __num[0] == '8' else __num
  191. self.c['number'] = __num
  192. if len(__num) == 11 and __num[0:2] == '79':
  193. self.setStyleSheet('QLineEdit#number{border: 1px solid #aaa;background: #fff;}')
  194. self.ui.number.setText('+' + __num[0] + ' (' + __num[1:4] + ') ' + __num[4:7] + '-' + __num[7:9] + '-' + __num[9:11])
  195. return __num
  196. else:
  197. self.setStyleSheet('QLineEdit#number{border: 1px solid red;background: #fee;}')
  198.  
  199. def check_proxy(self):
  200. __proxy = ''
  201. __port = 0
  202. __string = unicode(self.ui.proxy.text())
  203. if __string.find(':') > 0:
  204. __port = __string[__string.find(':') + 1:]
  205. __proxy = __string[:__string.find(':')]
  206. try: __port = int(__port)
  207. except: __port = 0
  208. self.c['proxy'] = __proxy
  209. self.c['port'] = __port
  210.  
  211. if len(self.c['proxy']) > 0 and self.c['port'] in range(1,65535):
  212. self.setStyleSheet('QLineEdit#proxy{border: 1px solid #aaa;background: #fff;}')
  213. return 1
  214. else:
  215. self.setStyleSheet('QLineEdit#proxy{border: 1px solid red;background: #fee;}')
  216. return
  217.  
  218. def check_input(self):
  219. __max = 200
  220. __text = unicode(self.ui.text_input.toPlainText())
  221. __length = len(__text)
  222. self.ui.counter.display(__length)
  223. if __length>__max:
  224. self.ui.text_input.setPlainText(__text[0:__max])
  225. __index = int(((float(__length)/140)*255))
  226. if __index not in range(0,255):__index = 255
  227. __color = str(hex(__index))[2:]
  228. if len(__color) == 1:__color = '0'+__color
  229. self.setStyleSheet('QPlainTextEdit{color: #'+__color+'0000;}')
  230. self.c['message'] = unicode(self.ui.text_input.toPlainText())
  231. if len(__text) in range(10,__max): return 1
  232.  
  233. def keyPressEvent(self,event):
  234. if event.nativeScanCode()==28 and self.__last_scancode==285:
  235. # self.form_submit()
  236. self.send_message('captcha')
  237. elif event.nativeScanCode()==28:
  238. self.ui.text_input.setFocus()
  239. elif event.nativeScanCode()==1:
  240. self.form_close()
  241. self.__last_scancode=event.nativeScanCode()
  242.  
  243. app=QtGui.QApplication(sys.argv)
  244. app.setStyleSheet(open("pager.qss","r").read())
  245.  
  246. main=Main()
  247. main.show()
  248.  
  249. captcha=Captcha()
  250. sys.exit(app.exec_())
RAW Paste Data