Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import pymysql.cursors
  3. import datetime
  4. import requests
  5. import time
  6.  
  7. class GetUsers:
  8. def __init__(self, tags):
  9. self.tags = tags
  10.  
  11. def __dataHalaman(self, tag):
  12. self.tag = tag
  13. page = requests.get('http://picbear.com/tag/' + self.tag, verify=False)
  14. time.sleep(1)
  15. soup = BeautifulSoup(page.content, 'html.parser')
  16. time.sleep(1)
  17. return soup
  18.  
  19. def __writeLog(self, tag, count):
  20. self.tag = tag
  21. self.count = count
  22. log = open('log.txt', 'a')
  23. log.write('TagName: ' + self.tag + '; UserCount: ' + str(self.count) + '; Time: ' + str(datetime.datetime.now()) + "\n")
  24. log.close()
  25.  
  26. # Remove duplicates from this list.
  27. def __removeDuplicates(self, arrays):
  28. self.arrays = arrays
  29. output = []
  30. seen = set()
  31.  
  32. for value in self.arrays:
  33. # If value has not been encountered yet,
  34. # ... add it to both list and set.
  35. if value not in seen:
  36. output.append(value)
  37. seen.add(value)
  38.  
  39. return output
  40.  
  41. def getThem(self):
  42. usernames = []
  43.  
  44. for number, tag in enumerate(self.tags, start=1):
  45. try:
  46. count = 0
  47. soup = self.__dataHalaman(tag)
  48. users = soup.findAll('a',{'class':'grid-media-owner'})
  49. for user in users:
  50. count += 1
  51. user = user['href'].rsplit('/')
  52. usernames.insert(len(usernames), user[-1])
  53. except Exception as e:
  54. print(e)
  55. finally:
  56. time.sleep(1)
  57. self.__writeLog(tag, count)
  58.  
  59. usernames = self.__removeDuplicates(usernames)
  60. return usernames
  61.  
  62.  
  63.  
  64. class SaveUsers:
  65. def __init__(self, users):
  66. self.users = users
  67.  
  68. def __myConnect(self, host, user, password, dbname):
  69. self.host = host
  70. self.user = user
  71. self.password = password
  72. self.dbname =dbname
  73. return pymysql.connect(host = self.host, user = self.user, password = self.password, db = self.dbname, charset = 'utf8mb4', cursorclass = pymysql.cursors.DictCursor)
  74.  
  75.  
  76. def save(self) :
  77. connection = self.__myConnect('localhost', 'root', '', 'igusers')
  78.  
  79. for user in self.users:
  80. try:
  81. with connection.cursor() as cursor:
  82. # Create a new record
  83. sql = "INSERT INTO users (user) VALUES (%s)"
  84. cursor.execute(sql, (user))
  85. # connection is not autocommit by default. So you must commit to save
  86. # your changes.
  87. connection.commit()
  88. except Exception as e:
  89. print(e)
  90.  
  91. connection.close()
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. def checkTime():
  103. jam = datetime.datetime.now()
  104. return jam
  105.  
  106. def eksekusi(tags, loop_count):
  107.  
  108. for x in range(0, n):
  109. jam = checkTime()
  110.  
  111. if (6 < int(jam.hour) < 23):
  112. times = 0
  113. else:
  114. times = 3
  115.  
  116. users = GetUsers(tags)
  117. time.sleep(1)
  118. save = SaveUsers(users.getThem())
  119. save.save()
  120. time.sleep(60 * times)
  121.  
  122.  
  123. n = 10000
  124.  
  125. tags = [
  126. 'tasimport',
  127. 'taswanita',
  128. 'jualtas',
  129. 'tasmurah',
  130. 'taskw',
  131. 'tasbatam',
  132. 'tastangan',
  133. 'tasfashion'
  134. ]
  135.  
  136. eksekusi(tags,n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement