SHARE
TWEET

Untitled

a guest May 31st, 2017 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. import os
  3. import re
  4. import pymysql
  5. import Config
  6. import requests
  7. from os.path import isfile, join
  8. from shutil import copyfile
  9. from pprint import pprint
  10.  
  11. DB_HOST = '140.118.70.162'
  12. DB_USER = 'jobguide'
  13. DB_PASSWD = 'zNW3hw1HjMsQvOc9'
  14. DB_NAME = 'jobguide'
  15.  
  16.  
  17. DB = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWD, db=DB_NAME, port=13306, charset='utf8')
  18.  
  19. CURSOR = DB.cursor()
  20.  
  21. SQL = "INSERT INTO ptt_articles(title, author, board, content, date, ip, article_uuid) VALUES (%s, %s, %s, %s, %s, %s, %s)"
  22.  
  23.  
  24. def main():
  25.     """
  26.    file = open("job-200-300.json", 'r')
  27.    lines = file.readlines()
  28.    lines[0] = '[\n'
  29.    lines[len(lines) - 1] = '}\n]'
  30.    file.close()
  31.  
  32.    file = open("job-200-300_update.json", 'w')
  33.    file.writelines(lines)
  34.    file.close()
  35.    """
  36.     with open('job-200-300_update.json','r') as json_file:
  37.         data = json.loads(json_file.read())
  38.  
  39.     date_time = format_date(data[0]['date'])
  40.  
  41.     CURSOR.execute(SQL, (data[0]['article_title'],
  42.                          data[0]['author'],
  43.                          data[0]['board'],
  44.                          data[0]['content'],
  45.                          date_time,
  46.                          data[0]['ip'],
  47.                          data[0]['article_id'] ))
  48.  
  49.     #json_data[0]['date'][0] = ' '
  50.  
  51.  
  52.  
  53.     """
  54.    for data in json_data:
  55.        print (data['author'])
  56.    """
  57. def format_date(datetime):
  58.     temp = datetime.split(' ')
  59.     format_datetime = '%s-%s-%s %s' %(temp[4], month(temp[1]), temp[2], temp[3])
  60.     return format_datetime
  61.  
  62. def month(month):
  63.     return{
  64.         'Jan' : '01',
  65.         'Feb' : '02',
  66.         'Mar' : '03',
  67.         'Apr' : '04',
  68.         'May' : '05',
  69.         'Jun' : '06',
  70.         'Jul' : '07',
  71.         'Aug' : '08',
  72.         'Sep' : '09',
  73.         'Oct' : '10',
  74.         'Nov' : '11',
  75.         'Dec' : '12'
  76.     }[month]
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top