Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3.  
  4. # In[351]:
  5.  
  6.  
  7. try:
  8. get_ipython().system('pip install requests')
  9. get_ipython().system('pip install gql')
  10. except:
  11. pass
  12.  
  13.  
  14. # In[352]:
  15.  
  16.  
  17. import csv
  18. from random import shuffle
  19. import random
  20. from collections import defaultdict
  21. import html
  22. from datetime import datetime, timedelta
  23. from datetime import timezone
  24. import threading
  25. import requests
  26. import re
  27. import os
  28. import json
  29. import smtplib
  30. from email.mime.multipart import MIMEMultipart
  31. from email.mime.text import MIMEText
  32. import threading
  33.  
  34.  
  35. # In[353]:
  36.  
  37.  
  38. def nested_dict(n, type):
  39. if n == 1:
  40. return defaultdict(type)
  41. else:
  42. return defaultdict(lambda: nested_dict(n-1, type))
  43.  
  44.  
  45. # In[354]:
  46.  
  47.  
  48. LP_URL = '56cashnow.com'
  49. GQL_URL = 'http://prisma:4466/'
  50.  
  51.  
  52. # In[355]:
  53.  
  54.  
  55. HEADERS = {
  56. 'Content-Type': 'application/json',
  57. 'Accept': 'application/json',
  58. 'Connection': 'keep-alive',
  59. 'DNT': '1',
  60. 'Accept-Encoding': 'gzip, deflate, br'
  61. }
  62.  
  63.  
  64. # In[356]:
  65.  
  66.  
  67. # EMAIL_SERVER = smtplib.SMTP_SSL('mail.wheelchairpd.com', 465)
  68. EMAIL_SERVER = smtplib.SMTP('mail.wheelchairpd.com', 2525)
  69. EMAIL_SERVER.login('email@wheelchairpd.com', 'Localhost@123')
  70. # EMAIL_SERVER = smtplib.SMTP('smtp', 25)
  71.  
  72.  
  73. # In[357]:
  74.  
  75.  
  76. cwd = os.getcwd() + '\\'
  77.  
  78.  
  79. # In[358]:
  80.  
  81.  
  82. db = nested_dict(1, list)
  83. file_list = ['keyword', 'geo', 'title', 'subtitle', 'head', 'welcome', 'text1', 'text2', 'text3', 'text4', 'text5', 'text6', 'cta']
  84.  
  85. def read_text():
  86. for f in file_list:
  87. with open(f + '.csv') as csvfile:
  88. rows = csv.reader(csvfile, delimiter='\t')
  89. next(rows, None)
  90. for r in rows:
  91. try:
  92. db[f].append(r[0])
  93. except:
  94. continue
  95.  
  96. read_text()
  97.  
  98.  
  99. # In[359]:
  100.  
  101.  
  102. def retrieve_blog_networks():
  103.  
  104. query = """
  105. query metadatas {
  106. metadatas {
  107. id
  108. blogUrl
  109. blogNetwork
  110. secretEmail
  111. nextPostDate
  112. }
  113. }
  114. """
  115.  
  116. data = {'query': query}
  117. data = json.dumps(data)
  118. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  119. res = r.content.decode('utf8')
  120. resJson = json.loads(res)
  121. return resJson['data']['metadatas']
  122.  
  123. blog_networks = retrieve_blog_networks()
  124.  
  125.  
  126. # In[360]:
  127.  
  128.  
  129. def replace_kw(s, kw, url):
  130. rand = random.randint(0, 6)
  131.  
  132. if rand == 0:
  133. r_text = '<a href="' + url + '" alt="' + kw +'"><strong>' + kw + '</strong></a>'
  134. elif rand == 1:
  135. r_text = '<a href="' + url + '" alt="' + kw +'"><i>' + kw + '</i></a>'
  136. elif rand == 2:
  137. r_text = '<a href="' + url + '" alt="' + kw +'"><u>' + kw + '</u></a>'
  138. elif rand == 3:
  139. r_text = '<a href="' + url + '" alt="' + kw +'">' + kw + '</a>'
  140. elif rand == 4:
  141. r_text = '<strong>' + kw + '</strong>'
  142. else:
  143. r_text = kw
  144.  
  145. s = s.replace('{{keyword}}', ' ' + r_text + ' ')
  146. s = s.replace(' ', ' ')
  147. return s
  148.  
  149.  
  150. # In[361]:
  151.  
  152.  
  153. def clean_text(text):
  154. text = text.strip()
  155. text = text.replace("“", "'")
  156. text = text.replace("’", "'")
  157. text = text.replace("–", "-")
  158. text = text.replace("¬", "")
  159.  
  160. return text
  161.  
  162.  
  163. # In[362]:
  164.  
  165.  
  166. def sanitize_url(keyword, date):
  167. keyword = keyword.lower()
  168. keyword = re.sub('[^a-zA-Z0-9 \n]', '', keyword)
  169. keyword = keyword.replace(' a ', ' ')
  170. keyword = keyword.replace(' an ', ' ')
  171. keyword = keyword.replace(' the ', ' ')
  172. keywords = keyword.split(' ')
  173.  
  174. slug = keywords[0]
  175. for i in range(1, len(keywords)):
  176. if (len(slug + '-' + keywords[i]) > 40 and i == len(keywords) - 1) or (len(slug + '-' + keywords[i]) >= 40 and i < len(keywords) - 1):
  177. break
  178. else:
  179. slug = slug + '-' + keywords[i]
  180.  
  181. print(slug)
  182.  
  183. date_slug = '%d/%02d' % (date.year, date.month)
  184. return '/' + date_slug + '/' + slug + '.html'
  185.  
  186.  
  187. # In[363]:
  188.  
  189.  
  190. def get_related_url(blog_url, blog_network_id, link_type):
  191.  
  192. if link_type == "internal":
  193. query = """
  194. query posts(
  195. $where: PostWhereInput!
  196. ) {
  197. posts(where: $where, first: 50, orderBy: updatedAt_DESC) {
  198. id
  199. blogUrl
  200. keyword
  201. postUrl
  202. blogNetwork
  203. isPublished
  204. updatedAt
  205. }
  206. }
  207. """
  208. variables = {
  209. "where": {
  210. "AND": {
  211. "blogUrl": blog_url,
  212. "isPublished": True
  213. }
  214. }
  215. }
  216. else:
  217. query = """
  218. query posts(
  219. $where: PostWhereInput!
  220. ) {
  221. posts(where: $where, first: 1000, orderBy: updatedAt_DESC) {
  222. id
  223. blogUrl
  224. keyword
  225. postUrl
  226. blogNetwork
  227. isPublished
  228. updatedAt
  229. }
  230. }
  231. """
  232. variables = {
  233. "where": {
  234. "AND": {
  235. "blogNetwork": blog_network_id,
  236. "isPublished": True
  237. }
  238. }
  239. }
  240.  
  241.  
  242. data = {'query': query, 'variables': variables}
  243. data = json.dumps(data)
  244. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  245. res = r.content.decode('utf8')
  246. resJson = json.loads(res)
  247. resJson = resJson['data']['posts']
  248. shuffle(resJson)
  249.  
  250. return resJson
  251.  
  252.  
  253. # In[364]:
  254.  
  255.  
  256. def gen_related_list(blog_url, blog_network_id, link_type):
  257.  
  258. resJson = get_related_url(blog_url, blog_network_id, link_type)
  259. n = min(random.randint(3, 10), len(resJson))
  260.  
  261. content = '<ul>'
  262. for i in range(n):
  263. if resJson[i]['postUrl'] == None:
  264. resJson[i]['postUrl'] = ''
  265.  
  266. url = 'https://' + resJson[i]['blogUrl'] + resJson[i]['postUrl']
  267. content += '<li><a href="' + url + '">' + resJson[i]['keyword'] + '</a>'
  268.  
  269. content += '</ul>'
  270. return content
  271.  
  272.  
  273. # In[365]:
  274.  
  275.  
  276. def gen_post(keyword, blog_url, blog_network_id):
  277.  
  278. keyword = clean_text(keyword)
  279. lp_url = 'https://' + LP_URL + '/?keyword=' + keyword + '&refurl=' + blog_url
  280. internal_links = gen_related_list(blog_url, blog_network_id, "internal")
  281. external_links = gen_related_list(blog_url, blog_network_id, "external")
  282. urls = get_related_url(blog_url, blog_network_id, "internal")
  283.  
  284. s = {}
  285. for f in file_list:
  286. shuffle(db[f])
  287.  
  288. if len(urls) > 0:
  289. if urls[0]['postUrl'] == None:
  290. urls[0]['postUrl'] = ''
  291. url = 'https://' + urls[0]['blogUrl'] + urls[0]['postUrl']
  292. else:
  293. url = lp_url
  294.  
  295. s[f] = replace_kw(db[f][0], keyword, url)
  296. s[f] = s[f].replace('Get Started Now', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>' + LP_URL + '</u></strong></a>')
  297. s[f] = s[f].replace('{{lp}}', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>' + LP_URL + '</u></strong></a>')
  298.  
  299. if f == 'text5':
  300. s[f] = s[f].replace('Get Started Now', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>Get Started Now</u></strong></a>')
  301.  
  302. text = '<h1>' + keyword + '</h1>'
  303.  
  304. text += '<p><font style="font-size:25px; color:#f01701; font-weight:700; line-height:30px; margin-bottom:3px; display: inline-block;">'
  305. text += s['title']
  306. text += '</font></p>'
  307.  
  308. text += '<p style="color: #3d85c6;">' + s['subtitle'] + '</p><br>'
  309.  
  310. button = '<a href="' + lp_url + '" alt="' + keyword +'" style="color:#73261e;font-size: 18px;display: block;width: 80%;border: none;background-color: #FBB914;padding: 14px 28px;text-align: center; font-weight:700;"><span>>>> Click here to Apply Now &raquo;</span></a></button><br>'
  311.  
  312. text += button
  313.  
  314. text += '<p style="font-size:12px;">By clicking Apply Now, you agree to our Terms of Use and that you have read our Privacy Policy. Get Cash with <i>' + keyword +'</i>.</p>'
  315. text += '<p><a href="' + lp_url + '"><strong><u>>>> Get Your Dream Loans Now !.</u></strong></a></p><br>'
  316. text += '<p><h2 style="font-size: 25px; color:#E74C3C;">' + keyword + '</h2></p>'
  317. text += '<p><font style="font-size:20px; color:#205297; font-style:italic;">' + s['head'] + '</font></p><br>'
  318. text += '<p>' + s['welcome'] + '</p>'
  319. text += '<p>' + s['text1'] + '</p>'
  320. text += '<p>' + s['text2'] + '</p>'
  321. text += '<p>' + s['text3'] + '</p>'
  322. text += '<p>' + s['text4'] + '</p>'
  323. text += '<p>' + s['text5'] + '</p>'
  324. text += '<p><h3 style="font-size:25px; color:#205297; font-style:italic;">' + keyword + ' - ' + s['cta'] + '</h3></p>'
  325. text += '<p>' + internal_links + '</p>'
  326. text += '<p>' + s['text6'] + '</p><br>'
  327. text += '<p style="font-weight:700; text-align:center;">Don\'t Worry About Anything. We Try To Help You Happy.</p><br>'
  328. text += button + '<br><br>'
  329.  
  330. if external_links != '':
  331. text += '<h3>Reference</h3>'
  332. text += '<p>' + external_links + '</p>'
  333.  
  334. text = clean_text(text)
  335.  
  336. # text = text.encode('utf-8', 'ignore')
  337. return str(text)
  338.  
  339.  
  340. # In[366]:
  341.  
  342.  
  343. def gen_keyword(keywords, geos):
  344. shuffle(keywords)
  345. geo = geos[random.randint(0, len(geos))].split(',')
  346.  
  347. keyword = keywords[0].lower()
  348.  
  349. if keyword.find(geo[0].lower()) >= 0:
  350. geo[0] = ''
  351.  
  352. if keyword.find(geo[1].lower()) >= 0:
  353. geo[1] = ''
  354.  
  355. if keyword.find(geo[2].lower()) >= 0:
  356. geo[2] = ''
  357.  
  358. keyword = keyword.title()
  359.  
  360. kw = keyword + " in " + geo[1] + " " + geo[2] + " " + geo[3] + ", " + geo[0]
  361. kw = kw.replace(' ', ' ')
  362.  
  363. return kw
  364.  
  365.  
  366. # In[367]:
  367.  
  368.  
  369. def add_post_queue(blog_url, blog_network_id):
  370. keyword = gen_keyword(db['keyword'], db['geo'])
  371.  
  372. query = """
  373. mutation createPost(
  374. $data:PostCreateInput!
  375. ) {
  376. createPost(data: $data){
  377. id
  378. blogUrl
  379. keyword
  380. blogNetwork
  381. isPublished
  382. }
  383. }
  384. """
  385.  
  386. variables = {
  387. "data": {
  388. "blogUrl": blog_url,
  389. "keyword": keyword,
  390. "blogNetwork": int(blog_network_id),
  391. "isPublished": False,
  392. "primaryKey": str(blog_network_id) + "_" + keyword
  393. }
  394. }
  395.  
  396. data = {'query': query, 'variables': variables}
  397. data = json.dumps(data)
  398. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  399. res = r.content.decode('utf8')
  400. resJson = json.loads(res)
  401. return resJson['data']['createPost']
  402.  
  403.  
  404. # In[368]:
  405.  
  406.  
  407. def get_post_queue(blog_url):
  408.  
  409. query = """
  410. query posts(
  411. $where: PostWhereInput!
  412. ) {
  413. posts(where: $where, first: 1) {
  414. id
  415. blogUrl
  416. keyword
  417. blogNetwork
  418. isPublished
  419. }
  420. }
  421. """
  422.  
  423. variables = {
  424. "where": {
  425. "AND": {
  426. "blogUrl": blog_url,
  427. "isPublished": False
  428. }
  429. }
  430. }
  431.  
  432. data = {'query': query, 'variables': variables}
  433. data = json.dumps(data)
  434. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  435. res = r.content.decode('utf8')
  436. resJson = json.loads(res)
  437. return resJson['data']['posts'][0]
  438.  
  439.  
  440. # In[369]:
  441.  
  442.  
  443. def update_post_published(id, post_url):
  444. query = """
  445. mutation updatePost(
  446. $data: PostUpdateInput!,
  447. $where: PostWhereUniqueInput!
  448. ) {
  449. updatePost(data: $data, where: $where) {
  450. id
  451. isPublished
  452. }
  453. }
  454. """
  455.  
  456. variables = {
  457. "data": {
  458. "isPublished": True,
  459. "postUrl": post_url
  460. },
  461. "where": {
  462. "id": id
  463. }
  464. }
  465.  
  466. data = {'query': query, 'variables': variables}
  467. data = json.dumps(data)
  468. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  469. res = r.content.decode('utf8')
  470. return res
  471.  
  472.  
  473. # In[370]:
  474.  
  475.  
  476. def update_blog_nextpostdate(id):
  477. query = """
  478. mutation updateMetadata(
  479. $data: MetadataUpdateInput!,
  480. $where: MetadataWhereUniqueInput!
  481. ) {
  482. updateMetadata(data: $data, where: $where) {
  483. id
  484. blogUrl
  485. nextPostDate
  486. }
  487. }
  488. """
  489. next_post_date = datetime.now() + timedelta(hours = random.randint(0, 24), minutes=random.randint(0, 60), seconds=random.randint(0, 60))
  490.  
  491. variables = {
  492. "data": {
  493. "nextPostDate": str(next_post_date.isoformat())
  494. },
  495. "where": {
  496. "id": id
  497. }
  498. }
  499.  
  500. data = {'query': query, 'variables': variables}
  501. data = json.dumps(data)
  502. r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
  503. res = r.content.decode('utf8')
  504. return res
  505.  
  506.  
  507. # In[371]:
  508.  
  509.  
  510. def send_email(email, title, content):
  511. from_email = "email@a2hosting.com"
  512. to_email = email
  513.  
  514. msg = MIMEMultipart('alternative')
  515. msg['Subject'] = title
  516. msg['From'] = from_email
  517. msg['To'] = to_email
  518.  
  519. text = MIMEText(content, 'html')
  520. msg.attach(text)
  521. # print(msg)
  522.  
  523. EMAIL_SERVER.sendmail(from_email, to_email, msg.as_string())
  524.  
  525.  
  526. # In[372]:
  527.  
  528.  
  529. def post_by_email(blog_id, blog_url, secret_email, blog_network_id):
  530. q = add_post_queue(blog_url, blog_network_id)
  531. title = clean_text(q['keyword'])
  532. content = gen_post(clean_text(q['keyword']), q['blogUrl'], q['blogNetwork']) + "#end"
  533. post_url = sanitize_url(title, datetime.now())
  534. print(post_url)
  535. print(q)
  536. print(secret_email)
  537.  
  538. send_email(secret_email, title, content)
  539. update_post_published(q['id'], post_url)
  540. update_blog_nextpostdate(blog_id)
  541.  
  542.  
  543.  
  544. # In[373]:
  545.  
  546.  
  547. def check_post_schedule(blog_networks):
  548. for blog_network in blog_networks:
  549. if datetime.now() > datetime.strptime(blog_network['nextPostDate'][:-5], '%Y-%m-%dT%H:%M:%S'):
  550. post_by_email(blog_network['id'], blog_network['blogUrl'], blog_network['secretEmail'], blog_network['blogNetwork'])
  551.  
  552.  
  553. # In[ ]:
  554.  
  555.  
  556. # def run_thread(n_thread = 10):
  557. # n = len(blog_networks)
  558. # f = lambda A, n=int(n/n_thread): [A[i:i+n] for i in range(0, len(A), n)]
  559. # thread_list = []
  560.  
  561. # for sub_blog_network in f(blog_networks):
  562. # t = threading.Thread(target=check_post_schedule, args=(sub_blog_network,))
  563. # thread_list.append(t)
  564.  
  565. # for thread in thread_list:
  566. # thread.start()
  567.  
  568. # for thread in thread_list:
  569. # thread.join()
  570.  
  571. # run_thread(10)
  572. check_post_schedule(blog_networks)
  573.  
  574.  
  575. # In[ ]:
  576.  
  577.  
  578. # EMAIL_SERVER.quit()
  579.  
  580.  
  581. # In[ ]:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement