Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. import requests, hashlib, http.cookiejar, sqlite3, datetime, time
  2. from fake_useragent import UserAgent
  3. from bs4 import BeautifulSoup
  4.  
  5. WEBSITE_URL = 'http://www.gta-sarp.com/forums/login.php?do=login'
  6. FRR_URL = 'http://www.gta-sarp.com/forums/forumdisplay.php?15-Forum-Related'
  7. HANDLED_URL = 'http://www.gta-sarp.com/forums/forumdisplay.php?1535-Handled/page'
  8. ua = UserAgent()
  9.  
  10.  
  11. def create_table():
  12.     conn = sqlite3.connect('sarp.db')
  13.     c = conn.cursor()
  14.     c.execute("CREATE TABLE IF NOT EXISTS threads_frr ("
  15.               "thread_id text NOT NULL UNIQUE);")
  16.  
  17.     c.execute("CREATE TABLE IF NOT EXISTS threads_frr_handled ("
  18.               "thread_id text NOT NULL UNIQUE,"
  19.               "handled_by text NOT NULL,"
  20.               "user_id text NOT NULL);")
  21.  
  22.  
  23. def print_table():
  24.     conn = sqlite3.connect('example.db')
  25.     c = conn.cursor()
  26.     c.execute("SELECT * FROM threads_frr")
  27.     for row in c:
  28.         print(row)
  29.  
  30.  
  31. class Account:
  32.     def __init__(self, username, password):
  33.         self.username = username
  34.         self.password = password
  35.         self.headers = {'User-Agent': str(ua.random)}
  36.         self.cookie_jar = http.cookiejar.CookieJar()
  37.  
  38.         self.session = requests.Session()
  39.         self.soup = None
  40.  
  41.         self.log_in()
  42.  
  43.     def log_in(self):
  44.         self.session.post(WEBSITE_URL, data={'vb_login_username': self.username,
  45.                                              'vb_login_password': self.password,
  46.  
  47.                                              's': '',
  48.                                              'securitytoken': 'guest',
  49.                                              'do': 'login',
  50.                                              'vb_login_md5password': hashlib.md5(self.password.encode()).hexdigest(),
  51.                                              'vb_login_md5password_utf': hashlib.md5(self.password.encode()).hexdigest()
  52.                                              })
  53.  
  54.     def is_logged_in(self):
  55.         r = self.session.get("http://www.gta-sarp.com/forums/forumdisplay.php?2213-The-Golden-Club",
  56.                              cookies=self.cookie_jar)
  57.         if "You are not logged in. Fill in the form at the bottom of this page and try again" in r.text:
  58.             return False
  59.         else:
  60.             return True
  61.  
  62.     def check_threads(self):
  63.         while True:
  64.             if datetime.datetime.now().minute % 3 == 0:
  65.                 threads = []
  66.                 self.soup = BeautifulSoup(self.session.get(FRR_URL).content, "html.parser")
  67.  
  68.                 for thread in self.soup.select("ol.threads li"):
  69.                     if thread.get('id') is not None:
  70.                         threads.append(thread.get('id'))
  71.  
  72.                 conn = sqlite3.connect('sarp.db')
  73.                 c = conn.cursor()
  74.                 for thread in threads:
  75.                     c.execute("SELECT thread_id FROM threads_frr WHERE thread_id=?", (thread,))
  76.                     if c.fetchone() is None:
  77.                         c.execute("INSERT INTO threads_frr VALUES (?);", (thread,))
  78.                         print(datetime.datetime.now(), "A forum related request has been posted!")
  79.                     conn.commit()
  80.             time.sleep(60)
  81.  
  82.     def collect_data(self):
  83.         page = 1
  84.         counter = 0
  85.         thread_info = []
  86.         conn = sqlite3.connect('sarp.db')
  87.         c = conn.cursor()
  88.  
  89.         while page < 3:
  90.             self.soup = BeautifulSoup(self.session.get(HANDLED_URL + str(page)).content, "html.parser")
  91.             """
  92.            for thread in self.soup.select("ol.threads li"):
  93.                if thread.get('id') is not None:
  94.                    thread_info[thread.get('id')] = {}
  95.                    user_info = thread.select("dl.threadlastpost dd:nth-of-type(1)")
  96.                    user_info = user_info.find('a').get('href')
  97.                    user_id = user_info[user_info.index('?') + 1:user_info.index('-')]
  98.                    user_name = user_info[user_info.index('-')+1:]
  99.                    user_name = user_name.replace('-', ' ')
  100.                    thread_info[thread.get('id')]['username'] = user_name
  101.                    thread_info[thread.get('id')]['user_id'] = user_id
  102.            """
  103.             threads = [thread for thread in self.soup.select("ol.threads li.threadbit")]
  104.             for thread in threads:
  105.                 thread_id = thread.get('id')
  106.                 c.execute("SELECT thread_id FROM threads_frr_handled WHERE thread_id=?", (thread_id,))
  107.                 if c.fetchone() is not None:
  108.                     continue
  109.                 info = thread.find('a', {"class": "username", "class": "popupctrl"})
  110.                 if info is not None:
  111.                     info = info.get('href')
  112.                 else:
  113.                     continue
  114.                 counter = counter + 1
  115.                 user_id = info[info.index('?') + 1:info.index('-')]
  116.                 user_name = info[info.index('-') + 1:]
  117.                 this_thread = [thread_id, user_name, user_id]
  118.                 thread_info.append(this_thread)
  119.             page = page + 1
  120.  
  121.  
  122.         c.executemany("INSERT INTO threads_frr_handled VALUES (?,?,?)", thread_info)
  123.         conn.commit()
  124.         print(counter, "entries added.")
  125.  
  126.  
  127. a = Account('X', 'X')
  128. a.log_in()
  129. a.collect_data()
  130. conn = sqlite3.connect('sarp.db')
  131. c = conn.cursor()
  132. c.execute("SELECT handled_by, count(*) FROM threads_frr_handled GROUP BY handled_by")
  133. rows = c.fetchall()
  134. for row in rows:
  135.     print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement