Ivars 4 years ago
parent
commit
e01410dd1f
1 changed files with 23 additions and 42 deletions
  1. 23
    42
      ltcproxy.py

+ 23
- 42
ltcproxy.py View File

@@ -12,21 +12,15 @@ usage: %s start|stop|restart|manualstart [options]
12 12
 __version__ = "0.1b"
13 13
 
14 14
 import os, sys, time
15
-import urllib
15
+import urllib,urlparse, urllib2, requests
16 16
 from urllib import unquote, quote
17
-import urlparse
18
-import requests
19
-import re
20
-import json
17
+import re, json
21 18
 import ConfigParser, getopt
22 19
 import arrow
23 20
 from diskcache import Cache
24 21
 import daemonize
25 22
 import bottle
26 23
 from bottle import Bottle, hook, response, route, request, run
27
-import urllib3
28
-urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
29
-
30 24
 
31 25
 cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
32 26
 cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
@@ -480,27 +474,23 @@ Content-Type: application/x-www-form-urlencoded; charset=UTF-8
480 474
 Host: manstv.lattelecom.tv
481 475
 """ )
482 476
     try:
483
-        r = requests.post(url, data=params, headers=headers, verify=False)
477
+        r = urllib2.Request(url, data=params, headers=headers)
478
+        u = urllib2.urlopen(r)
479
+        content = u.read()
480
+        u.close()
484 481
     except Exception as ex:
485 482
         if DEBUG:
486
-            print "Login exception - %s " % (str(ex))
487
-            if "hdrs" in dir(ex):
488
-                print ex.hdrs
489
-        return None
490
-    if not r.ok:
491
-        if DEBUG:
492
-            print "Login error - %s: %s" % (r.status_code, r.reason)
483
+            print "Login error: %s - %s" % (ex.code, ex.msg)
484
+            print ex.hdrs
493 485
         return None
494
-
495
-    content = r.content
496
-    if not "token" in content:
486
+    if u and "token" in content:
487
+        token = re.search('"token":"(.+?)"', content).group(1)
488
+        return token
489
+    else:
497 490
         if DEBUG:
498 491
             print "Error searching token in response"
499 492
             print content
500
-        return None
501
-    token = re.search('"token":"(.+?)"', content).group(1)
502
-    return token
503
-
493
+        return False
504 494
 
505 495
 def refresh_token(token):
506 496
     """Refresh"""
@@ -511,28 +501,19 @@ def refresh_token(token):
511 501
 User-Agent: Shortcut.lv v2.9.1 / Dalvik/1.6.0 (Linux; U; Android 4.4.2; SM-G900FD Build/KOT49H)
512 502
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
513 503
 Host: manstv.lattelecom.tv
514
-""")
504
+""" )
515 505
     try:
516
-        r = requests.post(url, data=params, headers=headers, verify = False)
506
+        r = urllib2.Request(url, data=params, headers=headers)
507
+        u = urllib2.urlopen(r)
508
+        content = u.read()
509
+        u.close()
517 510
     except Exception as ex:
518
-        if DEBUG:
519
-            print "Login exception - %s " % (str(ex))
520
-            if "hdrs" in dir(ex):
521
-                print ex.hdrs
522
-        return None
523
-    if not r.ok:
524
-        if DEBUG:
525
-            print "Login error - %s: %s" % (r.status_code, r.reason)
526 511
         return None
527
-
528
-    content = r.content
529
-    if not "token" in content:
530
-        if DEBUG:
531
-            print "Error searching token in response"
532
-            print content
533
-        return None
534
-    token2 = re.search('"token":"(.+?)"', content).group(1)
535
-    return token2
512
+    if r and "token" in content:
513
+        token2 = re.search('"token":"(.+?)"', content).group(1)
514
+        return token2
515
+    else:
516
+        return False
536 517
 
537 518
 def print_headers(headers):
538 519
     for h in headers: